src/Pure/name_space.ML
author wenzelm
Wed, 01 Oct 1997 14:30:38 +0200
changeset 3761 d99d93d46ca5
child 3769 931c336b0707
permissions -rw-r--r--
Hierarchically structured name spaces.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3761
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/name_space.ML
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
     2
    ID:         $Id$
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
     3
    Author:     Markus Wenzel, TU Muenchen
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
     4
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
     5
Hierarchically structured name spaces.
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
     6
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
     7
More general than ML-like nested structures, but slightly more ad-hoc.
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
     8
Does not support absolute addressing.  Unknown short (FIXME?) names
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
     9
are implicitely considered to be declared outermost.
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    10
*)
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    11
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    12
signature NAME_SPACE =
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    13
sig
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    14
  val unpack: string -> string list
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    15
  val pack: string list -> string
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    16
  val qualified: string -> bool
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    17
  type T
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    18
  val dest: T -> string list
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    19
  val empty: T
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    20
  val extend: string list -> string list * T -> T
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    21
  val merge: T * T -> T
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    22
  val lookup: T -> string -> string
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    23
  val prune: T -> string -> string
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    24
end;
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    25
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    26
structure NameSpace: NAME_SPACE =
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    27
struct
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    28
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    29
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    30
(** long identifiers **)
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    31
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    32
val separator = "'";
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    33
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    34
fun unpack_aux name =
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    35
  let
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    36
    (*handle trailing separators gracefully*)
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    37
    val (chars, seps) = take_suffix (equal separator) name;
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    38
    fun expl chs =
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    39
      (case take_prefix (not_equal separator) chs of
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    40
        (cs, []) => [implode (cs @ seps)]
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    41
      | (cs, _ :: cs') => implode cs :: expl cs');
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    42
  in expl chars end;
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    43
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    44
val unpack = unpack_aux o explode;
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    45
val pack = space_implode separator;
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    46
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    47
fun qualified name =
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    48
  let val chs = explode name in
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    49
    exists (equal separator) chs andalso (length (unpack_aux chs) > 1)
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    50
  end;
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    51
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    52
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    53
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    54
(** name spaces **)
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    55
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    56
(* utils *)
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    57
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    58
fun prefixes1 [] = []
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    59
  | prefixes1 (x :: xs) = map (cons x) ([] :: prefixes1 xs);
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    60
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    61
fun suffixes1 xs = map rev (prefixes1 (rev xs));
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    62
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    63
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    64
(* datatype T *)
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    65
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    66
datatype T =
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    67
  NameSpace of string list list * string Symtab.table;
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    68
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    69
fun entries_of (NameSpace (entries, _)) = entries;
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    70
fun tab_of (NameSpace (_, tab)) = tab;
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    71
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    72
fun make entries =
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    73
  let
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    74
    fun accesses entry =
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    75
      let val packed = pack entry in
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    76
        map (rpair packed o pack) (suffixes1 entry)
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    77
      end;
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    78
    val mapping = gen_distinct eq_fst (flat (map accesses entries));
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    79
  in
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    80
    NameSpace (entries, Symtab.make mapping)
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    81
  end;
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    82
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    83
fun dest space = rev (map pack (entries_of space));
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    84
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    85
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    86
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    87
(* empty, extend, merge operations *)
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    88
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    89
val empty = make [];
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    90
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    91
fun extend path (entries, space) =
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    92
  make (map (fn e => path @ unpack e) (rev entries) @ entries_of space);
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    93
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    94
fun merge (space1, space2) =
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    95
  make (merge_lists (entries_of space1) (entries_of space2));
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    96
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    97
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    98
(* lookup / prune names *)
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    99
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
   100
(* FIXME improve handling of undeclared qualified names (?) *)
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
   101
fun lookup space name =
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
   102
  if_none (Symtab.lookup (tab_of space, name)) name;
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
   103
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
   104
fun prune space name =
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
   105
  let
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
   106
    fun try [] = name
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
   107
      | try (nm :: nms) =
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
   108
          if lookup space nm = name then nm
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
   109
          else try nms;
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
   110
  in try (map pack (suffixes1 (unpack name))) end;
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
   111
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
   112
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
   113
end;