src/Pure/symtab.ML
author wenzelm
Wed Jan 19 14:12:40 1994 +0100 (1994-01-19)
changeset 234 1b3bee8d5d7e
parent 0 a5a9c433f639
child 374 caf9a9b7f605
permissions -rw-r--r--
minor cleanup;
added extend, merge;
added {lookup,make,dest}_multi;
wenzelm@234
     1
(*  Title:      Pure/symtab.ML
clasohm@0
     2
    ID:         $Id$
wenzelm@234
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
clasohm@0
     4
    Copyright   1989  University of Cambridge
clasohm@0
     5
wenzelm@234
     6
Unbalanced binary trees indexed by strings; no way to delete an entry.
clasohm@0
     7
*)
clasohm@0
     8
wenzelm@234
     9
signature SYMTAB =
clasohm@0
    10
sig
wenzelm@234
    11
  exception DUPLICATE of string
wenzelm@234
    12
  type 'a table
wenzelm@234
    13
  val null: 'a table
wenzelm@234
    14
  val is_null: 'a table -> bool
wenzelm@234
    15
  val lookup: 'a table * string -> 'a option
wenzelm@234
    16
  val update: (string * 'a) * 'a table -> 'a table
wenzelm@234
    17
  val update_new: (string * 'a) * 'a table -> 'a table
wenzelm@234
    18
  val alist_of: 'a table -> (string * 'a) list
wenzelm@234
    19
  val balance: 'a table -> 'a table
wenzelm@234
    20
  val st_of_alist: (string * 'a) list * 'a table -> 'a table
wenzelm@234
    21
  val st_of_declist: (string list * 'a) list * 'a table -> 'a table
wenzelm@234
    22
  val extend: ('a * 'a -> bool) -> 'a table * (string * 'a) list -> 'a table
wenzelm@234
    23
  val merge: ('a * 'a -> bool) -> 'a table * 'a table -> 'a table
wenzelm@234
    24
  val lookup_multi: 'a list table * string -> 'a list
wenzelm@234
    25
  val make_multi: (string * 'a) list -> 'a list table
wenzelm@234
    26
  val dest_multi: 'a list table -> (string * 'a) list
clasohm@0
    27
end;
clasohm@0
    28
wenzelm@234
    29
functor SymtabFun(): SYMTAB =
clasohm@0
    30
struct
clasohm@0
    31
clasohm@0
    32
(*symbol table errors, such as from update_new*)
clasohm@0
    33
exception DUPLICATE of string;
clasohm@0
    34
wenzelm@234
    35
datatype 'a table = Tip | Branch of (string * 'a * 'a table * 'a table);
clasohm@0
    36
clasohm@0
    37
clasohm@0
    38
val null = Tip;
clasohm@0
    39
clasohm@0
    40
fun is_null Tip = true
clasohm@0
    41
  | is_null _ = false;
clasohm@0
    42
clasohm@0
    43
wenzelm@234
    44
fun lookup (symtab: 'a table, key: string) : 'a option =
clasohm@0
    45
  let fun look  Tip  = None
wenzelm@234
    46
        | look (Branch (key', entry, left, right)) =
wenzelm@234
    47
            if      key < key' then look left
wenzelm@234
    48
            else if key' < key then look right
wenzelm@234
    49
            else  Some entry
clasohm@0
    50
  in look symtab end;
clasohm@0
    51
clasohm@0
    52
(*update, allows overwriting of an entry*)
clasohm@0
    53
fun update ((key: string, entry: 'a), symtab : 'a table)
clasohm@0
    54
  : 'a table =
wenzelm@234
    55
  let fun upd  Tip  = Branch (key, entry, Tip, Tip)
wenzelm@234
    56
        | upd (Branch(key', entry', left, right)) =
wenzelm@234
    57
            if      key < key' then Branch (key', entry', upd left, right)
wenzelm@234
    58
            else if key' < key then Branch (key', entry', left, upd right)
wenzelm@234
    59
            else                    Branch (key, entry, left, right)
clasohm@0
    60
  in  upd symtab  end;
clasohm@0
    61
clasohm@0
    62
(*Like update but fails if key is already defined in table.
clasohm@0
    63
  Allows st_of_alist, etc. to detect multiple definitions*)
clasohm@0
    64
fun update_new ((key: string, entry: 'a), symtab : 'a table)
clasohm@0
    65
  : 'a table =
wenzelm@234
    66
  let fun upd Tip = Branch (key, entry, Tip, Tip)
wenzelm@234
    67
        | upd (Branch(key', entry', left, right)) =
wenzelm@234
    68
            if      key < key' then Branch (key', entry', upd left, right)
wenzelm@234
    69
            else if key' < key then Branch (key', entry', left, upd right)
wenzelm@234
    70
            else  raise DUPLICATE(key)
clasohm@0
    71
  in  upd symtab  end;
clasohm@0
    72
clasohm@0
    73
(*conversion of symbol table to sorted association list*)
clasohm@0
    74
fun alist_of (symtab : 'a table) : (string * 'a) list =
wenzelm@234
    75
  let fun ali (symtab, cont) = case symtab of
wenzelm@234
    76
                Tip => cont
wenzelm@234
    77
        | Branch (key, entry, left, right) =>
wenzelm@234
    78
            ali(left, (key, entry) :: ali(right, cont))
wenzelm@234
    79
  in  ali (symtab, [])  end;
clasohm@0
    80
clasohm@0
    81
clasohm@0
    82
(*Make a balanced tree of the first n members of the sorted alist (sal).
clasohm@0
    83
  Utility for the function balance.*)
clasohm@0
    84
fun bal_of (sal, 0) = Tip
clasohm@0
    85
  | bal_of (sal, n) =
clasohm@0
    86
      let val mid = n div 2
wenzelm@234
    87
      in  case  drop (mid, sal) of
wenzelm@234
    88
            [] => bal_of (sal, mid)   (*should not occur*)
wenzelm@234
    89
          | ((key, entry):: pairs) =>
wenzelm@234
    90
                Branch(key, entry, bal_of(sal, mid), bal_of(pairs, n-mid-1))
clasohm@0
    91
      end;
clasohm@0
    92
clasohm@0
    93
clasohm@0
    94
fun balance symtab =
clasohm@0
    95
  let val sal = alist_of symtab
clasohm@0
    96
  in  bal_of (sal, length sal)  end;
clasohm@0
    97
clasohm@0
    98
clasohm@0
    99
(*Addition of association list to a symbol table*)
clasohm@0
   100
fun st_of_alist (al, symtab) =
wenzelm@234
   101
  foldr update_new (al, symtab);
clasohm@0
   102
clasohm@0
   103
(*A "declaration" associates the same entry with a list of keys;
clasohm@0
   104
  does not allow overwriting of an entry*)
clasohm@0
   105
fun decl_update_new ((keys : string list, entry: 'a), symtab)
clasohm@0
   106
  : 'a table =
wenzelm@234
   107
  let fun decl (key, symtab) = update_new ((key, entry), symtab)
wenzelm@234
   108
  in foldr decl (keys, symtab) end;
clasohm@0
   109
clasohm@0
   110
(*Addition of a list of declarations to a symbol table*)
clasohm@0
   111
fun st_of_declist (dl, symtab) =
wenzelm@234
   112
  balance (foldr decl_update_new (dl, symtab));
wenzelm@234
   113
wenzelm@234
   114
wenzelm@234
   115
(* extend, merge tables *)
wenzelm@234
   116
wenzelm@234
   117
fun eq_pair eq ((key1, entry1), (key2, entry2)) =
wenzelm@234
   118
  key1 = key2 andalso eq (entry1, entry2);
wenzelm@234
   119
wenzelm@234
   120
fun mk_tab alst = balance (st_of_alist (alst, Tip));
wenzelm@234
   121
wenzelm@234
   122
fun extend eq (tab, alst) =
wenzelm@234
   123
  generic_extend (eq_pair eq) alist_of mk_tab tab alst;
wenzelm@234
   124
wenzelm@234
   125
fun merge eq (tab1, tab2) =
wenzelm@234
   126
  generic_merge (eq_pair eq) alist_of mk_tab tab1 tab2;
wenzelm@234
   127
wenzelm@234
   128
wenzelm@234
   129
(* tables with multiple entries per key *)
wenzelm@234
   130
(*order of entries with same key is preserved*)
wenzelm@234
   131
wenzelm@234
   132
fun lookup_multi (tab, key) =
wenzelm@234
   133
  (case lookup (tab, key) of
wenzelm@234
   134
    Some entries => entries
wenzelm@234
   135
  | None => []);
wenzelm@234
   136
wenzelm@234
   137
fun cons_entry ((key, entry), tab) =
wenzelm@234
   138
  update ((key, entry :: lookup_multi (tab, key)), tab);
wenzelm@234
   139
wenzelm@234
   140
fun make_multi alst =
wenzelm@234
   141
  balance (foldr cons_entry (alst, Tip));
wenzelm@234
   142
wenzelm@234
   143
fun dest_multi tab =
wenzelm@234
   144
  flat (map (fn (key, entries) => map (pair key) entries) (alist_of tab));
wenzelm@234
   145
clasohm@0
   146
clasohm@0
   147
end;
clasohm@0
   148