Set is:module

Finite Sets

The Set e type represents a set of elements of type e. Most operations require that e be an instance of the Ord class. A Set is strict in its elements. For a walkthrough of the most commonly used functions see the sets introduction. This module is intended to be imported qualified, to avoid name clashes with Prelude functions, e.g.
import Data.Set (Set)
import qualified Data.Set as Set
Note that the implementation is generally left-biased. Functions that take two sets as arguments and combine them, such as union and intersection, prefer the entries in the first argument to those in the second. Of course, this bias can only be observed when equality is an equivalence relation instead of structural equality.

Warning

The size of the set must not exceed maxBound::Int. Violation of this condition is not detected and if the size limit is exceeded, its behaviour is undefined.

Implementation

The implementation of Set is based on size balanced binary trees (or trees of bounded balance) as described by: Bounds for union, intersection, and difference are as given by

Performance information

The time complexity is given for each operation in big-O notation, with <math> referring to the number of entries in the set. Operations like member, insert, and delete take <math> time. Binary set operations like union and intersection take <math> time, where <math> and <math> are the sizes of the smaller and larger input sets respectively.
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
This module only exports ways of constructing a Set, retrieving List, Set, and Seq representations of the same data, as well as a novel "difference" function. Any other Set-like or List-like functionality should be obtained through toSet and toList, respectively.
TOML-specific combinators for converting between TOML and Haskell Set-like data types. There are two way to represent list-like structures with the tomland library.
  • Ordinary array sets of primitives:
    foo = [100, 200, 300]
    
  • Sets via tables:
    foo = [ {x = 100} , {x = 200} , {x = 300} ]
    OR [[foo]] x = 100 [[foo]] x = 200 [[foo]] x = 300 
You can find both types of the codecs in this module for different set-like structures. See the following table for the better understanding: TODO: table
A collection of set utilities, useful when working with symbolic sets. To the extent possible, the functions in this module follow those of Data.Set so importing qualified is the recommended workflow. Note that unlike Data.Set, SBV sets can be infinite, represented as a complement of some finite set. This means that a symbolic set is either finite, or its complement is finite. (If the underlying domain is finite, then obviously both the set itself and its complement will always be finite.) Therefore, there are some differences in the API from Haskell sets. For instance, you can take the complement of any set, which is something you cannot do in Haskell! Conversely, you cannot compute the size of a symbolic set (as it can be infinite!), nor you can turn it into a list or necessarily enumerate its elements.
This module defines set operations on monadic Relation operations.
This module defines a few set-like operations on type-level lists. It may be applicable beyond the units package.
Efficient sets over bounded enumerations, using bitwise operations based on containers and EdisonCore. In many cases, EnumSets may be optimised away entirely by constant folding at compile-time. For example, in the following code:
import Data.Enum.Set as E

data Foo = A | B | C | D | E | F | G | H deriving (Bounded, Enum, Eq, Ord)

instance E.AsEnumSet Foo

addFoos :: E.EnumSet Foo -> E.EnumSet Foo
addFoos = E.delete A . E.insert B

bar :: E.EnumSet Foo
bar = addFoos $ E.fromFoldable [A, C, E]

barHasA :: Bool
barHasA = E.member A bar
With -O or -O2, bar will compile to GHC.Types.W# 22## and barHasA will compile to GHC.Types.False. By default, Words are used as the representation. Other representations may be chosen in the class instance:
{-# LANGUAGE TypeFamilies #-}

import Data.Enum.Set as E
import Data.Word (Word64)

data Foo = A | B | C | D | E | F | G | H deriving (Bounded, Enum, Eq, Ord, Show)

instance E.AsEnumSet Foo where
type EnumSetRep Foo = Word64
For type EnumSet E, EnumSetRep E should be a Word-like type that implements Bits and Num, and E should be a type that implements Eq and Enum equivalently and is a bijection to Int over its range. EnumSet E can only store a value of E if the result of applying fromEnum to the value is positive and less than the number of bits in EnumSetRep E. For this reason, it is preferable for E to be a type that derives Eq and Enum, and for EnumSetRep E to have more bits than the number of constructors of E. If the highest fromEnum value of E is 29, EnumSetRep E should be Word, because it always has at least 30 bits. This is the default implementation. Otherwise, options include Word32, Word64, and the wide-word package's Data.WideWord.Word128. Foreign types may also be used. Note: complexity calculations assume that EnumSetRep E implements Bits with constant-time functions, as is the case with Word etc. Otherwise, the complexity of those operations should be added to the complexity of EnumSet functions.
Delta types for Set.
This module provides set-specific multimap functionality.
Multimaps, where values behave like (non empty) sets. Multimaps whose values behave like lists are in Data.Multimap. Multimaps whose values behave like maps (i.e., two-dimensional tables) are in Data.Multimap.Table. The implementation is backed by a Map k (Set a). The differences between Multimap k a and Map k (Set a) include:
  • A key is only present in a SetMultimap if it is associated with at least one values, i.e., a key is never associated with an empty set of values.
  • lookup (or !) returns a possibly empty set. Unlike regular maps, the ! operator is total for multimaps.
  • Functions like map, adjust, update, etc., take functions on individual values (e.g., a -> b) as opposed to, e.g., Set a -> Set b.
  • union and unions union the values when there are duplicate keys, rather than being left- or right-biased.
  • The difference function computes set differences for values of keys that exist in both maps.
  • The size function returns the total number of values for all keys in the multimap, not the number of distinct keys. The latter can be obtained by first getting the keysSet or first converting the multimap to a regular map via toMap.
In the following Big-O notations, unless otherwise noted, n denotes the size of the multimap, k denotes the number of distinct keys, and m denotes the maximum number of values associated with a single key.
This module provides a type TSet c, which is a set of list of some characters. It serves almost same purpose to Set [c], and functions of this module mirrors functions with same name from Data.Set module. The advantages to use this module over Data.Set are: But notice for some disadvantages:
  • Some operations are slower than Set [c]. Especially, count is much much slower than size (because Set.size is already recorded in the data structure). Consider TSet.count be like length of list.
  • Constructed TSet c from a list of lists [[c]] do not share each member lists with original list unlike Set [c] does. This means holding both TSet c and [[c]] in memory consumes much more memory than Set [c] and [[c]].