1 (* Copyright (C) 1999-2006 Henry Cejtin, Matthew Fluet, Suresh
2 * Jagannathan, and Stephen Weeks.
4 * MLton is released under a BSD-style license.
5 * See the file MLton-LICENSE for details.
8 functor PolyUnorderedSet(): POLY_SET =
14 type 'a info = {equal: 'a * 'a -> bool,
15 output: 'a * Out.t -> unit}
17 datatype 'a t = T of 'a List.t * 'a info
19 fun elts(T(xs, _)) = xs
21 fun empty info = T([], info)
23 fun isEmpty s = List.isEmpty(elts s)
25 fun forall(s, f) = L.forall(elts s, f)
26 fun exists(s, f) = L.exists(elts s, f)
27 fun foreach(s, f) = L.foreach(elts s, f)
29 fun contains(T(elts, {equal, ...}), x) =
30 L.exists(elts, fn x' => equal(x, x'))
32 fun s <= s' = forall(s, fn x => contains(s', x))
34 fun equal(s, s') = s <= s' andalso s' <= s
40 fun s < s' = s <= s' andalso exists(s', fn x => not(contains(s, x)))
44 fun add(s as T(elts, info), x) =
45 if contains(s, x) then s
46 else T(x :: elts, info)
48 fun subset(T(elts, info), f) =
49 T(L.keepAll(elts, f), info)
51 fun s1 - s2 = subset(s1, fn x => not(contains(s2, x)))
53 fun s1 + (s2 as T(x2s, _)) = let val T(x1s, info) = s1 - s2
54 in T(L.append(x1s, x2s), info)
57 (*fun union ss = L.foldl(ss, empty, op +)*)
59 fun intersect(s, s') = subset(s, fn x => contains(s', x))
61 fun toList(T(xs, _)) = xs
63 fun remove(T(xs, info as {equal, ...}), x) =
64 T(L.remove(xs, fn x' => equal(x, x')),
67 fun size(T(xs, _)) = L.length xs
69 fun output(T(elts, {output, ...}), out) =
70 let val print = Outstream.outputc out
72 L.output(", ", output) (elts, out) ;
78 structure PolySet = PolyUnorderedSet()