src/Pure/symtab.ML
author clasohm
Thu Sep 16 12:20:38 1993 +0200 (1993-09-16)
changeset 0 a5a9c433f639
child 234 1b3bee8d5d7e
permissions -rw-r--r--
Initial revision
clasohm@0
     1
(*  Title: 	symtab
clasohm@0
     2
    ID:         $Id$
clasohm@0
     3
    Author: 	Lawrence C Paulson, Cambridge University Computer Laboratory
clasohm@0
     4
    Copyright   1989  University of Cambridge
clasohm@0
     5
*)
clasohm@0
     6
clasohm@0
     7
(*Unbalanced binary trees indexed by strings
clasohm@0
     8
  No way to delete an entry
clasohm@0
     9
  could generalize alist_of to a traversal functional
clasohm@0
    10
*)
clasohm@0
    11
clasohm@0
    12
signature SYMTAB = 
clasohm@0
    13
sig
clasohm@0
    14
   type 'a table
clasohm@0
    15
   val alist_of : 'a table -> (string*'a) list
clasohm@0
    16
   val balance : 'a table -> 'a table
clasohm@0
    17
   val lookup : 'a table * string -> 'a option
clasohm@0
    18
   val null : 'a table
clasohm@0
    19
   val is_null : 'a table -> bool
clasohm@0
    20
   val st_of_alist : (string*'a)list * 'a table -> 'a table
clasohm@0
    21
   val st_of_declist : (string list * 'a)list * 'a table -> 'a table
clasohm@0
    22
   val update : (string*'a) * 'a table -> 'a table
clasohm@0
    23
   val update_new : (string*'a) * 'a table -> 'a table
clasohm@0
    24
   exception DUPLICATE of string
clasohm@0
    25
end;
clasohm@0
    26
clasohm@0
    27
clasohm@0
    28
functor SymtabFun () : SYMTAB = 
clasohm@0
    29
struct
clasohm@0
    30
clasohm@0
    31
(*symbol table errors, such as from update_new*)
clasohm@0
    32
exception DUPLICATE of string;
clasohm@0
    33
clasohm@0
    34
datatype 'a table = Tip  |  Branch of (string * 'a * 'a table * 'a table);
clasohm@0
    35
clasohm@0
    36
clasohm@0
    37
val null = Tip;
clasohm@0
    38
clasohm@0
    39
fun is_null Tip = true
clasohm@0
    40
  | is_null _ = false;
clasohm@0
    41
clasohm@0
    42
clasohm@0
    43
fun lookup (symtab: 'a table, key: string) : 'a option = 
clasohm@0
    44
  let fun look  Tip  = None
clasohm@0
    45
	| look (Branch (key',entry,left,right)) =
clasohm@0
    46
	    if      key < key' then look left
clasohm@0
    47
	    else if key' < key then look right
clasohm@0
    48
	    else  Some entry
clasohm@0
    49
  in look symtab end;
clasohm@0
    50
clasohm@0
    51
(*update, allows overwriting of an entry*)
clasohm@0
    52
fun update ((key: string, entry: 'a), symtab : 'a table)
clasohm@0
    53
  : 'a table =
clasohm@0
    54
  let fun upd  Tip  = Branch (key,entry,Tip,Tip)
clasohm@0
    55
	| upd (Branch(key',entry',left,right)) =
clasohm@0
    56
	    if      key < key' then Branch (key',entry', upd left, right)
clasohm@0
    57
	    else if key' < key then Branch (key',entry',left, upd right)
clasohm@0
    58
	    else                    Branch (key,entry,left,right)
clasohm@0
    59
  in  upd symtab  end;
clasohm@0
    60
clasohm@0
    61
(*Like update but fails if key is already defined in table.
clasohm@0
    62
  Allows st_of_alist, etc. to detect multiple definitions*)
clasohm@0
    63
fun update_new ((key: string, entry: 'a), symtab : 'a table)
clasohm@0
    64
  : 'a table =
clasohm@0
    65
  let fun upd Tip = Branch (key,entry,Tip,Tip)
clasohm@0
    66
	| upd (Branch(key',entry',left,right)) =
clasohm@0
    67
	    if      key < key' then Branch (key',entry', upd left, right)
clasohm@0
    68
	    else if key' < key then Branch (key',entry',left, upd right)
clasohm@0
    69
	    else  raise DUPLICATE(key)
clasohm@0
    70
  in  upd symtab  end;
clasohm@0
    71
clasohm@0
    72
(*conversion of symbol table to sorted association list*)
clasohm@0
    73
fun alist_of (symtab : 'a table) : (string * 'a) list =
clasohm@0
    74
  let fun ali (symtab,cont) = case symtab of
clasohm@0
    75
		Tip => cont
clasohm@0
    76
	| Branch (key,entry,left,right) =>
clasohm@0
    77
	    ali(left, (key,entry) :: ali(right,cont))
clasohm@0
    78
  in  ali (symtab,[])  end;
clasohm@0
    79
clasohm@0
    80
clasohm@0
    81
(*Make a balanced tree of the first n members of the sorted alist (sal).
clasohm@0
    82
  Utility for the function balance.*)
clasohm@0
    83
fun bal_of (sal, 0) = Tip
clasohm@0
    84
  | bal_of (sal, n) =
clasohm@0
    85
      let val mid = n div 2
clasohm@0
    86
      in  case  drop (mid,sal) of
clasohm@0
    87
	    [] => bal_of (sal, mid)   (*should not occur*)
clasohm@0
    88
	  | ((key,entry):: pairs) =>
clasohm@0
    89
		Branch(key,entry, bal_of(sal,mid), bal_of(pairs, n-mid-1))
clasohm@0
    90
      end;
clasohm@0
    91
clasohm@0
    92
clasohm@0
    93
fun balance symtab =
clasohm@0
    94
  let val sal = alist_of symtab
clasohm@0
    95
  in  bal_of (sal, length sal)  end;
clasohm@0
    96
clasohm@0
    97
clasohm@0
    98
(*Addition of association list to a symbol table*)
clasohm@0
    99
fun st_of_alist (al, symtab) =
clasohm@0
   100
    foldr update_new (al, symtab);
clasohm@0
   101
clasohm@0
   102
(*A "declaration" associates the same entry with a list of keys;
clasohm@0
   103
  does not allow overwriting of an entry*)
clasohm@0
   104
fun decl_update_new ((keys : string list, entry: 'a), symtab)
clasohm@0
   105
  : 'a table =
clasohm@0
   106
  let fun decl (key,symtab) = update_new((key,entry), symtab)
clasohm@0
   107
  in  foldr decl (keys, symtab)  end;
clasohm@0
   108
clasohm@0
   109
(*Addition of a list of declarations to a symbol table*)
clasohm@0
   110
fun st_of_declist (dl, symtab) =
clasohm@0
   111
    balance (foldr decl_update_new (dl, symtab))
clasohm@0
   112
clasohm@0
   113
end;
clasohm@0
   114