11f4b611f3b11e54cc2f8b5ac3842ce1e45c8aa4
[packages/base.git] / Data / Set.hs
1 -----------------------------------------------------------------------------
2 --
3 -- Module : Data.Set
4 -- Copyright : (c) The University of Glasgow 2001
5 -- License : BSD-style (see the file libraries/core/LICENSE)
6 --
7 -- Maintainer : libraries@haskell.org
8 -- Stability : provisional
9 -- Portability : portable
10 --
11 -- $Id: Set.hs,v 1.1 2001/09/13 11:50:35 simonmar Exp $
12 --
13 -- This implementation of sets sits squarely upon Data.FiniteMap.
14 --
15 -----------------------------------------------------------------------------
16
17 module Data.Set (
18 Set, -- abstract, instance of: Eq
19
20 emptySet, -- :: Set a
21 mkSet, -- :: Ord a => [a] -> Set a
22 setToList, -- :: Set a -> [a]
23 unitSet, -- :: a -> Set a
24 singletonSet, -- :: a -> Set a
25
26 union, -- :: Ord a => Set a -> Set a -> Set a
27 unionManySets, -- :: Ord a => [Set a] -> Set a
28 minusSet, -- :: Ord a => Set a -> Set a -> Set a
29 mapSet, -- :: Ord a => (b -> a) -> Set b -> Set a
30 intersect, -- :: Ord a => Set a -> Set a -> Set a
31 addToSet, -- :: Ord a => Set a -> a -> Set a
32 delFromSet, -- :: Ord a => Set a -> a -> Set a
33
34 elementOf, -- :: Ord a => a -> Set a -> Bool
35 isEmptySet, -- :: Set a -> Bool
36
37 cardinality -- :: Set a -> Int
38 ) where
39
40 import Prelude
41
42 import Data.FiniteMap
43 import Data.Maybe
44
45 -- This can't be a type synonym if you want to use constructor classes.
46 newtype Set a = MkSet (FiniteMap a ())
47
48 emptySet :: Set a
49 emptySet = MkSet emptyFM
50
51 unitSet :: a -> Set a
52 unitSet x = MkSet (unitFM x ())
53
54 {-# DEPRECATED singletonSet "use Set.unitSet" #-}
55 singletonSet = unitSet -- old;deprecated.
56
57 setToList :: Set a -> [a]
58 setToList (MkSet set) = keysFM set
59
60 mkSet :: Ord a => [a] -> Set a
61 mkSet xs = MkSet (listToFM [ (x, ()) | x <- xs])
62
63 union :: Ord a => Set a -> Set a -> Set a
64 union (MkSet set1) (MkSet set2) = MkSet (plusFM set1 set2)
65
66 unionManySets :: Ord a => [Set a] -> Set a
67 unionManySets ss = foldr union emptySet ss
68
69 minusSet :: Ord a => Set a -> Set a -> Set a
70 minusSet (MkSet set1) (MkSet set2) = MkSet (minusFM set1 set2)
71
72 intersect :: Ord a => Set a -> Set a -> Set a
73 intersect (MkSet set1) (MkSet set2) = MkSet (intersectFM set1 set2)
74
75 addToSet :: Ord a => Set a -> a -> Set a
76 addToSet (MkSet set) a = MkSet (addToFM set a ())
77
78 delFromSet :: Ord a => Set a -> a -> Set a
79 delFromSet (MkSet set) a = MkSet (delFromFM set a)
80
81 elementOf :: Ord a => a -> Set a -> Bool
82 elementOf x (MkSet set) = isJust (lookupFM set x)
83
84 isEmptySet :: Set a -> Bool
85 isEmptySet (MkSet set) = sizeFM set == 0
86
87 mapSet :: Ord a => (b -> a) -> Set b -> Set a
88 mapSet f (MkSet set) = MkSet (listToFM [ (f key, ()) | key <- keysFM set ])
89
90 cardinality :: Set a -> Int
91 cardinality (MkSet set) = sizeFM set
92
93 -- fair enough...
94 instance (Eq a) => Eq (Set a) where
95 (MkSet set_1) == (MkSet set_2) = set_1 == set_2
96 (MkSet set_1) /= (MkSet set_2) = set_1 /= set_2
97
98 -- but not so clear what the right thing to do is:
99 {- NO:
100 instance (Ord a) => Ord (Set a) where
101 (MkSet set_1) <= (MkSet set_2) = set_1 <= set_2
102 -}