| author | wenzelm | 
| Wed, 19 May 2021 15:53:55 +0200 | |
| changeset 73746 | b2d47981c8dc | 
| parent 70586 | 57df8a85317a | 
| child 74227 | fdcc7e8f95ea | 
| permissions | -rw-r--r-- | 
| 6118 | 1 | (* Title: Pure/General/table.ML | 
| 15014 | 2 | Author: Markus Wenzel and Stefan Berghofer, TU Muenchen | 
| 5015 | 3 | |
| 16342 | 4 | Generic tables. Efficient purely functional implementation using | 
| 5 | balanced 2-3 trees. | |
| 5015 | 6 | *) | 
| 7 | ||
| 8 | signature KEY = | |
| 9 | sig | |
| 10 | type key | |
| 70586 | 11 | val ord: key ord | 
| 5015 | 12 | end; | 
| 13 | ||
| 14 | signature TABLE = | |
| 15 | sig | |
| 16 | type key | |
| 17 | type 'a table | |
| 18 | exception DUP of key | |
| 19031 | 19 | exception SAME | 
| 15014 | 20 | exception UNDEF of key | 
| 5015 | 21 | val empty: 'a table | 
| 22 | val is_empty: 'a table -> bool | |
| 67537 | 23 | val is_single: 'a table -> bool | 
| 39020 | 24 | val map: (key -> 'a -> 'b) -> 'a table -> 'b table | 
| 16446 | 25 | val fold: (key * 'b -> 'a -> 'a) -> 'b table -> 'a -> 'a | 
| 28334 | 26 | val fold_rev: (key * 'b -> 'a -> 'a) -> 'b table -> 'a -> 'a | 
| 5015 | 27 | val dest: 'a table -> (key * 'a) list | 
| 5681 | 28 | val keys: 'a table -> key list | 
| 52049 | 29 | val min: 'a table -> (key * 'a) option | 
| 30 | val max: 'a table -> (key * 'a) option | |
| 35012 
c3e3ac3ca091
removed unused "boundary" of Table/Graph.get_first;
 wenzelm parents: 
33162diff
changeset | 31 | val get_first: (key * 'a -> 'b option) -> 'a table -> 'b option | 
| 27508 | 32 | val exists: (key * 'a -> bool) -> 'a table -> bool | 
| 33 | val forall: (key * 'a -> bool) -> 'a table -> bool | |
| 43792 
d5803c3d537a
Table.lookup_key and Graph.get_entry allow to retrieve the original key, which is not necessarily identical to the given one;
 wenzelm parents: 
39020diff
changeset | 34 | val lookup_key: 'a table -> key -> (key * 'a) option | 
| 31616 | 35 | val lookup: 'a table -> key -> 'a option | 
| 16887 | 36 | val defined: 'a table -> key -> bool | 
| 31209 | 37 | val update: key * 'a -> 'a table -> 'a table | 
| 38 | val update_new: key * 'a -> 'a table -> 'a table (*exception DUP*) | |
| 17179 | 39 | val default: key * 'a -> 'a table -> 'a table | 
| 56051 | 40 |   val map_entry: key -> ('a -> 'a) (*exception SAME*) -> 'a table -> 'a table
 | 
| 31209 | 41 |   val map_default: key * 'a -> ('a -> 'a) -> 'a table -> 'a table
 | 
| 23655 
d2d1138e0ddc
replaced exception TableFun/GraphFun.DUPS by TableFun/GraphFun.DUP;
 wenzelm parents: 
21065diff
changeset | 42 | val make: (key * 'a) list -> 'a table (*exception DUP*) | 
| 56051 | 43 | val join: (key -> 'a * 'a -> 'a) (*exception SAME*) -> | 
| 23655 
d2d1138e0ddc
replaced exception TableFun/GraphFun.DUPS by TableFun/GraphFun.DUP;
 wenzelm parents: 
21065diff
changeset | 44 | 'a table * 'a table -> 'a table (*exception DUP*) | 
| 
d2d1138e0ddc
replaced exception TableFun/GraphFun.DUPS by TableFun/GraphFun.DUP;
 wenzelm parents: 
21065diff
changeset | 45 |   val merge: ('a * 'a -> bool) -> 'a table * 'a table -> 'a table      (*exception DUP*)
 | 
| 15665 | 46 | val delete: key -> 'a table -> 'a table (*exception UNDEF*) | 
| 15761 | 47 | val delete_safe: key -> 'a table -> 'a table | 
| 16466 | 48 |   val member: ('b * 'a -> bool) -> 'a table -> key * 'b -> bool
 | 
| 15761 | 49 |   val insert: ('a * 'a -> bool) -> key * 'a -> 'a table -> 'a table    (*exception DUP*)
 | 
| 16139 | 50 |   val remove: ('b * 'a -> bool) -> key * 'b -> 'a table -> 'a table
 | 
| 18946 | 51 | val lookup_list: 'a list table -> key -> 'a list | 
| 31209 | 52 | val cons_list: key * 'a -> 'a list table -> 'a list table | 
| 19506 | 53 |   val insert_list: ('a * 'a -> bool) -> key * 'a -> 'a list table -> 'a list table
 | 
| 18946 | 54 |   val remove_list: ('b * 'a -> bool) -> key * 'b -> 'a list table -> 'a list table
 | 
| 25391 | 55 |   val update_list: ('a * 'a -> bool) -> key * 'a -> 'a list table -> 'a list table
 | 
| 18946 | 56 | val make_list: (key * 'a) list -> 'a list table | 
| 57 | val dest_list: 'a list table -> (key * 'a) list | |
| 33162 | 58 |   val merge_list: ('a * 'a -> bool) -> 'a list table * 'a list table -> 'a list table
 | 
| 50234 | 59 | type set = unit table | 
| 63511 | 60 | val insert_set: key -> set -> set | 
| 67529 | 61 | val remove_set: key -> set -> set | 
| 59012 
f4e9bd04e1d5
clarified Table.make_set: duplicate arguments are allowed, like Table.make_list or Scala Set() formation;
 wenzelm parents: 
56051diff
changeset | 62 | val make_set: key list -> set | 
| 5015 | 63 | end; | 
| 64 | ||
| 31971 
8c1b845ed105
renamed functor TableFun to Table, and GraphFun to Graph;
 wenzelm parents: 
31621diff
changeset | 65 | functor Table(Key: KEY): TABLE = | 
| 5015 | 66 | struct | 
| 67 | ||
| 68 | ||
| 69 | (* datatype table *) | |
| 70 | ||
| 71 | type key = Key.key; | |
| 72 | ||
| 73 | datatype 'a table = | |
| 74 | Empty | | |
| 75 | Branch2 of 'a table * (key * 'a) * 'a table | | |
| 76 | Branch3 of 'a table * (key * 'a) * 'a table * (key * 'a) * 'a table; | |
| 77 | ||
| 78 | exception DUP of key; | |
| 79 | ||
| 80 | ||
| 67537 | 81 | (* empty and single *) | 
| 5681 | 82 | |
| 5015 | 83 | val empty = Empty; | 
| 84 | ||
| 85 | fun is_empty Empty = true | |
| 86 | | is_empty _ = false; | |
| 87 | ||
| 67537 | 88 | fun is_single (Branch2 (Empty, _, Empty)) = true | 
| 89 | | is_single _ = false; | |
| 90 | ||
| 5681 | 91 | |
| 92 | (* map and fold combinators *) | |
| 93 | ||
| 16657 | 94 | fun map_table f = | 
| 95 | let | |
| 96 | fun map Empty = Empty | |
| 97 | | map (Branch2 (left, (k, x), right)) = | |
| 98 | Branch2 (map left, (k, f k x), map right) | |
| 99 | | map (Branch3 (left, (k1, x1), mid, (k2, x2), right)) = | |
| 100 | Branch3 (map left, (k1, f k1 x1), map mid, (k2, f k2 x2), map right); | |
| 101 | in map end; | |
| 5681 | 102 | |
| 16657 | 103 | fun fold_table f = | 
| 104 | let | |
| 105 | fun fold Empty x = x | |
| 106 | | fold (Branch2 (left, p, right)) x = | |
| 107 | fold right (f p (fold left x)) | |
| 108 | | fold (Branch3 (left, p1, mid, p2, right)) x = | |
| 109 | fold right (f p2 (fold mid (f p1 (fold left x)))); | |
| 110 | in fold end; | |
| 5681 | 111 | |
| 28334 | 112 | fun fold_rev_table f = | 
| 113 | let | |
| 114 | fun fold Empty x = x | |
| 115 | | fold (Branch2 (left, p, right)) x = | |
| 116 | fold left (f p (fold right x)) | |
| 117 | | fold (Branch3 (left, p1, mid, p2, right)) x = | |
| 118 | fold left (f p1 (fold mid (f p2 (fold right x)))); | |
| 119 | in fold end; | |
| 120 | ||
| 121 | fun dest tab = fold_rev_table cons tab []; | |
| 122 | fun keys tab = fold_rev_table (cons o #1) tab []; | |
| 16192 | 123 | |
| 27508 | 124 | |
| 52049 | 125 | (* min/max entries *) | 
| 5015 | 126 | |
| 52049 | 127 | fun min Empty = NONE | 
| 128 | | min (Branch2 (Empty, p, _)) = SOME p | |
| 129 | | min (Branch3 (Empty, p, _, _, _)) = SOME p | |
| 130 | | min (Branch2 (left, _, _)) = min left | |
| 131 | | min (Branch3 (left, _, _, _, _)) = min left; | |
| 8409 | 132 | |
| 52049 | 133 | fun max Empty = NONE | 
| 134 | | max (Branch2 (_, p, Empty)) = SOME p | |
| 135 | | max (Branch3 (_, _, _, p, Empty)) = SOME p | |
| 136 | | max (Branch2 (_, _, right)) = max right | |
| 137 | | max (Branch3 (_, _, _, _, right)) = max right; | |
| 15665 | 138 | |
| 5015 | 139 | |
| 31616 | 140 | (* get_first *) | 
| 141 | ||
| 35012 
c3e3ac3ca091
removed unused "boundary" of Table/Graph.get_first;
 wenzelm parents: 
33162diff
changeset | 142 | fun get_first f = | 
| 31616 | 143 | let | 
| 35012 
c3e3ac3ca091
removed unused "boundary" of Table/Graph.get_first;
 wenzelm parents: 
33162diff
changeset | 144 | fun get Empty = NONE | 
| 31618 
2e4430b84303
improved get_first: check boundary before entering subtrees;
 wenzelm parents: 
31616diff
changeset | 145 | | get (Branch2 (left, (k, x), right)) = | 
| 35012 
c3e3ac3ca091
removed unused "boundary" of Table/Graph.get_first;
 wenzelm parents: 
33162diff
changeset | 146 | (case get left of | 
| 31616 | 147 | NONE => | 
| 35012 
c3e3ac3ca091
removed unused "boundary" of Table/Graph.get_first;
 wenzelm parents: 
33162diff
changeset | 148 | (case f (k, x) of | 
| 31616 | 149 | NONE => get right | 
| 150 | | some => some) | |
| 151 | | some => some) | |
| 31618 
2e4430b84303
improved get_first: check boundary before entering subtrees;
 wenzelm parents: 
31616diff
changeset | 152 | | get (Branch3 (left, (k1, x1), mid, (k2, x2), right)) = | 
| 35012 
c3e3ac3ca091
removed unused "boundary" of Table/Graph.get_first;
 wenzelm parents: 
33162diff
changeset | 153 | (case get left of | 
| 31616 | 154 | NONE => | 
| 35012 
c3e3ac3ca091
removed unused "boundary" of Table/Graph.get_first;
 wenzelm parents: 
33162diff
changeset | 155 | (case f (k1, x1) of | 
| 31616 | 156 | NONE => | 
| 35012 
c3e3ac3ca091
removed unused "boundary" of Table/Graph.get_first;
 wenzelm parents: 
33162diff
changeset | 157 | (case get mid of | 
| 31616 | 158 | NONE => | 
| 35012 
c3e3ac3ca091
removed unused "boundary" of Table/Graph.get_first;
 wenzelm parents: 
33162diff
changeset | 159 | (case f (k2, x2) of | 
| 31616 | 160 | NONE => get right | 
| 161 | | some => some) | |
| 162 | | some => some) | |
| 163 | | some => some) | |
| 164 | | some => some); | |
| 35012 
c3e3ac3ca091
removed unused "boundary" of Table/Graph.get_first;
 wenzelm parents: 
33162diff
changeset | 165 | in get end; | 
| 31616 | 166 | |
| 35012 
c3e3ac3ca091
removed unused "boundary" of Table/Graph.get_first;
 wenzelm parents: 
33162diff
changeset | 167 | fun exists pred = is_some o get_first (fn entry => if pred entry then SOME () else NONE); | 
| 31616 | 168 | fun forall pred = not o exists (not o pred); | 
| 169 | ||
| 170 | ||
| 5015 | 171 | (* lookup *) | 
| 172 | ||
| 67557 | 173 | fun lookup tab key = | 
| 174 | let | |
| 175 | fun look Empty = NONE | |
| 176 | | look (Branch2 (left, (k, x), right)) = | |
| 177 | (case Key.ord (key, k) of | |
| 178 | LESS => look left | |
| 179 | | EQUAL => SOME x | |
| 180 | | GREATER => look right) | |
| 181 | | look (Branch3 (left, (k1, x1), mid, (k2, x2), right)) = | |
| 182 | (case Key.ord (key, k1) of | |
| 183 | LESS => look left | |
| 184 | | EQUAL => SOME x1 | |
| 185 | | GREATER => | |
| 186 | (case Key.ord (key, k2) of | |
| 187 | LESS => look mid | |
| 188 | | EQUAL => SOME x2 | |
| 189 | | GREATER => look right)); | |
| 190 | in look tab end; | |
| 191 | ||
| 43792 
d5803c3d537a
Table.lookup_key and Graph.get_entry allow to retrieve the original key, which is not necessarily identical to the given one;
 wenzelm parents: 
39020diff
changeset | 192 | fun lookup_key tab key = | 
| 19031 | 193 | let | 
| 194 | fun look Empty = NONE | |
| 195 | | look (Branch2 (left, (k, x), right)) = | |
| 196 | (case Key.ord (key, k) of | |
| 197 | LESS => look left | |
| 43792 
d5803c3d537a
Table.lookup_key and Graph.get_entry allow to retrieve the original key, which is not necessarily identical to the given one;
 wenzelm parents: 
39020diff
changeset | 198 | | EQUAL => SOME (k, x) | 
| 19031 | 199 | | GREATER => look right) | 
| 200 | | look (Branch3 (left, (k1, x1), mid, (k2, x2), right)) = | |
| 201 | (case Key.ord (key, k1) of | |
| 202 | LESS => look left | |
| 43792 
d5803c3d537a
Table.lookup_key and Graph.get_entry allow to retrieve the original key, which is not necessarily identical to the given one;
 wenzelm parents: 
39020diff
changeset | 203 | | EQUAL => SOME (k1, x1) | 
| 19031 | 204 | | GREATER => | 
| 205 | (case Key.ord (key, k2) of | |
| 206 | LESS => look mid | |
| 43792 
d5803c3d537a
Table.lookup_key and Graph.get_entry allow to retrieve the original key, which is not necessarily identical to the given one;
 wenzelm parents: 
39020diff
changeset | 207 | | EQUAL => SOME (k2, x2) | 
| 19031 | 208 | | GREATER => look right)); | 
| 209 | in look tab end; | |
| 5015 | 210 | |
| 19031 | 211 | fun defined tab key = | 
| 212 | let | |
| 213 | fun def Empty = false | |
| 214 | | def (Branch2 (left, (k, x), right)) = | |
| 215 | (case Key.ord (key, k) of | |
| 216 | LESS => def left | |
| 217 | | EQUAL => true | |
| 218 | | GREATER => def right) | |
| 219 | | def (Branch3 (left, (k1, x1), mid, (k2, x2), right)) = | |
| 220 | (case Key.ord (key, k1) of | |
| 221 | LESS => def left | |
| 222 | | EQUAL => true | |
| 223 | | GREATER => | |
| 224 | (case Key.ord (key, k2) of | |
| 225 | LESS => def mid | |
| 226 | | EQUAL => true | |
| 227 | | GREATER => def right)); | |
| 228 | in def tab end; | |
| 16887 | 229 | |
| 5015 | 230 | |
| 19031 | 231 | (* modify *) | 
| 5015 | 232 | |
| 233 | datatype 'a growth = | |
| 234 | Stay of 'a table | | |
| 235 | Sprout of 'a table * (key * 'a) * 'a table; | |
| 236 | ||
| 15761 | 237 | exception SAME; | 
| 238 | ||
| 15665 | 239 | fun modify key f tab = | 
| 240 | let | |
| 241 | fun modfy Empty = Sprout (Empty, (key, f NONE), Empty) | |
| 242 | | modfy (Branch2 (left, p as (k, x), right)) = | |
| 243 | (case Key.ord (key, k) of | |
| 244 | LESS => | |
| 245 | (case modfy left of | |
| 246 | Stay left' => Stay (Branch2 (left', p, right)) | |
| 247 | | Sprout (left1, q, left2) => Stay (Branch3 (left1, q, left2, p, right))) | |
| 248 | | EQUAL => Stay (Branch2 (left, (k, f (SOME x)), right)) | |
| 249 | | GREATER => | |
| 250 | (case modfy right of | |
| 251 | Stay right' => Stay (Branch2 (left, p, right')) | |
| 252 | | Sprout (right1, q, right2) => | |
| 253 | Stay (Branch3 (left, p, right1, q, right2)))) | |
| 254 | | modfy (Branch3 (left, p1 as (k1, x1), mid, p2 as (k2, x2), right)) = | |
| 255 | (case Key.ord (key, k1) of | |
| 5015 | 256 | LESS => | 
| 15665 | 257 | (case modfy left of | 
| 258 | Stay left' => Stay (Branch3 (left', p1, mid, p2, right)) | |
| 259 | | Sprout (left1, q, left2) => | |
| 260 | Sprout (Branch2 (left1, q, left2), p1, Branch2 (mid, p2, right))) | |
| 261 | | EQUAL => Stay (Branch3 (left, (k1, f (SOME x1)), mid, p2, right)) | |
| 5015 | 262 | | GREATER => | 
| 15665 | 263 | (case Key.ord (key, k2) of | 
| 264 | LESS => | |
| 265 | (case modfy mid of | |
| 266 | Stay mid' => Stay (Branch3 (left, p1, mid', p2, right)) | |
| 267 | | Sprout (mid1, q, mid2) => | |
| 268 | Sprout (Branch2 (left, p1, mid1), q, Branch2 (mid2, p2, right))) | |
| 269 | | EQUAL => Stay (Branch3 (left, p1, mid, (k2, f (SOME x2)), right)) | |
| 270 | | GREATER => | |
| 271 | (case modfy right of | |
| 272 | Stay right' => Stay (Branch3 (left, p1, mid, p2, right')) | |
| 273 | | Sprout (right1, q, right2) => | |
| 274 | Sprout (Branch2 (left, p1, mid), p2, Branch2 (right1, q, right2))))); | |
| 5015 | 275 | |
| 15665 | 276 | in | 
| 277 | (case modfy tab of | |
| 278 | Stay tab' => tab' | |
| 279 | | Sprout br => Branch2 br) | |
| 280 | handle SAME => tab | |
| 281 | end; | |
| 5015 | 282 | |
| 17412 | 283 | fun update (key, x) tab = modify key (fn _ => x) tab; | 
| 284 | fun update_new (key, x) tab = modify key (fn NONE => x | SOME _ => raise DUP key) tab; | |
| 17709 | 285 | fun default (key, x) tab = modify key (fn NONE => x | SOME _ => raise SAME) tab; | 
| 15761 | 286 | fun map_entry key f = modify key (fn NONE => raise SAME | SOME x => f x); | 
| 27783 | 287 | fun map_default (key, x) f = modify key (fn NONE => f x | SOME y => f y); | 
| 5015 | 288 | |
| 289 | ||
| 15014 | 290 | (* delete *) | 
| 291 | ||
| 15665 | 292 | exception UNDEF of key; | 
| 293 | ||
| 294 | local | |
| 295 | ||
| 296 | fun compare NONE (k2, _) = LESS | |
| 297 | | compare (SOME k1) (k2, _) = Key.ord (k1, k2); | |
| 15014 | 298 | |
| 299 | fun if_eq EQUAL x y = x | |
| 300 | | if_eq _ x y = y; | |
| 301 | ||
| 15531 | 302 | fun del (SOME k) Empty = raise UNDEF k | 
| 303 | | del NONE (Branch2 (Empty, p, Empty)) = (p, (true, Empty)) | |
| 304 | | del NONE (Branch3 (Empty, p, Empty, q, Empty)) = | |
| 15014 | 305 | (p, (false, Branch2 (Empty, q, Empty))) | 
| 15665 | 306 | | del k (Branch2 (Empty, p, Empty)) = (case compare k p of | 
| 16002 | 307 | EQUAL => (p, (true, Empty)) | _ => raise UNDEF (the k)) | 
| 15665 | 308 | | del k (Branch3 (Empty, p, Empty, q, Empty)) = (case compare k p of | 
| 15014 | 309 | EQUAL => (p, (false, Branch2 (Empty, q, Empty))) | 
| 15665 | 310 | | _ => (case compare k q of | 
| 15014 | 311 | EQUAL => (q, (false, Branch2 (Empty, p, Empty))) | 
| 16002 | 312 | | _ => raise UNDEF (the k))) | 
| 15665 | 313 | | del k (Branch2 (l, p, r)) = (case compare k p of | 
| 15014 | 314 | LESS => (case del k l of | 
| 315 | (p', (false, l')) => (p', (false, Branch2 (l', p, r))) | |
| 316 | | (p', (true, l')) => (p', case r of | |
| 317 | Branch2 (rl, rp, rr) => | |
| 318 | (true, Branch3 (l', p, rl, rp, rr)) | |
| 319 | | Branch3 (rl, rp, rm, rq, rr) => (false, Branch2 | |
| 320 | (Branch2 (l', p, rl), rp, Branch2 (rm, rq, rr))))) | |
| 15531 | 321 | | ord => (case del (if_eq ord NONE k) r of | 
| 15014 | 322 | (p', (false, r')) => (p', (false, Branch2 (l, if_eq ord p' p, r'))) | 
| 323 | | (p', (true, r')) => (p', case l of | |
| 324 | Branch2 (ll, lp, lr) => | |
| 325 | (true, Branch3 (ll, lp, lr, if_eq ord p' p, r')) | |
| 326 | | Branch3 (ll, lp, lm, lq, lr) => (false, Branch2 | |
| 327 | (Branch2 (ll, lp, lm), lq, Branch2 (lr, if_eq ord p' p, r')))))) | |
| 15665 | 328 | | del k (Branch3 (l, p, m, q, r)) = (case compare k q of | 
| 329 | LESS => (case compare k p of | |
| 15014 | 330 | LESS => (case del k l of | 
| 331 | (p', (false, l')) => (p', (false, Branch3 (l', p, m, q, r))) | |
| 332 | | (p', (true, l')) => (p', (false, case (m, r) of | |
| 333 | (Branch2 (ml, mp, mr), Branch2 _) => | |
| 334 | Branch2 (Branch3 (l', p, ml, mp, mr), q, r) | |
| 335 | | (Branch3 (ml, mp, mm, mq, mr), _) => | |
| 336 | Branch3 (Branch2 (l', p, ml), mp, Branch2 (mm, mq, mr), q, r) | |
| 337 | | (Branch2 (ml, mp, mr), Branch3 (rl, rp, rm, rq, rr)) => | |
| 338 | Branch3 (Branch2 (l', p, ml), mp, Branch2 (mr, q, rl), rp, | |
| 339 | Branch2 (rm, rq, rr))))) | |
| 15531 | 340 | | ord => (case del (if_eq ord NONE k) m of | 
| 15014 | 341 | (p', (false, m')) => | 
| 342 | (p', (false, Branch3 (l, if_eq ord p' p, m', q, r))) | |
| 343 | | (p', (true, m')) => (p', (false, case (l, r) of | |
| 344 | (Branch2 (ll, lp, lr), Branch2 _) => | |
| 345 | Branch2 (Branch3 (ll, lp, lr, if_eq ord p' p, m'), q, r) | |
| 346 | | (Branch3 (ll, lp, lm, lq, lr), _) => | |
| 347 | Branch3 (Branch2 (ll, lp, lm), lq, | |
| 348 | Branch2 (lr, if_eq ord p' p, m'), q, r) | |
| 349 | | (_, Branch3 (rl, rp, rm, rq, rr)) => | |
| 350 | Branch3 (l, if_eq ord p' p, Branch2 (m', q, rl), rp, | |
| 351 | Branch2 (rm, rq, rr)))))) | |
| 15531 | 352 | | ord => (case del (if_eq ord NONE k) r of | 
| 15014 | 353 | (q', (false, r')) => | 
| 354 | (q', (false, Branch3 (l, p, m, if_eq ord q' q, r'))) | |
| 355 | | (q', (true, r')) => (q', (false, case (l, m) of | |
| 356 | (Branch2 _, Branch2 (ml, mp, mr)) => | |
| 357 | Branch2 (l, p, Branch3 (ml, mp, mr, if_eq ord q' q, r')) | |
| 358 | | (_, Branch3 (ml, mp, mm, mq, mr)) => | |
| 359 | Branch3 (l, p, Branch2 (ml, mp, mm), mq, | |
| 360 | Branch2 (mr, if_eq ord q' q, r')) | |
| 361 | | (Branch3 (ll, lp, lm, lq, lr), Branch2 (ml, mp, mr)) => | |
| 362 | Branch3 (Branch2 (ll, lp, lm), lq, Branch2 (lr, p, ml), mp, | |
| 363 | Branch2 (mr, if_eq ord q' q, r')))))); | |
| 364 | ||
| 15665 | 365 | in | 
| 366 | ||
| 15761 | 367 | fun delete key tab = snd (snd (del (SOME key) tab)); | 
| 44336 
59ff5a93eef4
tuned Table.delete_safe: avoid potentially expensive attempt of delete;
 wenzelm parents: 
43792diff
changeset | 368 | fun delete_safe key tab = if defined tab key then delete key tab else tab; | 
| 15014 | 369 | |
| 15665 | 370 | end; | 
| 371 | ||
| 15014 | 372 | |
| 19031 | 373 | (* membership operations *) | 
| 16466 | 374 | |
| 375 | fun member eq tab (key, x) = | |
| 17412 | 376 | (case lookup tab key of | 
| 16466 | 377 | NONE => false | 
| 378 | | SOME y => eq (x, y)); | |
| 15761 | 379 | |
| 380 | fun insert eq (key, x) = | |
| 381 | modify key (fn NONE => x | SOME y => if eq (x, y) then raise SAME else raise DUP key); | |
| 382 | ||
| 383 | fun remove eq (key, x) tab = | |
| 17412 | 384 | (case lookup tab key of | 
| 15761 | 385 | NONE => tab | 
| 386 | | SOME y => if eq (x, y) then delete key tab else tab); | |
| 387 | ||
| 388 | ||
| 19031 | 389 | (* simultaneous modifications *) | 
| 390 | ||
| 29004 | 391 | fun make entries = fold update_new entries empty; | 
| 5015 | 392 | |
| 12287 | 393 | fun join f (table1, table2) = | 
| 55727 
7e330ae052bb
optimize special case according to Library.merge (see also 8fbc355100f2, 520872460b7b);
 wenzelm parents: 
52049diff
changeset | 394 | let | 
| 
7e330ae052bb
optimize special case according to Library.merge (see also 8fbc355100f2, 520872460b7b);
 wenzelm parents: 
52049diff
changeset | 395 | fun add (key, y) tab = modify key (fn NONE => y | SOME x => f key (x, y)) tab; | 
| 
7e330ae052bb
optimize special case according to Library.merge (see also 8fbc355100f2, 520872460b7b);
 wenzelm parents: 
52049diff
changeset | 396 | in | 
| 
7e330ae052bb
optimize special case according to Library.merge (see also 8fbc355100f2, 520872460b7b);
 wenzelm parents: 
52049diff
changeset | 397 | if pointer_eq (table1, table2) then table1 | 
| 
7e330ae052bb
optimize special case according to Library.merge (see also 8fbc355100f2, 520872460b7b);
 wenzelm parents: 
52049diff
changeset | 398 | else if is_empty table1 then table2 | 
| 
7e330ae052bb
optimize special case according to Library.merge (see also 8fbc355100f2, 520872460b7b);
 wenzelm parents: 
52049diff
changeset | 399 | else fold_table add table2 table1 | 
| 
7e330ae052bb
optimize special case according to Library.merge (see also 8fbc355100f2, 520872460b7b);
 wenzelm parents: 
52049diff
changeset | 400 | end; | 
| 12287 | 401 | |
| 19031 | 402 | fun merge eq = join (fn key => fn xy => if eq xy then raise SAME else raise DUP key); | 
| 5015 | 403 | |
| 404 | ||
| 19031 | 405 | (* list tables *) | 
| 15761 | 406 | |
| 18946 | 407 | fun lookup_list tab key = these (lookup tab key); | 
| 25391 | 408 | |
| 409 | fun cons_list (key, x) tab = modify key (fn NONE => [x] | SOME xs => x :: xs) tab; | |
| 5015 | 410 | |
| 20124 | 411 | fun insert_list eq (key, x) = | 
| 412 | modify key (fn NONE => [x] | SOME xs => if Library.member eq xs x then raise SAME else x :: xs); | |
| 25391 | 413 | |
| 18946 | 414 | fun remove_list eq (key, x) tab = | 
| 15761 | 415 | map_entry key (fn xs => (case Library.remove eq x xs of [] => raise UNDEF key | ys => ys)) tab | 
| 416 | handle UNDEF _ => delete key tab; | |
| 5015 | 417 | |
| 25391 | 418 | fun update_list eq (key, x) = | 
| 419 | modify key (fn NONE => [x] | SOME [] => [x] | SOME (xs as y :: _) => | |
| 420 | if eq (x, y) then raise SAME else Library.update eq x xs); | |
| 421 | ||
| 422 | fun make_list args = fold_rev cons_list args empty; | |
| 19482 
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
 wenzelm parents: 
19073diff
changeset | 423 | fun dest_list tab = maps (fn (key, xs) => map (pair key) xs) (dest tab); | 
| 19031 | 424 | fun merge_list eq = join (fn _ => Library.merge eq); | 
| 5015 | 425 | |
| 426 | ||
| 50234 | 427 | (* unit tables *) | 
| 428 | ||
| 429 | type set = unit table; | |
| 430 | ||
| 67529 | 431 | fun insert_set x = default (x, ()); | 
| 432 | fun remove_set x : set -> set = delete_safe x; | |
| 63511 | 433 | fun make_set entries = fold insert_set entries empty; | 
| 50234 | 434 | |
| 435 | ||
| 47980 
c81801f881b3
simplified Poly/ML setup -- 5.3.0 is now the common base-line;
 wenzelm parents: 
44336diff
changeset | 436 | (* ML pretty-printing *) | 
| 38635 
f76ad0771f67
added ML toplevel pretty-printing for tables, using dummy for anything other than Poly/ML 5.3.0 (or later);
 wenzelm parents: 
35012diff
changeset | 437 | |
| 
f76ad0771f67
added ML toplevel pretty-printing for tables, using dummy for anything other than Poly/ML 5.3.0 (or later);
 wenzelm parents: 
35012diff
changeset | 438 | val _ = | 
| 62819 
d3ff367a16a0
careful export of type-dependent functions, without losing their special status;
 wenzelm parents: 
62503diff
changeset | 439 | ML_system_pp (fn depth => fn pretty => fn tab => | 
| 62503 | 440 | ML_Pretty.to_polyml | 
| 38635 
f76ad0771f67
added ML toplevel pretty-printing for tables, using dummy for anything other than Poly/ML 5.3.0 (or later);
 wenzelm parents: 
35012diff
changeset | 441 |       (ML_Pretty.enum "," "{" "}"
 | 
| 62503 | 442 | (ML_Pretty.pair | 
| 62819 
d3ff367a16a0
careful export of type-dependent functions, without losing their special status;
 wenzelm parents: 
62503diff
changeset | 443 | (ML_Pretty.from_polyml o ML_system_pretty) | 
| 62503 | 444 | (ML_Pretty.from_polyml o pretty)) | 
| 38635 
f76ad0771f67
added ML toplevel pretty-printing for tables, using dummy for anything other than Poly/ML 5.3.0 (or later);
 wenzelm parents: 
35012diff
changeset | 445 | (dest tab, depth))); | 
| 
f76ad0771f67
added ML toplevel pretty-printing for tables, using dummy for anything other than Poly/ML 5.3.0 (or later);
 wenzelm parents: 
35012diff
changeset | 446 | |
| 
f76ad0771f67
added ML toplevel pretty-printing for tables, using dummy for anything other than Poly/ML 5.3.0 (or later);
 wenzelm parents: 
35012diff
changeset | 447 | |
| 5681 | 448 | (*final declarations of this structure!*) | 
| 39020 | 449 | val map = map_table; | 
| 16446 | 450 | val fold = fold_table; | 
| 28334 | 451 | val fold_rev = fold_rev_table; | 
| 5015 | 452 | |
| 453 | end; | |
| 454 | ||
| 31971 
8c1b845ed105
renamed functor TableFun to Table, and GraphFun to Graph;
 wenzelm parents: 
31621diff
changeset | 455 | structure Inttab = Table(type key = int val ord = int_ord); | 
| 
8c1b845ed105
renamed functor TableFun to Table, and GraphFun to Graph;
 wenzelm parents: 
31621diff
changeset | 456 | structure Symtab = Table(type key = string val ord = fast_string_ord); | 
| 
8c1b845ed105
renamed functor TableFun to Table, and GraphFun to Graph;
 wenzelm parents: 
31621diff
changeset | 457 | structure Symreltab = Table(type key = string * string | 
| 30647 
ef8f46c3158a
added Symreltab (binary relations of symbols) instance of TableFun
 haftmann parents: 
30290diff
changeset | 458 | val ord = prod_ord fast_string_ord fast_string_ord); |