@@ -2,8 +2,6 @@ module Data.SortedSet
22
33import Data.SortedMap
44
5−-- TODO: add intersection, union, difference
6−
75export
86data SortedSet k = SetWrapper (Data.SortedMap.SortedMap k ())
97
@@ -31,5 +29,31 @@ export
3129toList : SortedSet k -> List k
3230toList (SetWrapper m) = map (\(i, _) => i) (Data.SortedMap.toList m)
3331
34−implementation Foldable SortedSet where
32+Foldable SortedSet where
3533 foldr f e xs = foldr f e (Data.SortedSet.toList xs)
34+
35+||| Set union. Inserts all elements of x into y
36+export
37+union : (x, y : SortedSet k) -> SortedSet k
38+union x y = foldr insert x y
39+
40+||| Set difference. Delete all elments in y from x
41+export
42+difference : (x, y : SortedSet k) -> SortedSet k
43+difference x y = foldr delete x y
44+
45+||| Set symmetric difference. Uses the union of the differences.
46+export
47+symDifference : (x, y : SortedSet k) -> SortedSet k
48+symDifference x y = union (difference x y) (difference y x)
49+
50+||| Set intersection. Implemented as the difference of the union and the symetric difference.
51+export
52+intersection : (x, y : SortedSet k) -> SortedSet k
53+intersection x y = difference x (difference x y)
54+
55+Ord k => Semigroup (SortedSet k) where
56+ (<+>) = union
57+
58+Ord k => Monoid (SortedSet k) where
59+ neutral = empty