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