src/Pure/name_space.ML
author wenzelm
Mon, 06 Oct 1997 18:41:39 +0200
changeset 3786 d5655489867c
parent 3769 931c336b0707
child 3803 3e581526ae5e
permissions -rw-r--r--
tuned;
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.
wenzelm
parents: 3769
diff changeset
    10
wenzelm
parents: 3769
diff changeset
    11
TODO:
wenzelm
parents: 3769
diff changeset
    12
  - absolute paths?
3761
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    13
*)
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    14
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    15
signature NAME_SPACE =
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    16
sig
3769
931c336b0707 exported separator;
wenzelm
parents: 3761
diff changeset
    17
  val separator: string		(*single char!*)
3761
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    18
  val unpack: string -> string list
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    19
  val pack: string list -> string
3786
wenzelm
parents: 3769
diff changeset
    20
  val base: string -> string
3761
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    21
  val qualified: string -> bool
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    22
  type T
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    23
  val dest: T -> string list
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    24
  val empty: T
3786
wenzelm
parents: 3769
diff changeset
    25
  val extend: string list * T -> T
3761
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    26
  val merge: T * T -> T
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    27
  val lookup: T -> string -> string
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    28
  val prune: T -> string -> string
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    29
end;
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    30
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    31
structure NameSpace: NAME_SPACE =
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    32
struct
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    33
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    34
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    35
(** long identifiers **)
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    36
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    37
val separator = "'";
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    38
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    39
fun unpack_aux name =
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    40
  let
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    41
    (*handle trailing separators gracefully*)
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    42
    val (chars, seps) = take_suffix (equal separator) name;
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    43
    fun expl chs =
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    44
      (case take_prefix (not_equal separator) chs of
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    45
        (cs, []) => [implode (cs @ seps)]
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    46
      | (cs, _ :: cs') => implode cs :: expl cs');
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    47
  in expl chars end;
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    48
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    49
val unpack = unpack_aux o explode;
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    50
val pack = space_implode separator;
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    51
3786
wenzelm
parents: 3769
diff changeset
    52
val base = last_elem o unpack;
wenzelm
parents: 3769
diff changeset
    53
3761
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    54
fun qualified name =
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    55
  let val chs = explode name in
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    56
    exists (equal separator) chs andalso (length (unpack_aux chs) > 1)
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    57
  end;
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    58
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    59
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    60
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    61
(** name spaces **)
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    62
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    63
(* utils *)
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    64
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    65
fun prefixes1 [] = []
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    66
  | prefixes1 (x :: xs) = map (cons x) ([] :: prefixes1 xs);
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    67
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    68
fun suffixes1 xs = map rev (prefixes1 (rev xs));
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    69
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    70
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    71
(* datatype T *)
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    72
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    73
datatype T =
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    74
  NameSpace of string list list * string Symtab.table;
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    75
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    76
fun entries_of (NameSpace (entries, _)) = entries;
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    77
fun tab_of (NameSpace (_, tab)) = tab;
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    78
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    79
fun make entries =
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    80
  let
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    81
    fun accesses entry =
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    82
      let val packed = pack entry in
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    83
        map (rpair packed o pack) (suffixes1 entry)
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    84
      end;
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    85
    val mapping = gen_distinct eq_fst (flat (map accesses entries));
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    86
  in
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    87
    NameSpace (entries, Symtab.make mapping)
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    88
  end;
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    89
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    90
fun dest space = rev (map pack (entries_of space));
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    91
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    92
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    93
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    94
(* empty, extend, merge operations *)
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    95
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    96
val empty = make [];
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
    97
3786
wenzelm
parents: 3769
diff changeset
    98
fun extend (entries, space) =
wenzelm
parents: 3769
diff changeset
    99
  make (map unpack (rev entries) @ entries_of space);
3761
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
   100
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
   101
fun merge (space1, space2) =
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
   102
  make (merge_lists (entries_of space1) (entries_of space2));
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
   103
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
   104
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
   105
(* lookup / prune names *)
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
   106
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
   107
fun lookup space name =
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
   108
  if_none (Symtab.lookup (tab_of space, name)) name;
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
   109
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
   110
fun prune space name =
3786
wenzelm
parents: 3769
diff changeset
   111
  if not (qualified name) then name
wenzelm
parents: 3769
diff changeset
   112
  else
wenzelm
parents: 3769
diff changeset
   113
    let
wenzelm
parents: 3769
diff changeset
   114
      fun try [] = name
wenzelm
parents: 3769
diff changeset
   115
        | try (nm :: nms) =
wenzelm
parents: 3769
diff changeset
   116
            if lookup space nm = name then nm
wenzelm
parents: 3769
diff changeset
   117
            else try nms;
wenzelm
parents: 3769
diff changeset
   118
    in try (map pack (suffixes1 (unpack name))) end;
3761
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
   119
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
   120
d99d93d46ca5 Hierarchically structured name spaces.
wenzelm
parents:
diff changeset
   121
end;