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