src/Pure/General/name_space.ML
author wenzelm
Sat Mar 14 00:13:50 2009 +0100 (2009-03-14)
changeset 30522 e26b80647189
parent 30469 de9e8f1d927c
child 32738 15bb09ca0378
permissions -rw-r--r--
removed obsolete no_base_names naming policy;
wenzelm@6118
     1
(*  Title:      Pure/General/name_space.ML
wenzelm@5012
     2
    Author:     Markus Wenzel, TU Muenchen
wenzelm@5012
     3
wenzelm@16137
     4
Generic name spaces with declared and hidden entries.  Unknown names
wenzelm@16341
     5
are considered global; no support for absolute addressing.
wenzelm@16137
     6
*)
wenzelm@16137
     7
wenzelm@26440
     8
type xstring = string;    (*external names*)
wenzelm@5012
     9
wenzelm@30215
    10
signature BASIC_NAME_SPACE =
wenzelm@30215
    11
sig
wenzelm@30215
    12
  val long_names: bool ref
wenzelm@30215
    13
  val short_names: bool ref
wenzelm@30215
    14
  val unique_names: bool ref
wenzelm@30215
    15
end;
wenzelm@30215
    16
wenzelm@5012
    17
signature NAME_SPACE =
wenzelm@5012
    18
sig
wenzelm@30215
    19
  include BASIC_NAME_SPACE
wenzelm@9120
    20
  val hidden: string -> string
wenzelm@25225
    21
  val is_hidden: string -> bool
wenzelm@5012
    22
  type T
wenzelm@5012
    23
  val empty: T
wenzelm@16137
    24
  val intern: T -> xstring -> string
wenzelm@16137
    25
  val extern: T -> string -> xstring
wenzelm@30215
    26
  val extern_flags: {long_names: bool, short_names: bool, unique_names: bool} ->
wenzelm@30215
    27
    T -> string -> xstring
wenzelm@16137
    28
  val hide: bool -> string -> T -> T
wenzelm@5012
    29
  val merge: T * T -> T
wenzelm@16137
    30
  type naming
haftmann@28965
    31
  val default_naming: naming
haftmann@29581
    32
  val declare: naming -> binding -> T -> string * T
haftmann@29581
    33
  val full_name: naming -> binding -> string
wenzelm@25072
    34
  val external_names: naming -> string -> string list
wenzelm@16137
    35
  val add_path: string -> naming -> naming
wenzelm@30418
    36
  val root_path: naming -> naming
wenzelm@30418
    37
  val parent_path: naming -> naming
wenzelm@30469
    38
  val mandatory_path: string -> naming -> naming
wenzelm@24361
    39
  type 'a table = T * 'a Symtab.table
wenzelm@30465
    40
  val define: naming -> binding * 'a -> 'a table -> string * 'a table (*exception Symtab.DUP*)
wenzelm@16341
    41
  val empty_table: 'a table
wenzelm@30465
    42
  val merge_tables: ('a * 'a -> bool) -> 'a table * 'a table -> 'a table (*exception Symtab.DUP*)
wenzelm@30233
    43
  val join_tables: (string -> 'a * 'a -> 'a) (*exception Symtab.DUP/Symtab.SAME*) ->
wenzelm@30465
    44
    'a table * 'a table -> 'a table (*exception Symtab.DUP*)
wenzelm@16848
    45
  val dest_table: 'a table -> (string * 'a) list
wenzelm@16341
    46
  val extern_table: 'a table -> (xstring * 'a) list
wenzelm@5012
    47
end;
wenzelm@5012
    48
wenzelm@5012
    49
structure NameSpace: NAME_SPACE =
wenzelm@5012
    50
struct
wenzelm@5012
    51
wenzelm@30412
    52
wenzelm@30412
    53
(** name spaces **)
wenzelm@30412
    54
wenzelm@30412
    55
(* hidden entries *)
wenzelm@5012
    56
wenzelm@16137
    57
fun hidden name = "??." ^ name;
haftmann@29338
    58
val is_hidden = String.isPrefix "??.";
wenzelm@16137
    59
wenzelm@5012
    60
wenzelm@5012
    61
(* datatype T *)
wenzelm@5012
    62
wenzelm@5012
    63
datatype T =
wenzelm@25072
    64
  NameSpace of
wenzelm@30233
    65
    (string list * string list) Symtab.table *   (*internals, hidden internals*)
wenzelm@30277
    66
    xstring list Symtab.table;                   (*externals*)
wenzelm@5012
    67
wenzelm@25072
    68
val empty = NameSpace (Symtab.empty, Symtab.empty);
wenzelm@5012
    69
wenzelm@25072
    70
fun lookup (NameSpace (tab, _)) xname =
wenzelm@17412
    71
  (case Symtab.lookup tab xname of
wenzelm@16137
    72
    NONE => (xname, true)
wenzelm@30233
    73
  | SOME ([], []) => (xname, true)
wenzelm@30233
    74
  | SOME ([name], _) => (name, true)
wenzelm@30233
    75
  | SOME (name :: _, _) => (name, false)
wenzelm@30233
    76
  | SOME ([], name' :: _) => (hidden name', true));
wenzelm@8728
    77
wenzelm@30233
    78
fun get_accesses (NameSpace (_, xtab)) name =
wenzelm@30233
    79
  (case Symtab.lookup xtab name of
wenzelm@25072
    80
    NONE => [name]
wenzelm@30233
    81
  | SOME xnames => xnames);
wenzelm@25072
    82
wenzelm@25072
    83
fun put_accesses name xnames (NameSpace (tab, xtab)) =
wenzelm@30233
    84
  NameSpace (tab, Symtab.update (name, xnames) xtab);
wenzelm@25072
    85
wenzelm@30233
    86
fun valid_accesses (NameSpace (tab, _)) name = Symtab.fold (fn (xname, (names, _)) =>
wenzelm@16444
    87
  if not (null names) andalso hd names = name then cons xname else I) tab [];
wenzelm@8728
    88
wenzelm@8728
    89
wenzelm@16137
    90
(* intern and extern *)
wenzelm@16137
    91
wenzelm@16137
    92
fun intern space xname = #1 (lookup space xname);
wenzelm@16137
    93
wenzelm@30215
    94
fun extern_flags {long_names, short_names, unique_names} space name =
wenzelm@16137
    95
  let
wenzelm@30277
    96
    fun valid require_unique xname =
wenzelm@30277
    97
      let val (name', is_unique) = lookup space xname
wenzelm@30277
    98
      in name = name' andalso (not require_unique orelse is_unique) end;
wenzelm@8728
    99
wenzelm@26440
   100
    fun ext [] = if valid false name then name else hidden name
wenzelm@30215
   101
      | ext (nm :: nms) = if valid unique_names nm then nm else ext nms;
wenzelm@16137
   102
  in
wenzelm@30215
   103
    if long_names then name
wenzelm@30359
   104
    else if short_names then Long_Name.base_name name
wenzelm@30213
   105
    else ext (get_accesses space name)
wenzelm@16137
   106
  end;
wenzelm@16137
   107
wenzelm@30215
   108
val long_names = ref false;
wenzelm@30215
   109
val short_names = ref false;
wenzelm@30215
   110
val unique_names = ref true;
wenzelm@30215
   111
wenzelm@30215
   112
fun extern space name =
wenzelm@30215
   113
  extern_flags
wenzelm@30215
   114
   {long_names = ! long_names,
wenzelm@30215
   115
    short_names = ! short_names,
wenzelm@30215
   116
    unique_names = ! unique_names} space name;
wenzelm@30215
   117
wenzelm@5012
   118
wenzelm@16137
   119
(* basic operations *)
wenzelm@16137
   120
wenzelm@25072
   121
local
wenzelm@25072
   122
wenzelm@25072
   123
fun map_space f xname (NameSpace (tab, xtab)) =
wenzelm@30233
   124
  NameSpace (Symtab.map_default (xname, ([], [])) f tab, xtab);
wenzelm@16137
   125
wenzelm@25072
   126
in
wenzelm@25072
   127
wenzelm@26440
   128
val del_name = map_space o apfst o remove (op =);
wenzelm@27196
   129
fun del_name_extra name = map_space (apfst (fn [] => [] | x :: xs => x :: remove (op =) name xs));
wenzelm@26440
   130
val add_name = map_space o apfst o update (op =);
wenzelm@26440
   131
val add_name' = map_space o apsnd o update (op =);
wenzelm@8728
   132
wenzelm@25072
   133
end;
wenzelm@25072
   134
wenzelm@8728
   135
wenzelm@8728
   136
(* hide *)
wenzelm@5012
   137
wenzelm@16137
   138
fun hide fully name space =
wenzelm@30359
   139
  if not (Long_Name.is_qualified name) then
wenzelm@8728
   140
    error ("Attempt to hide global name " ^ quote name)
wenzelm@8728
   141
  else if is_hidden name then
wenzelm@8728
   142
    error ("Attempt to hide hidden name " ^ quote name)
wenzelm@16137
   143
  else
wenzelm@16137
   144
    let val names = valid_accesses space name in
wenzelm@16137
   145
      space
wenzelm@16137
   146
      |> add_name' name name
wenzelm@30359
   147
      |> fold (del_name name)
wenzelm@30359
   148
        (if fully then names else names inter_string [Long_Name.base_name name])
wenzelm@30213
   149
      |> fold (del_name_extra name) (get_accesses space name)
wenzelm@16137
   150
    end;
wenzelm@5012
   151
wenzelm@5012
   152
wenzelm@16137
   153
(* merge *)
wenzelm@5012
   154
wenzelm@25072
   155
fun merge (NameSpace (tab1, xtab1), NameSpace (tab2, xtab2)) =
wenzelm@25072
   156
  let
wenzelm@25072
   157
    val tab' = (tab1, tab2) |> Symtab.join
wenzelm@30465
   158
      (K (fn ((names1, names1'), (names2, names2')) =>
wenzelm@30465
   159
        if pointer_eq (names1, names2) andalso pointer_eq (names1', names2') then raise Symtab.SAME
wenzelm@30233
   160
        else (Library.merge (op =) (names1, names2), Library.merge (op =) (names1', names2'))));
wenzelm@25072
   161
    val xtab' = (xtab1, xtab2) |> Symtab.join
wenzelm@30233
   162
      (K (fn xnames =>
wenzelm@30233
   163
        if pointer_eq xnames then raise Symtab.SAME
wenzelm@30233
   164
        else (Library.merge (op =) xnames)));
wenzelm@25072
   165
  in NameSpace (tab', xtab') end;
wenzelm@5012
   166
wenzelm@16137
   167
wenzelm@26440
   168
wenzelm@16137
   169
(** naming contexts **)
wenzelm@16137
   170
wenzelm@16137
   171
(* datatype naming *)
wenzelm@16137
   172
wenzelm@30522
   173
datatype naming = Naming of (string * bool) list;
wenzelm@30522
   174
fun map_naming f (Naming path) = Naming (f path);
wenzelm@16137
   175
wenzelm@30522
   176
val default_naming = Naming [];
wenzelm@16137
   177
wenzelm@30522
   178
fun add_path elems = map_naming (fn path => path @ [(elems, false)]);
wenzelm@30522
   179
val root_path = map_naming (fn _ => []);
wenzelm@30522
   180
val parent_path = map_naming (perhaps (try (#1 o split_last)));
wenzelm@30522
   181
fun mandatory_path elems = map_naming (fn path => path @ [(elems, true)]);
haftmann@28860
   182
haftmann@28860
   183
wenzelm@30233
   184
(* full name *)
wenzelm@30233
   185
wenzelm@30465
   186
fun err_bad binding = error ("Bad name binding " ^ quote (Binding.str_of binding));
haftmann@28860
   187
wenzelm@30522
   188
fun name_spec (Naming path) binding =
wenzelm@30233
   189
  let
wenzelm@30412
   190
    val (prefix, name) = Binding.dest binding;
wenzelm@30438
   191
    val _ = Long_Name.is_qualified name andalso err_bad binding;
wenzelm@30412
   192
wenzelm@30412
   193
    val spec1 = maps (fn (a, b) => map (rpair b) (Long_Name.explode a)) (path @ prefix);
wenzelm@30438
   194
    val spec2 = if name = "" then [] else [(name, true)];
wenzelm@30412
   195
    val spec = spec1 @ spec2;
wenzelm@30412
   196
    val _ =
wenzelm@30412
   197
      exists (fn (a, _) => a = "" orelse a = "??" orelse exists_string (fn s => s = "\"") a) spec
wenzelm@30412
   198
      andalso err_bad binding;
wenzelm@30412
   199
  in if null spec2 then [] else spec end;
wenzelm@30412
   200
wenzelm@30412
   201
fun full naming = name_spec naming #> map fst;
wenzelm@30412
   202
fun full_name naming = full naming #> Long_Name.implode;
wenzelm@30412
   203
wenzelm@30412
   204
wenzelm@30412
   205
(* accesses *)
wenzelm@30412
   206
wenzelm@30412
   207
fun mandatory xs = map_filter (fn (x, true) => SOME x | _ => NONE) xs;
wenzelm@30412
   208
wenzelm@30412
   209
fun mandatory_prefixes xs = mandatory xs :: mandatory_prefixes1 xs
wenzelm@30412
   210
and mandatory_prefixes1 [] = []
wenzelm@30412
   211
  | mandatory_prefixes1 ((x, true) :: xs) = map (cons x) (mandatory_prefixes1 xs)
wenzelm@30412
   212
  | mandatory_prefixes1 ((x, false) :: xs) = map (cons x) (mandatory_prefixes xs);
wenzelm@30412
   213
wenzelm@30412
   214
fun mandatory_suffixes xs = map rev (mandatory_prefixes (rev xs));
wenzelm@30412
   215
wenzelm@30522
   216
fun accesses naming binding =
wenzelm@30412
   217
  let
wenzelm@30412
   218
    val spec = name_spec naming binding;
wenzelm@30412
   219
    val sfxs = mandatory_suffixes spec;
wenzelm@30412
   220
    val pfxs = mandatory_prefixes spec;
wenzelm@30522
   221
  in pairself (map Long_Name.implode) (sfxs @ pfxs, sfxs) end;
wenzelm@30412
   222
wenzelm@30412
   223
fun external_names naming = #2 o accesses naming o Binding.qualified_name;
wenzelm@30233
   224
wenzelm@30233
   225
wenzelm@30233
   226
(* declaration *)
haftmann@28860
   227
wenzelm@30233
   228
fun declare naming binding space =
wenzelm@30233
   229
  let
wenzelm@30412
   230
    val names = full naming binding;
wenzelm@30412
   231
    val name = Long_Name.implode names;
wenzelm@30412
   232
    val _ = name = "" andalso err_bad binding;
wenzelm@30412
   233
    val (accs, accs') = accesses naming binding;
wenzelm@30233
   234
    val space' = space |> fold (add_name name) accs |> put_accesses name accs';
wenzelm@30233
   235
  in (name, space') end;
haftmann@28860
   236
wenzelm@16137
   237
wenzelm@16341
   238
wenzelm@16341
   239
(** name spaces coupled with symbol tables **)
wenzelm@16341
   240
wenzelm@16341
   241
type 'a table = T * 'a Symtab.table;
wenzelm@16341
   242
wenzelm@30465
   243
fun define naming (binding, x) (space, tab) =
wenzelm@30233
   244
  let val (name, space') = declare naming binding space
wenzelm@30233
   245
  in (name, (space', Symtab.update_new (name, x) tab)) end;
wenzelm@16341
   246
wenzelm@30233
   247
val empty_table = (empty, Symtab.empty);
haftmann@28860
   248
wenzelm@16341
   249
fun merge_tables eq ((space1, tab1), (space2, tab2)) =
wenzelm@19367
   250
  (merge (space1, space2), Symtab.merge eq (tab1, tab2));
wenzelm@16341
   251
haftmann@28991
   252
fun join_tables f ((space1, tab1), (space2, tab2)) =
haftmann@28991
   253
  (merge (space1, space2), Symtab.join f (tab1, tab2));
haftmann@28991
   254
wenzelm@16848
   255
fun ext_table (space, tab) =
wenzelm@16848
   256
  Symtab.fold (fn (name, x) => cons ((name, extern space name), x)) tab []
wenzelm@16848
   257
  |> Library.sort_wrt (#2 o #1);
wenzelm@16848
   258
wenzelm@16848
   259
fun dest_table tab = map (apfst #1) (ext_table tab);
wenzelm@16848
   260
fun extern_table tab = map (apfst #2) (ext_table tab);
wenzelm@16341
   261
wenzelm@5012
   262
end;
wenzelm@30215
   263
wenzelm@30215
   264
structure BasicNameSpace: BASIC_NAME_SPACE = NameSpace;
wenzelm@30215
   265
open BasicNameSpace;
wenzelm@30215
   266