src/Pure/General/name_space.ML
author wenzelm
Thu Oct 18 21:02:46 2001 +0200 (2001-10-18)
changeset 11829 f252646080fc
parent 9120 04a46ceace35
child 12251 53b7962bcdb1
permissions -rw-r--r--
added map_base;
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@5012
    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@5012
    24
  type T
wenzelm@5012
    25
  val empty: T
wenzelm@5012
    26
  val extend: T * string list -> T
wenzelm@5012
    27
  val merge: T * T -> T
wenzelm@8728
    28
  val hide: T * string list -> T
wenzelm@5012
    29
  val intern: T -> string -> string
wenzelm@5012
    30
  val extern: T -> string -> string
wenzelm@5175
    31
  val long_names: bool ref
wenzelm@5175
    32
  val cond_extern: T -> string -> string
wenzelm@6845
    33
  val cond_extern_table: T -> 'a Symtab.table -> (string * 'a) list
wenzelm@5012
    34
  val dest: T -> (string * string list) list
wenzelm@5012
    35
end;
wenzelm@5012
    36
wenzelm@5012
    37
structure NameSpace: NAME_SPACE =
wenzelm@5012
    38
struct
wenzelm@5012
    39
wenzelm@5012
    40
wenzelm@5012
    41
(** utils **)
wenzelm@5012
    42
wenzelm@5012
    43
fun prefixes1 [] = []
wenzelm@5012
    44
  | prefixes1 (x :: xs) = map (cons x) ([] :: prefixes1 xs);
wenzelm@5012
    45
wenzelm@5012
    46
fun suffixes1 xs = map rev (prefixes1 (rev xs));
wenzelm@5012
    47
wenzelm@5012
    48
wenzelm@5012
    49
wenzelm@5012
    50
(** long identifiers **)
wenzelm@5012
    51
wenzelm@5012
    52
val separator = ".";
wenzelm@8728
    53
fun hidden name = "??." ^ name;
wenzelm@8728
    54
fun is_hidden name = (case explode name of "?" :: "?" :: "." :: _ => true | _ => false);
wenzelm@5012
    55
wenzelm@5012
    56
fun append name1 name2 = name1 ^ separator ^ name2;
wenzelm@5012
    57
wenzelm@5012
    58
val unpack = space_explode separator;
wenzelm@5012
    59
val pack = space_implode separator;
wenzelm@5012
    60
wenzelm@5012
    61
val base = last_elem o unpack;
wenzelm@11829
    62
wenzelm@11829
    63
fun map_base f name =
wenzelm@11829
    64
  let val uname = unpack name
wenzelm@11829
    65
  in pack (map_nth_elem (length uname - 1) f uname) end;
wenzelm@11829
    66
wenzelm@8728
    67
fun is_qualified name = length (unpack name) > 1;
wenzelm@5012
    68
wenzelm@5012
    69
fun accesses name =
wenzelm@5012
    70
  let
wenzelm@5012
    71
    val uname = unpack name;
wenzelm@5012
    72
    val (q, b) = split_last uname;
wenzelm@5012
    73
    val sfxs = suffixes1 uname;
wenzelm@5012
    74
    val pfxs = map (fn x => x @ [b]) (prefixes1 q);
wenzelm@5012
    75
  in map pack (sfxs @ pfxs) end;
wenzelm@5012
    76
wenzelm@5012
    77
wenzelm@5012
    78
wenzelm@5012
    79
(** name spaces **)
wenzelm@5012
    80
wenzelm@8728
    81
(* basic operations *)
wenzelm@8728
    82
wenzelm@8728
    83
fun map_space f xname tab =
wenzelm@8728
    84
  Symtab.update ((xname, f (if_none (Symtab.lookup (tab, xname)) ([], []))), tab);
wenzelm@8728
    85
wenzelm@8728
    86
fun change f xname (name, tab) =
wenzelm@8728
    87
  map_space (fn (names, names') => (f names name, names')) xname tab;
wenzelm@8728
    88
wenzelm@8728
    89
fun change' f xname (name', tab) =
wenzelm@8728
    90
  map_space (fn (names, names') => (names, f names' name')) xname tab;
wenzelm@8728
    91
wenzelm@8728
    92
fun del names nm = if nm mem_string names then Library.gen_rem (op =) (names, nm) else names;
wenzelm@8728
    93
fun add names nm = nm :: del names nm;
wenzelm@8728
    94
wenzelm@8728
    95
wenzelm@5012
    96
(* datatype T *)
wenzelm@5012
    97
wenzelm@5012
    98
datatype T =
wenzelm@8728
    99
  NameSpace of (string list * string list) Symtab.table;
wenzelm@5012
   100
wenzelm@5012
   101
val empty = NameSpace Symtab.empty;
wenzelm@5012
   102
wenzelm@5012
   103
wenzelm@8728
   104
(* extend *)            (*later entries preferred*)
wenzelm@8728
   105
wenzelm@8728
   106
fun extend_aux (name, tab) =
wenzelm@8728
   107
  if is_hidden name then
wenzelm@8728
   108
    error ("Attempt to declare hidden name " ^ quote name)
wenzelm@8728
   109
  else foldl (fn (tb, xname) => change add xname (name, tb)) (tab, accesses name);
wenzelm@5012
   110
wenzelm@8728
   111
fun extend (NameSpace tab, names) = NameSpace (foldr extend_aux (names, tab));
wenzelm@8728
   112
wenzelm@8728
   113
wenzelm@8728
   114
(* merge *)             (*1st preferred over 2nd*)
wenzelm@8728
   115
wenzelm@8728
   116
fun merge_aux (tab, (xname, (names, names'))) =
wenzelm@8728
   117
  foldr (change add xname) (names, foldr (change' add xname) (names', tab));
wenzelm@5012
   118
wenzelm@8728
   119
fun merge (NameSpace tab1, NameSpace tab2) =
wenzelm@8728
   120
  NameSpace (Symtab.foldl merge_aux (tab2, tab1));
wenzelm@8728
   121
wenzelm@8728
   122
wenzelm@8728
   123
(* hide *)
wenzelm@5012
   124
wenzelm@8728
   125
fun hide_aux (name, tab) =
wenzelm@8728
   126
  if not (is_qualified name) then
wenzelm@8728
   127
    error ("Attempt to hide global name " ^ quote name)
wenzelm@8728
   128
  else if is_hidden name then
wenzelm@8728
   129
    error ("Attempt to hide hidden name " ^ quote name)
wenzelm@8728
   130
  else (change' add name (name,
wenzelm@8728
   131
    foldl (fn (tb, xname) => change del xname (name, tb)) (tab, accesses name)));
wenzelm@8728
   132
wenzelm@8728
   133
fun hide (NameSpace tab, names) = NameSpace (foldr hide_aux (names, tab));
wenzelm@5012
   134
wenzelm@5012
   135
wenzelm@5012
   136
(* intern / extern names *)
wenzelm@5012
   137
wenzelm@8728
   138
fun lookup (NameSpace tab) xname =
wenzelm@8728
   139
  (case Symtab.lookup (tab, xname) of
wenzelm@8728
   140
    None => (xname, true)
wenzelm@8728
   141
  | Some ([name], _) => (name, true)
wenzelm@8728
   142
  | Some (name :: _, _) => (name, false)
wenzelm@8728
   143
  | Some ([], []) => (xname, true)
wenzelm@8728
   144
  | Some ([], name' :: _) => (hidden name', true));
wenzelm@8728
   145
wenzelm@8728
   146
fun intern spc xname = #1 (lookup spc xname);
wenzelm@5012
   147
wenzelm@5012
   148
fun extern space name =
wenzelm@5012
   149
  let
wenzelm@8728
   150
    fun try [] = hidden name
wenzelm@5012
   151
      | try (nm :: nms) =
wenzelm@8728
   152
          let val (full_nm, uniq) = lookup space nm
wenzelm@8728
   153
          in if full_nm = name andalso uniq then nm else try nms end
wenzelm@5012
   154
  in try (map pack (suffixes1 (unpack name))) end;
wenzelm@5012
   155
wenzelm@5175
   156
(*prune names on output by default*)
wenzelm@5175
   157
val long_names = ref false;
wenzelm@5175
   158
wenzelm@5175
   159
fun cond_extern space = if ! long_names then I else extern space;
wenzelm@5175
   160
wenzelm@6845
   161
fun cond_extern_table space tab =
wenzelm@6845
   162
  Library.sort_wrt #1 (map (apfst (cond_extern space)) (Symtab.dest tab));
wenzelm@6845
   163
wenzelm@5012
   164
wenzelm@5012
   165
(* dest *)
wenzelm@5012
   166
wenzelm@8728
   167
fun dest_entry (xname, ([], _)) = None
wenzelm@8728
   168
  | dest_entry (xname, (name :: _, _)) = Some (name, xname);
wenzelm@8728
   169
wenzelm@5012
   170
fun dest (NameSpace tab) =
wenzelm@5012
   171
  map (apsnd (sort (int_ord o pairself size)))
wenzelm@8728
   172
    (Symtab.dest (Symtab.make_multi (mapfilter dest_entry (Symtab.dest tab))));
wenzelm@5012
   173
wenzelm@5012
   174
wenzelm@5012
   175
end;
wenzelm@5175
   176
wenzelm@5175
   177
wenzelm@5175
   178
val long_names = NameSpace.long_names;