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