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