src/Pure/General/name_space.ML
author wenzelm
Wed Oct 17 23:16:33 2007 +0200 (2007-10-17)
changeset 25072 03f57b516e12
parent 24361 52a14669f9e9
child 25225 e638164593bf
permissions -rw-r--r--
store external accesses within name space (as produced by naming policy);
improved sticky_prefix: suppress redundant accesses to achieve shorter output;
removed unused interfaces;
replaced accesses' by external_names (depening on naming);
wenzelm@6118
     1
(*  Title:      Pure/General/name_space.ML
wenzelm@5012
     2
    ID:         $Id$
wenzelm@5012
     3
    Author:     Markus Wenzel, TU Muenchen
wenzelm@5012
     4
wenzelm@16137
     5
Generic name spaces with declared and hidden entries.  Unknown names
wenzelm@16341
     6
are considered global; no support for absolute addressing.
wenzelm@16137
     7
*)
wenzelm@16137
     8
wenzelm@16137
     9
type bstring = string;    (*names to be bound / internalized*)
wenzelm@16137
    10
type xstring = string;    (*externalized names / to be printed*)
wenzelm@5012
    11
wenzelm@16137
    12
signature BASIC_NAME_SPACE =
wenzelm@16137
    13
sig
wenzelm@16137
    14
  val long_names: bool ref
wenzelm@16137
    15
  val short_names: bool ref
wenzelm@16137
    16
  val unique_names: bool ref
wenzelm@16137
    17
end;
wenzelm@5012
    18
wenzelm@5012
    19
signature NAME_SPACE =
wenzelm@5012
    20
sig
wenzelm@16137
    21
  include BASIC_NAME_SPACE
wenzelm@9120
    22
  val hidden: string -> string
wenzelm@16137
    23
  val separator: string                 (*single char*)
wenzelm@16137
    24
  val is_qualified: string -> bool
wenzelm@21858
    25
  val implode: string list -> string
wenzelm@21858
    26
  val explode: string -> string list
wenzelm@16137
    27
  val append: string -> string -> string
wenzelm@16137
    28
  val qualified: string -> string -> string
wenzelm@5012
    29
  val base: string -> string
wenzelm@20530
    30
  val qualifier: string -> string
wenzelm@11829
    31
  val map_base: (string -> string) -> string -> string
wenzelm@5012
    32
  type T
wenzelm@5012
    33
  val empty: T
wenzelm@16137
    34
  val intern: T -> xstring -> string
wenzelm@16137
    35
  val extern: T -> string -> xstring
wenzelm@16137
    36
  val hide: bool -> string -> T -> T
wenzelm@5012
    37
  val merge: T * T -> T
wenzelm@16137
    38
  type naming
wenzelm@16137
    39
  val path_of: naming -> string
wenzelm@25072
    40
  val external_names: naming -> string -> string list
wenzelm@16137
    41
  val full: naming -> bstring -> string
wenzelm@16137
    42
  val declare: naming -> string -> T -> T
wenzelm@16137
    43
  val default_naming: naming
wenzelm@16137
    44
  val add_path: string -> naming -> naming
wenzelm@19015
    45
  val no_base_names: naming -> naming
wenzelm@16137
    46
  val qualified_names: naming -> naming
wenzelm@19055
    47
  val sticky_prefix: string -> naming -> naming
wenzelm@24361
    48
  type 'a table = T * 'a Symtab.table
wenzelm@16341
    49
  val empty_table: 'a table
haftmann@23086
    50
  val extend_table: naming -> (bstring * 'a) list -> 'a table -> 'a table
wenzelm@16341
    51
  val merge_tables: ('a * 'a -> bool) -> 'a table * 'a table -> 'a table
wenzelm@16848
    52
  val dest_table: 'a table -> (string * 'a) list
wenzelm@16341
    53
  val extern_table: 'a table -> (xstring * 'a) list
wenzelm@5012
    54
end;
wenzelm@5012
    55
wenzelm@5012
    56
structure NameSpace: NAME_SPACE =
wenzelm@5012
    57
struct
wenzelm@5012
    58
wenzelm@5012
    59
(** long identifiers **)
wenzelm@5012
    60
wenzelm@16137
    61
fun hidden name = "??." ^ name;
wenzelm@16137
    62
val is_hidden = String.isPrefix "??."
wenzelm@16137
    63
wenzelm@5012
    64
val separator = ".";
wenzelm@19305
    65
val is_qualified = exists_string (fn s => s = separator);
wenzelm@5012
    66
wenzelm@21858
    67
val implode_name = space_implode separator;
wenzelm@21858
    68
val explode_name = space_explode separator;
wenzelm@5012
    69
wenzelm@16137
    70
fun append name1 "" = name1
wenzelm@16137
    71
  | append "" name2 = name2
wenzelm@16137
    72
  | append name1 name2 = name1 ^ separator ^ name2;
wenzelm@16137
    73
wenzelm@16137
    74
fun qualified path name =
wenzelm@16137
    75
  if path = "" orelse name = "" then name
wenzelm@16137
    76
  else path ^ separator ^ name;
wenzelm@11829
    77
wenzelm@16137
    78
fun base "" = ""
wenzelm@21858
    79
  | base name = List.last (explode_name name);
wenzelm@11829
    80
wenzelm@20530
    81
fun qualifier "" = ""
wenzelm@21858
    82
  | qualifier name = implode_name (#1 (split_last (explode_name name)));
wenzelm@5012
    83
wenzelm@16137
    84
fun map_base _ "" = ""
wenzelm@16137
    85
  | map_base f name =
wenzelm@21858
    86
      let val names = explode_name name
wenzelm@21858
    87
      in implode_name (nth_map (length names - 1) f names) end;
wenzelm@16137
    88
wenzelm@19015
    89
wenzelm@25072
    90
(* standard accesses *)
wenzelm@19015
    91
wenzelm@19015
    92
infixr 6 @@;
wenzelm@19015
    93
fun ([] @@ yss) = []
wenzelm@19015
    94
  | ((xs :: xss) @@ yss) = map (fn ys => xs @ ys) yss @ (xss @@ yss);
wenzelm@19015
    95
wenzelm@19015
    96
fun suffixes_prefixes list =
wenzelm@5012
    97
  let
wenzelm@19015
    98
    val (xs, ws) = chop (length list - 1) list;
wenzelm@19015
    99
    val sfxs = suffixes xs @@ [ws];
wenzelm@19015
   100
    val pfxs = prefixes1 xs @@ [ws];
wenzelm@25072
   101
  in (sfxs @ pfxs, sfxs) end;
wenzelm@19015
   102
wenzelm@19015
   103
fun suffixes_prefixes_split i k list =
wenzelm@19015
   104
  let
wenzelm@19015
   105
    val (((xs, ys), zs), ws) = list |> chop i ||>> chop k ||>> chop (length list - (i + k + 1));
wenzelm@19015
   106
    val sfxs =
wenzelm@19015
   107
      [ys] @@ suffixes zs @@ [ws] @
wenzelm@19015
   108
      suffixes1 xs @@ [ys @ zs @ ws];
wenzelm@19015
   109
    val pfxs =
wenzelm@19015
   110
      prefixes1 xs @@ [ys @ ws] @
wenzelm@19015
   111
      [xs @ ys] @@ prefixes1 zs @@ [ws];
wenzelm@25072
   112
  in (sfxs @ pfxs, sfxs) end;
wenzelm@13332
   113
wenzelm@5012
   114
wenzelm@5012
   115
wenzelm@5012
   116
(** name spaces **)
wenzelm@5012
   117
wenzelm@5012
   118
(* datatype T *)
wenzelm@5012
   119
wenzelm@5012
   120
datatype T =
wenzelm@25072
   121
  NameSpace of
wenzelm@25072
   122
    ((string list * string list) * stamp) Symtab.table *   (*internals, hidden internals*)
wenzelm@25072
   123
    (string list * stamp) Symtab.table;                    (*externals*)
wenzelm@5012
   124
wenzelm@25072
   125
val empty = NameSpace (Symtab.empty, Symtab.empty);
wenzelm@5012
   126
wenzelm@25072
   127
fun lookup (NameSpace (tab, _)) xname =
wenzelm@17412
   128
  (case Symtab.lookup tab xname of
wenzelm@16137
   129
    NONE => (xname, true)
wenzelm@19030
   130
  | SOME (([], []), _) => (xname, true)
wenzelm@19030
   131
  | SOME (([name], _), _) => (name, true)
wenzelm@19030
   132
  | SOME ((name :: _, _), _) => (name, false)
wenzelm@19030
   133
  | SOME (([], name' :: _), _) => (hidden name', true));
wenzelm@8728
   134
wenzelm@25072
   135
fun get_accesses (NameSpace (_, tab)) name =
wenzelm@25072
   136
  (case Symtab.lookup tab name of
wenzelm@25072
   137
    NONE => [name]
wenzelm@25072
   138
  | SOME (xnames, _) => xnames);
wenzelm@25072
   139
wenzelm@25072
   140
fun put_accesses name xnames (NameSpace (tab, xtab)) =
wenzelm@25072
   141
  NameSpace (tab, Symtab.update (name, (xnames, stamp ())) xtab);
wenzelm@25072
   142
wenzelm@25072
   143
fun valid_accesses (NameSpace (tab, _)) name = Symtab.fold (fn (xname, ((names, _), _)) =>
wenzelm@16444
   144
  if not (null names) andalso hd names = name then cons xname else I) tab [];
wenzelm@8728
   145
wenzelm@8728
   146
wenzelm@16137
   147
(* intern and extern *)
wenzelm@16137
   148
wenzelm@16137
   149
fun intern space xname = #1 (lookup space xname);
wenzelm@16137
   150
wenzelm@16137
   151
val long_names = ref false;
wenzelm@16137
   152
val short_names = ref false;
wenzelm@16137
   153
val unique_names = ref true;
wenzelm@16137
   154
wenzelm@16137
   155
fun extern space name =
wenzelm@16137
   156
  let
wenzelm@16137
   157
    fun valid unique xname =
wenzelm@16137
   158
      let val (name', uniq) = lookup space xname
wenzelm@16137
   159
      in name = name' andalso (uniq orelse (not unique)) end;
wenzelm@8728
   160
wenzelm@16137
   161
    fun ex [] = if valid false name then name else hidden name
wenzelm@16137
   162
      | ex (nm :: nms) = if valid (! unique_names) nm then nm else ex nms;
wenzelm@16137
   163
  in
wenzelm@16137
   164
    if ! long_names then name
wenzelm@16137
   165
    else if ! short_names then base name
wenzelm@25072
   166
    else ex (get_accesses space name)
wenzelm@16137
   167
  end;
wenzelm@16137
   168
wenzelm@5012
   169
wenzelm@16137
   170
(* basic operations *)
wenzelm@16137
   171
wenzelm@25072
   172
local
wenzelm@25072
   173
wenzelm@25072
   174
fun map_space f xname (NameSpace (tab, xtab)) =
haftmann@23086
   175
  NameSpace (Symtab.map_default (xname, (([], []), stamp ()))
wenzelm@25072
   176
    (fn (entry, _) => (f entry, stamp ())) tab, xtab);
wenzelm@16137
   177
wenzelm@16137
   178
fun del (name: string) = remove (op =) name;
wenzelm@16137
   179
fun add name names = name :: del name names;
wenzelm@16137
   180
wenzelm@25072
   181
in
wenzelm@25072
   182
wenzelm@16137
   183
val del_name = map_space o apfst o del;
wenzelm@16137
   184
val add_name = map_space o apfst o add;
wenzelm@16137
   185
val add_name' = map_space o apsnd o add;
wenzelm@8728
   186
wenzelm@25072
   187
end;
wenzelm@25072
   188
wenzelm@8728
   189
wenzelm@8728
   190
(* hide *)
wenzelm@5012
   191
wenzelm@16137
   192
fun hide fully name space =
wenzelm@8728
   193
  if not (is_qualified name) then
wenzelm@8728
   194
    error ("Attempt to hide global name " ^ quote name)
wenzelm@8728
   195
  else if is_hidden name then
wenzelm@8728
   196
    error ("Attempt to hide hidden name " ^ quote name)
wenzelm@16137
   197
  else
wenzelm@16137
   198
    let val names = valid_accesses space name in
wenzelm@16137
   199
      space
wenzelm@16137
   200
      |> add_name' name name
wenzelm@16137
   201
      |> fold (del_name name) (if fully then names else names inter_string [base name])
wenzelm@16137
   202
    end;
wenzelm@5012
   203
wenzelm@5012
   204
wenzelm@16137
   205
(* merge *)
wenzelm@5012
   206
wenzelm@25072
   207
(* FIXME non-canonical merge order!? *)
wenzelm@6845
   208
wenzelm@25072
   209
fun merge (NameSpace (tab1, xtab1), NameSpace (tab2, xtab2)) =
wenzelm@25072
   210
  let
wenzelm@25072
   211
    val tab' = (tab1, tab2) |> Symtab.join
wenzelm@25072
   212
      (K (fn (((names1, names1'), stamp1), ((names2, names2'), stamp2)) =>
wenzelm@25072
   213
        if stamp1 = stamp2 then raise Symtab.SAME
wenzelm@25072
   214
        else
wenzelm@25072
   215
          ((Library.merge (op =) (names2, names1),
wenzelm@25072
   216
            Library.merge (op =) (names2', names1')), stamp ())));
wenzelm@25072
   217
    val xtab' = (xtab1, xtab2) |> Symtab.join
wenzelm@25072
   218
      (K (fn ((xnames1, stamp1), (xnames2, stamp2)) =>
wenzelm@25072
   219
        if stamp1 = stamp2 then raise Symtab.SAME
wenzelm@25072
   220
        else (Library.merge (op =) (xnames2, xnames1), stamp ())));
wenzelm@25072
   221
  in NameSpace (tab', xtab') end;
wenzelm@5012
   222
wenzelm@16137
   223
wenzelm@16137
   224
(** naming contexts **)
wenzelm@16137
   225
wenzelm@16137
   226
(* datatype naming *)
wenzelm@16137
   227
wenzelm@16137
   228
datatype naming = Naming of
wenzelm@25072
   229
  string *                                                 (*path*)
wenzelm@25072
   230
  ((string -> string -> string) *                          (*qualify*)
wenzelm@25072
   231
   (string list -> string list list * string list list));  (*accesses*)
wenzelm@16137
   232
wenzelm@16137
   233
fun path_of (Naming (path, _)) = path;
wenzelm@25072
   234
fun accesses (Naming (_, (_, accs))) = accs;
wenzelm@25072
   235
wenzelm@25072
   236
fun external_names naming = map implode_name o #2 o accesses naming o explode_name;
wenzelm@16137
   237
wenzelm@16137
   238
wenzelm@16137
   239
(* declarations *)
wenzelm@16137
   240
wenzelm@16137
   241
fun full (Naming (path, (qualify, _))) = qualify path;
wenzelm@16137
   242
wenzelm@25072
   243
fun declare naming name space =
wenzelm@16137
   244
  if is_hidden name then
wenzelm@16137
   245
    error ("Attempt to declare hidden name " ^ quote name)
wenzelm@16137
   246
  else
wenzelm@21962
   247
    let
wenzelm@21962
   248
      val names = explode_name name;
wenzelm@23668
   249
      val _ = (null names orelse exists (fn s => s = "") names
wenzelm@23668
   250
          orelse exists_string (fn s => s = "\"") name) andalso
wenzelm@21962
   251
        error ("Bad name declaration " ^ quote name);
wenzelm@25072
   252
      val (accs, accs') = pairself (map implode_name) (accesses naming names);
wenzelm@25072
   253
    in space |> fold (add_name name) accs |> put_accesses name accs' end;
wenzelm@16137
   254
wenzelm@16137
   255
wenzelm@16137
   256
(* manipulate namings *)
wenzelm@16137
   257
wenzelm@16137
   258
fun reject_qualified name =
wenzelm@16137
   259
  if is_qualified name then
wenzelm@16137
   260
    error ("Attempt to declare qualified name " ^ quote name)
wenzelm@16137
   261
  else name;
wenzelm@16137
   262
wenzelm@16137
   263
val default_naming =
wenzelm@16137
   264
  Naming ("", (fn path => qualified path o reject_qualified, suffixes_prefixes));
wenzelm@16137
   265
wenzelm@16137
   266
fun add_path elems (Naming (path, policy)) =
wenzelm@16137
   267
  if elems = "//" then Naming ("", (qualified, #2 policy))
wenzelm@16137
   268
  else if elems = "/" then Naming ("", policy)
wenzelm@20530
   269
  else if elems = ".." then Naming (qualifier path, policy)
wenzelm@16137
   270
  else Naming (append path elems, policy);
wenzelm@16137
   271
wenzelm@16137
   272
fun no_base_names (Naming (path, (qualify, accs))) =
wenzelm@25072
   273
  Naming (path, (qualify, pairself (filter_out (fn [_] => true | _ => false)) o accs));
wenzelm@16137
   274
wenzelm@19015
   275
fun qualified_names (Naming (path, (_, accs))) = Naming (path, (qualified, accs));
wenzelm@19015
   276
wenzelm@19055
   277
fun sticky_prefix prfx (Naming (path, (qualify, _))) =
wenzelm@19055
   278
  Naming (append path prfx,
wenzelm@21858
   279
    (qualify, suffixes_prefixes_split (length (explode_name path)) (length (explode_name prfx))));
wenzelm@19015
   280
wenzelm@16137
   281
wenzelm@16341
   282
wenzelm@16341
   283
(** name spaces coupled with symbol tables **)
wenzelm@16341
   284
wenzelm@16341
   285
type 'a table = T * 'a Symtab.table;
wenzelm@16341
   286
wenzelm@16341
   287
val empty_table = (empty, Symtab.empty);
wenzelm@16341
   288
haftmann@23086
   289
fun extend_table naming bentries (space, tab) =
wenzelm@16341
   290
  let val entries = map (apfst (full naming)) bentries
wenzelm@16341
   291
  in (fold (declare naming o #1) entries space, Symtab.extend (tab, entries)) end;
wenzelm@16341
   292
wenzelm@16341
   293
fun merge_tables eq ((space1, tab1), (space2, tab2)) =
wenzelm@19367
   294
  (merge (space1, space2), Symtab.merge eq (tab1, tab2));
wenzelm@16341
   295
wenzelm@16848
   296
fun ext_table (space, tab) =
wenzelm@16848
   297
  Symtab.fold (fn (name, x) => cons ((name, extern space name), x)) tab []
wenzelm@16848
   298
  |> Library.sort_wrt (#2 o #1);
wenzelm@16848
   299
wenzelm@16848
   300
fun dest_table tab = map (apfst #1) (ext_table tab);
wenzelm@16848
   301
fun extern_table tab = map (apfst #2) (ext_table tab);
wenzelm@16341
   302
wenzelm@21858
   303
wenzelm@21858
   304
(*final declarations of this structure!*)
wenzelm@21858
   305
val implode = implode_name;
wenzelm@21858
   306
val explode = explode_name;
wenzelm@21858
   307
wenzelm@5012
   308
end;
wenzelm@5175
   309
wenzelm@16137
   310
structure BasicNameSpace: BASIC_NAME_SPACE = NameSpace;
wenzelm@16137
   311
open BasicNameSpace;