HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Modulerio-0.1.22.0Haskell2010

RIO.Set

Set. Import as:

import qualified RIO.Set as Set

This module does not export any partial or unchecked functions. For those, see RIO.Set.Partial and RIO.Set.Unchecked

  • 1 type
  • 50 values
  • Packagerio-0.1.22.0
  • Exports51
  • LanguageHaskell2010
  • LicenceMIT
  • SourceSet.hs

Set type

1 declaration
datadata Set a
#

A set of values a.

Instances18Foldable, Eq1, Ord1, Show1, Hashable1, Lift, …
  • Foldable SetDefined in containers-0.7 · Data.Set.Internal

    Folds in order of increasing key.

  • Eq1 SetDefined in containers-0.7 · Data.Set.Internal
  • Ord1 SetDefined in containers-0.7 · Data.Set.Internal
  • Show1 SetDefined in containers-0.7 · Data.Set.Internal
  • Hashable1 SetDefined in hashable-1.4.7.0 · Data.Hashable.Class
  • Lift a => Lift (Set a)Defined in containers-0.7 · Data.Set.Internal
  • Ord a => IsList (Set a)Defined in containers-0.7 · Data.Set.Internal
  • Eq a => Eq (Set a)Defined in containers-0.7 · Data.Set.Internal
  • (Data a, Ord a) => Data (Set a)Defined in containers-0.7 · Data.Set.Internal
  • Ord a => Ord (Set a)Defined in containers-0.7 · Data.Set.Internal
  • (Read a, Ord a) => Read (Set a)Defined in containers-0.7 · Data.Set.Internal
  • Show a => Show (Set a)Defined in containers-0.7 · Data.Set.Internal
  • Ord a => Semigroup (Set a)Defined in containers-0.7 · Data.Set.Internal
  • Ord a => Monoid (Set a)Defined in containers-0.7 · Data.Set.Internal
  • NFData a => NFData (Set a)Defined in containers-0.7 · Data.Set.Internal
  • Binary a => Binary (Set a)Defined in binary-0.8.9.3 · Data.Binary.Class
  • Hashable v => Hashable (Set v)Defined in hashable-1.4.7.0 · Data.Hashable.Class
  • type Item (Set a) = aDefined in containers-0.7 · Data.Set.Internal

Operators

1 declaration
value(\\) :: Ord a => Set a -> Set a -> Set a
#

O\bigl(m \log\bigl(\frac{n}{m}+1\bigr)\bigr), \; 0 < m \leq n. See difference.

Query

10 declarations
valuenull :: Set a -> Bool
#

O(1). Is this the empty set?

valuesize :: Set a -> Int
#

O(1). The number of elements in the set.

valuemember :: Ord a => a -> Set a -> Bool
#

O(\log n). Is the element in the set?

valuelookupLT :: Ord a => a -> Set a -> Maybe a
#

O(\log n). Find largest element smaller than the given one.

lookupLT 3 (fromList [3, 5]) == Nothing
lookupLT 5 (fromList [3, 5]) == Just 3
valuelookupGT :: Ord a => a -> Set a -> Maybe a
#

O(\log n). Find smallest element greater than the given one.

lookupGT 4 (fromList [3, 5]) == Just 5
lookupGT 5 (fromList [3, 5]) == Nothing
valuelookupLE :: Ord a => a -> Set a -> Maybe a
#

O(\log n). Find largest element smaller or equal to the given one.

lookupLE 2 (fromList [3, 5]) == Nothing
lookupLE 4 (fromList [3, 5]) == Just 3
lookupLE 5 (fromList [3, 5]) == Just 5
valuelookupGE :: Ord a => a -> Set a -> Maybe a
#

O(\log n). Find smallest element greater or equal to the given one.

lookupGE 3 (fromList [3, 5]) == Just 3
lookupGE 4 (fromList [3, 5]) == Just 5
lookupGE 6 (fromList [3, 5]) == Nothing
valueisSubsetOf :: Ord a => Set a -> Set a -> Bool
#

O\bigl(m \log\bigl(\frac{n}{m}+1\bigr)\bigr), \; 0 < m \leq n. (s1 `isSubsetOf` s2) indicates whether s1 is a subset of s2.

s1 `isSubsetOf` s2 = all (`member` s2) s1
s1 `isSubsetOf` s2 = null (s1 `difference` s2)
s1 `isSubsetOf` s2 = s1 `union` s2 == s2
s1 `isSubsetOf` s2 = s1 `intersection` s2 == s1
valueisProperSubsetOf :: Ord a => Set a -> Set a -> Bool
#

O\bigl(m \log\bigl(\frac{n}{m}+1\bigr)\bigr), \; 0 < m \leq n. (s1 `isProperSubsetOf` s2) indicates whether s1 is a proper subset of s2.

s1 `isProperSubsetOf` s2 = s1 `isSubsetOf` s2 && s1 /= s2

Construction

4 declarations
valueempty :: Set a
#

O(1). The empty set.

valueinsert :: Ord a => a -> Set a -> Set a
#

O(\log n). Insert an element in a set. If the set already contains an element equal to the given value, it is replaced with the new value.

valuedelete :: Ord a => a -> Set a -> Set a
#

O(\log n). Delete an element from a set.

Combine

4 declarations
valueunion :: Ord a => Set a -> Set a -> Set a
#

O\bigl(m \log\bigl(\frac{n}{m}+1\bigr)\bigr), \; 0 < m \leq n. The union of two sets, preferring the first set when equal elements are encountered.

valuedifference :: Ord a => Set a -> Set a -> Set a
#

O\bigl(m \log\bigl(\frac{n}{m}+1\bigr)\bigr), \; 0 < m \leq n. Difference of two sets.

Return elements of the first set not existing in the second set.

difference (fromList [5, 3]) (fromList [5, 7]) == singleton 3
valueintersection :: Ord a => Set a -> Set a -> Set a
#

O\bigl(m \log\bigl(\frac{n}{m}+1\bigr)\bigr), \; 0 < m \leq n. The intersection of two sets. Elements of the result come from the first set, so for example

import qualified Data.Set as S
data AB = A | B deriving Show
instance Ord AB where compare _ _ = EQ
instance Eq AB where _ == _ = True
main = print (S.singleton A `S.intersection` S.singleton B,
              S.singleton B `S.intersection` S.singleton A)

prints (fromList [A],fromList [B]).

Filter

8 declarations
valuefilter :: (a -> Bool) -> Set a -> Set a
#

O(n). Filter all elements that satisfy the predicate.

valuespanAntitone :: (a -> Bool) -> Set a -> (Set a, Set a)
#

O(\log n). Divide a set at the point where a predicate on the elements stops holding. The user is responsible for ensuring that for all elements j and k in the set, j < k ==> p j >= p k.

spanAntitone p xs = (takeWhileAntitone p xs, dropWhileAntitone p xs)
spanAntitone p xs = partition p xs

Note: if p is not actually antitone, then spanAntitone will split the set at some unspecified point where the predicate switches from holding to not holding (where the predicate is seen to hold before the first element and to fail after the last element).

valuepartition :: (a -> Bool) -> Set a -> (Set a, Set a)
#

O(n). Partition the set into two sets, one with all elements that satisfy the predicate and one with all elements that don't satisfy the predicate. See also split.

valuesplit :: Ord a => a -> Set a -> (Set a, Set a)
#

O(\log n). The expression (split x set) is a pair (set1,set2) where set1 comprises the elements of set less than x and set2 comprises the elements of set greater than x.

valuesplitMember :: Ord a => a -> Set a -> (Set a, Bool, Set a)
#

O(\log n). Performs a split but also returns whether the pivot element was found in the original set.

valuesplitRoot :: Set a -> [Set a]
#

O(1). Decompose a set into pieces based on the structure of the underlying tree. This function is useful for consuming a set in parallel.

No guarantee is made as to the sizes of the pieces; an internal, but deterministic process determines this. However, it is guaranteed that the pieces returned will be in ascending order (all elements in the first subset less than all elements in the second, and so on).

Examples:

splitRoot (fromList [1..6]) ==
  [fromList [1,2,3],fromList [4],fromList [5,6]]
splitRoot empty == []

Note that the current implementation does not return more than three subsets, but you should not depend on this behaviour because it can change in the future without notice.

Indexed

4 declarations
valuelookupIndex :: Ord a => a -> Set a -> Maybe Int
#

O(\log n). Lookup the index of an element, which is its zero-based index in the sorted sequence of elements. The index is a number from 0 up to, but not including, the size of the set.

isJust   (lookupIndex 2 (fromList [5,3])) == False
fromJust (lookupIndex 3 (fromList [5,3])) == 0
fromJust (lookupIndex 5 (fromList [5,3])) == 1
isJust   (lookupIndex 6 (fromList [5,3])) == False

Map

1 declaration
valuemap :: Ord b => (a -> b) -> Set a -> Set b
#

O(n \log n). map f s is the set obtained by applying f to each element of s.

It's worth noting that the size of the result may be smaller if, for some (x,y), x /= y && f x == f y

Folds

2 declarations
valuefoldr :: (a -> b -> b) -> b -> Set a -> b
#

O(n). Fold the elements in the set using the given right-associative binary operator, such that foldr f z == foldr f z . toAscList.

For example,

toAscList set = foldr (:) [] set
valuefoldl :: (a -> b -> a) -> a -> Set b -> a
#

O(n). Fold the elements in the set using the given left-associative binary operator, such that foldl f z == foldl f z . toAscList.

For example,

toDescList set = foldl (flip (:)) [] set

Strict folds

valuefoldr' :: (a -> b -> b) -> b -> Set a -> b
#

O(n). A strict version of foldr. Each application of the operator is evaluated before using the result in the next application. This function is strict in the starting value.

valuefoldl' :: (a -> b -> a) -> a -> Set b -> a
#

O(n). A strict version of foldl. Each application of the operator is evaluated before using the result in the next application. This function is strict in the starting value.

Min/Max

6 declarations
valuedeleteMin :: Set a -> Set a
#

O(\log n). Delete the minimal element. Returns an empty set if the set is empty.

valuedeleteMax :: Set a -> Set a
#

O(\log n). Delete the maximal element. Returns an empty set if the set is empty.

valuemaxView :: Set a -> Maybe (a, Set a)
#

O(\log n). Retrieves the maximal key of the set, and the set stripped of that element, or Nothing if passed an empty set.

valueminView :: Set a -> Maybe (a, Set a)
#

O(\log n). Retrieves the minimal key of the set, and the set stripped of that element, or Nothing if passed an empty set.

Conversion

0 declarations

List

valueelems :: Set a -> [a]
#

O(n). An alias of toAscList. The elements of a set in ascending order. Subject to list fusion.

valuetoList :: Set a -> [a]
#

O(n). Convert the set to a list of elements. Subject to list fusion.

valuefromList :: Ord a => [a] -> Set a
#

O(n \log n). Create a set from a list of elements.

If the elements are ordered, a linear-time implementation is used.

Ordered list

valuetoAscList :: Set a -> [a]
#

O(n). Convert the set to an ascending list of elements. Subject to list fusion.

valuetoDescList :: Set a -> [a]
#

O(n). Convert the set to a descending list of elements. Subject to list fusion.

Debugging

3 declarations
valueshowTree :: Show a => Set a -> String
#

O(n \log n). Show the tree that implements the set. The tree is shown in a compressed, hanging format.

valueshowTreeWith :: Show a => Bool -> Bool -> Set a -> String
#

O(n \log n). The expression (showTreeWith hang wide map) shows the tree that implements the set. If hang is True, a hanging tree is shown otherwise a rotated tree is shown. If wide is True, an extra wide version is shown.

Set> putStrLn $ showTreeWith True False $ fromDistinctAscList [1..5]
4
+--2
|  +--1
|  +--3
+--5

Set> putStrLn $ showTreeWith True True $ fromDistinctAscList [1..5]
4
|
+--2
|  |
|  +--1
|  |
|  +--3
|
+--5

Set> putStrLn $ showTreeWith False True $ fromDistinctAscList [1..5]
+--5
|
4
|
|  +--3
|  |
+--2
   |
   +--1
valuevalid :: Ord a => Set a -> Bool
#

O(n). Test if the internal set structure is valid.