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