src/Pure/name_space.ML
author wenzelm
Fri, 10 Oct 1997 15:51:14 +0200
changeset 3833 370e845c391f
parent 3803 3e581526ae5e
child 3876 e6f918979f2d
permissions -rw-r--r--
tuned; more accesses to long name;
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
3786
wenzelm
parents: 3769
diff changeset
     7
More general than ML-like nested structures, but also slightly more
wenzelm
parents: 3769
diff changeset
     8
ad-hoc.  Does not support absolute addressing.  Unknown names are
wenzelm
parents: 3769
diff changeset
     9
implicitely considered to be declared outermost.
3761
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
3833
wenzelm
parents: 3803
diff changeset
    14
  val separator: string         (*single char!*)
3761
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    15
  val unpack: string -> string list
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    16
  val pack: string list -> string
3786
wenzelm
parents: 3769
diff changeset
    17
  val base: string -> string
3761
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    18
  val qualified: string -> bool
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    19
  type T
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    20
  val dest: T -> string list
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    21
  val empty: T
3786
wenzelm
parents: 3769
diff changeset
    22
  val extend: string list * T -> T
3761
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    23
  val merge: T * T -> T
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    24
  val lookup: T -> string -> string
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    25
  val prune: T -> string -> string
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    26
end;
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    27
3833
wenzelm
parents: 3803
diff changeset
    28
structure NameSpace(*: NAME_SPACE FIXME *) =
3761
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    29
struct
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    30
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    31
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    32
(** long identifiers **)
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    33
3833
wenzelm
parents: 3803
diff changeset
    34
val separator = ".";
3761
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    35
3833
wenzelm
parents: 3803
diff changeset
    36
val unpack = space_explode separator;
3761
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    37
val pack = space_implode separator;
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    38
3786
wenzelm
parents: 3769
diff changeset
    39
val base = last_elem o unpack;
3833
wenzelm
parents: 3803
diff changeset
    40
fun qualified name = length (unpack name) > 1;
3761
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    41
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    42
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    43
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    44
(** name spaces **)
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    45
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    46
(* utils *)
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    47
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    48
fun prefixes1 [] = []
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    49
  | prefixes1 (x :: xs) = map (cons x) ([] :: prefixes1 xs);
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    50
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    51
fun suffixes1 xs = map rev (prefixes1 (rev xs));
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
(* datatype T *)
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    55
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    56
datatype T =
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    57
  NameSpace of string list list * string Symtab.table;
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    58
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    59
fun entries_of (NameSpace (entries, _)) = entries;
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    60
fun tab_of (NameSpace (_, tab)) = tab;
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    61
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    62
fun make entries =
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    63
  let
3833
wenzelm
parents: 3803
diff changeset
    64
    fun accesses [] = []		(* FIXME !? *)
wenzelm
parents: 3803
diff changeset
    65
      | accesses entry =
wenzelm
parents: 3803
diff changeset
    66
          let
wenzelm
parents: 3803
diff changeset
    67
            val p = pack entry;
wenzelm
parents: 3803
diff changeset
    68
            val (q, b) = split_last entry;
wenzelm
parents: 3803
diff changeset
    69
            val sfxs = suffixes1 entry;
wenzelm
parents: 3803
diff changeset
    70
            val pfxs = map (fn x => x @ [b]) (prefixes1 q);
wenzelm
parents: 3803
diff changeset
    71
          in
wenzelm
parents: 3803
diff changeset
    72
            map (rpair p o pack) (sfxs @ pfxs)
wenzelm
parents: 3803
diff changeset
    73
          end;
3803
3e581526ae5e tuned internal mapping table;
wenzelm
parents: 3786
diff changeset
    74
    val mapping = filter_out (op =)
3e581526ae5e tuned internal mapping table;
wenzelm
parents: 3786
diff changeset
    75
      (gen_distinct eq_fst (flat (map accesses entries)));
3761
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    76
  in
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    77
    NameSpace (entries, Symtab.make mapping)
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    78
  end;
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    79
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    80
fun dest space = rev (map pack (entries_of space));
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    81
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    82
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    83
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    84
(* empty, extend, merge operations *)
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    85
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    86
val empty = make [];
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    87
3786
wenzelm
parents: 3769
diff changeset
    88
fun extend (entries, space) =
wenzelm
parents: 3769
diff changeset
    89
  make (map unpack (rev entries) @ entries_of space);
3761
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    90
3803
3e581526ae5e tuned internal mapping table;
wenzelm
parents: 3786
diff changeset
    91
fun merge (space1, space2) =    (*2nd overrides 1st*)
3e581526ae5e tuned internal mapping table;
wenzelm
parents: 3786
diff changeset
    92
  make (merge_lists (entries_of space2) (entries_of space1));
3761
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    93
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    94
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    95
(* lookup / prune names *)
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    96
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    97
fun lookup space name =
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    98
  if_none (Symtab.lookup (tab_of space, name)) name;
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    99
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
   100
fun prune space name =
3786
wenzelm
parents: 3769
diff changeset
   101
  if not (qualified name) then name
wenzelm
parents: 3769
diff changeset
   102
  else
wenzelm
parents: 3769
diff changeset
   103
    let
3833
wenzelm
parents: 3803
diff changeset
   104
      fun try [] = "??" ^ separator ^ name      (*hidden name*)
3786
wenzelm
parents: 3769
diff changeset
   105
        | try (nm :: nms) =
wenzelm
parents: 3769
diff changeset
   106
            if lookup space nm = name then nm
wenzelm
parents: 3769
diff changeset
   107
            else try nms;
wenzelm
parents: 3769
diff changeset
   108
    in try (map pack (suffixes1 (unpack name))) end;
3761
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
   109
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
   110
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
   111
end;