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