src/Pure/General/name_space.ML
author wenzelm
Wed Jul 10 14:47:48 2002 +0200 (2002-07-10)
changeset 13332 f130bcf29620
parent 12588 0361fd72f1a7
child 14198 8ea2645b8132
permissions -rw-r--r--
added accesses';
wenzelm@6118
     1
(*  Title:      Pure/General/name_space.ML
wenzelm@5012
     2
    ID:         $Id$
wenzelm@5012
     3
    Author:     Markus Wenzel, TU Muenchen
wenzelm@8806
     4
    License:    GPL (GNU GENERAL PUBLIC LICENSE)
wenzelm@5012
     5
wenzelm@5012
     6
Hierarchically structured name spaces.
wenzelm@5012
     7
wenzelm@5012
     8
More general than ML-like nested structures, but also slightly more
wenzelm@5012
     9
ad-hoc.  Does not support absolute addressing.  Unknown names are
wenzelm@5012
    10
implicitely considered to be declared outermost.
wenzelm@5012
    11
*)
wenzelm@5012
    12
wenzelm@5012
    13
signature NAME_SPACE =
wenzelm@5012
    14
sig
wenzelm@12588
    15
  val separator: string                 (*single char*)
wenzelm@9120
    16
  val hidden: string -> string
wenzelm@5012
    17
  val append: string -> string -> string
wenzelm@5012
    18
  val unpack: string -> string list
wenzelm@5012
    19
  val pack: string list -> string
wenzelm@5012
    20
  val base: string -> string
wenzelm@11829
    21
  val map_base: (string -> string) -> string -> string
wenzelm@8728
    22
  val is_qualified: string -> bool
wenzelm@5012
    23
  val accesses: string -> string list
wenzelm@13332
    24
  val accesses': string -> string list
wenzelm@5012
    25
  type T
wenzelm@5012
    26
  val empty: T
wenzelm@5012
    27
  val extend: T * string list -> T
wenzelm@5012
    28
  val merge: T * T -> T
wenzelm@12588
    29
  val hide: bool -> T * string list -> T
wenzelm@5012
    30
  val intern: T -> string -> string
wenzelm@5012
    31
  val extern: T -> string -> string
wenzelm@5175
    32
  val long_names: bool ref
wenzelm@5175
    33
  val cond_extern: T -> string -> string
wenzelm@6845
    34
  val cond_extern_table: T -> 'a Symtab.table -> (string * 'a) list
wenzelm@5012
    35
  val dest: T -> (string * string list) list
wenzelm@5012
    36
end;
wenzelm@5012
    37
wenzelm@5012
    38
structure NameSpace: NAME_SPACE =
wenzelm@5012
    39
struct
wenzelm@5012
    40
wenzelm@5012
    41
(** long identifiers **)
wenzelm@5012
    42
wenzelm@5012
    43
val separator = ".";
wenzelm@8728
    44
fun hidden name = "??." ^ name;
wenzelm@8728
    45
fun is_hidden name = (case explode name of "?" :: "?" :: "." :: _ => true | _ => false);
wenzelm@5012
    46
wenzelm@5012
    47
fun append name1 name2 = name1 ^ separator ^ name2;
wenzelm@5012
    48
wenzelm@5012
    49
val unpack = space_explode separator;
wenzelm@5012
    50
val pack = space_implode separator;
wenzelm@5012
    51
wenzelm@5012
    52
val base = last_elem o unpack;
wenzelm@11829
    53
wenzelm@11829
    54
fun map_base f name =
wenzelm@11829
    55
  let val uname = unpack name
wenzelm@11829
    56
  in pack (map_nth_elem (length uname - 1) f uname) end;
wenzelm@11829
    57
wenzelm@8728
    58
fun is_qualified name = length (unpack name) > 1;
wenzelm@5012
    59
wenzelm@5012
    60
fun accesses name =
wenzelm@5012
    61
  let
wenzelm@5012
    62
    val uname = unpack name;
wenzelm@5012
    63
    val (q, b) = split_last uname;
wenzelm@12251
    64
    val sfxs = Library.suffixes1 uname;
wenzelm@12251
    65
    val pfxs = map (fn x => x @ [b]) (Library.prefixes1 q);
wenzelm@5012
    66
  in map pack (sfxs @ pfxs) end;
wenzelm@5012
    67
wenzelm@13332
    68
val accesses' = map pack o Library.suffixes1 o unpack;
wenzelm@13332
    69
wenzelm@5012
    70
wenzelm@5012
    71
wenzelm@5012
    72
(** name spaces **)
wenzelm@5012
    73
wenzelm@8728
    74
(* basic operations *)
wenzelm@8728
    75
wenzelm@8728
    76
fun map_space f xname tab =
wenzelm@8728
    77
  Symtab.update ((xname, f (if_none (Symtab.lookup (tab, xname)) ([], []))), tab);
wenzelm@8728
    78
wenzelm@8728
    79
fun change f xname (name, tab) =
wenzelm@8728
    80
  map_space (fn (names, names') => (f names name, names')) xname tab;
wenzelm@8728
    81
wenzelm@8728
    82
fun change' f xname (name', tab) =
wenzelm@8728
    83
  map_space (fn (names, names') => (names, f names' name')) xname tab;
wenzelm@8728
    84
wenzelm@8728
    85
fun del names nm = if nm mem_string names then Library.gen_rem (op =) (names, nm) else names;
wenzelm@8728
    86
fun add names nm = nm :: del names nm;
wenzelm@8728
    87
wenzelm@8728
    88
wenzelm@5012
    89
(* datatype T *)
wenzelm@5012
    90
wenzelm@5012
    91
datatype T =
wenzelm@8728
    92
  NameSpace of (string list * string list) Symtab.table;
wenzelm@5012
    93
wenzelm@5012
    94
val empty = NameSpace Symtab.empty;
wenzelm@5012
    95
wenzelm@5012
    96
wenzelm@8728
    97
(* extend *)            (*later entries preferred*)
wenzelm@8728
    98
wenzelm@8728
    99
fun extend_aux (name, tab) =
wenzelm@8728
   100
  if is_hidden name then
wenzelm@8728
   101
    error ("Attempt to declare hidden name " ^ quote name)
wenzelm@8728
   102
  else foldl (fn (tb, xname) => change add xname (name, tb)) (tab, accesses name);
wenzelm@5012
   103
wenzelm@8728
   104
fun extend (NameSpace tab, names) = NameSpace (foldr extend_aux (names, tab));
wenzelm@8728
   105
wenzelm@8728
   106
wenzelm@8728
   107
(* merge *)             (*1st preferred over 2nd*)
wenzelm@8728
   108
wenzelm@8728
   109
fun merge_aux (tab, (xname, (names, names'))) =
wenzelm@8728
   110
  foldr (change add xname) (names, foldr (change' add xname) (names', tab));
wenzelm@5012
   111
wenzelm@8728
   112
fun merge (NameSpace tab1, NameSpace tab2) =
wenzelm@8728
   113
  NameSpace (Symtab.foldl merge_aux (tab2, tab1));
wenzelm@8728
   114
wenzelm@8728
   115
wenzelm@8728
   116
(* hide *)
wenzelm@5012
   117
wenzelm@12588
   118
fun hide_aux fully (name, tab) =
wenzelm@8728
   119
  if not (is_qualified name) then
wenzelm@8728
   120
    error ("Attempt to hide global name " ^ quote name)
wenzelm@8728
   121
  else if is_hidden name then
wenzelm@8728
   122
    error ("Attempt to hide hidden name " ^ quote name)
wenzelm@8728
   123
  else (change' add name (name,
wenzelm@12588
   124
    foldl (fn (tb, xname) => change del xname (name, tb))
wenzelm@12588
   125
      (tab, if fully then accesses name else [base name])));
wenzelm@8728
   126
wenzelm@12588
   127
fun hide fully (NameSpace tab, names) = NameSpace (foldr (hide_aux fully) (names, tab));
wenzelm@5012
   128
wenzelm@5012
   129
wenzelm@5012
   130
(* intern / extern names *)
wenzelm@5012
   131
wenzelm@8728
   132
fun lookup (NameSpace tab) xname =
wenzelm@8728
   133
  (case Symtab.lookup (tab, xname) of
wenzelm@8728
   134
    None => (xname, true)
wenzelm@8728
   135
  | Some ([name], _) => (name, true)
wenzelm@8728
   136
  | Some (name :: _, _) => (name, false)
wenzelm@8728
   137
  | Some ([], []) => (xname, true)
wenzelm@8728
   138
  | Some ([], name' :: _) => (hidden name', true));
wenzelm@8728
   139
wenzelm@8728
   140
fun intern spc xname = #1 (lookup spc xname);
wenzelm@5012
   141
wenzelm@5012
   142
fun extern space name =
wenzelm@5012
   143
  let
wenzelm@8728
   144
    fun try [] = hidden name
wenzelm@5012
   145
      | try (nm :: nms) =
wenzelm@8728
   146
          let val (full_nm, uniq) = lookup space nm
wenzelm@8728
   147
          in if full_nm = name andalso uniq then nm else try nms end
wenzelm@13332
   148
  in try (accesses' name) end;
wenzelm@5012
   149
wenzelm@5175
   150
(*prune names on output by default*)
wenzelm@5175
   151
val long_names = ref false;
wenzelm@5175
   152
wenzelm@5175
   153
fun cond_extern space = if ! long_names then I else extern space;
wenzelm@5175
   154
wenzelm@6845
   155
fun cond_extern_table space tab =
wenzelm@6845
   156
  Library.sort_wrt #1 (map (apfst (cond_extern space)) (Symtab.dest tab));
wenzelm@6845
   157
wenzelm@5012
   158
wenzelm@5012
   159
(* dest *)
wenzelm@5012
   160
wenzelm@8728
   161
fun dest_entry (xname, ([], _)) = None
wenzelm@8728
   162
  | dest_entry (xname, (name :: _, _)) = Some (name, xname);
wenzelm@8728
   163
wenzelm@5012
   164
fun dest (NameSpace tab) =
wenzelm@5012
   165
  map (apsnd (sort (int_ord o pairself size)))
wenzelm@8728
   166
    (Symtab.dest (Symtab.make_multi (mapfilter dest_entry (Symtab.dest tab))));
wenzelm@5012
   167
wenzelm@5012
   168
wenzelm@5012
   169
end;
wenzelm@5175
   170
wenzelm@5175
   171
wenzelm@5175
   172
val long_names = NameSpace.long_names;