data Set a
A set of values a.
This module is considered internal.
The Package Versioning Policy does not apply.
The contents of this module may change in any way whatsoever and without any warning between minor versions of this package.
Authors importing this module are expected to track development closely.
The type represents a set of elements of type Set ee. Most operations
require that e be an instance of the Ord class. A Set is strict in its
elements.
The implementation of Set is based on size balanced binary trees (or
trees of bounded balance) as described by:
Stephen Adams, "Efficient sets—a balancing act", Journal of Functional Programming 3(4):553-562, October 1993, https://doi.org/10.1017/S0956796800000885, https://groups.csail.mit.edu/mac/users/adams/BB/index.html.
J. Nievergelt and E.M. Reingold, "Binary search trees of bounded balance", SIAM journal of computing 2(1), March 1973. https://doi.org/10.1137/0202005.
Yoichi Hirai and Kazuhiko Yamamoto, "Balancing weight-balanced trees", Journal of Functional Programming 21(3):287-307, 2011, https://doi.org/10.1017/S0956796811000104
Bounds for union, intersection, and difference are as given
by
Guy Blelloch, Daniel Ferizovic, and Yihan Sun, "Parallel Ordered Sets Using Join", https://arxiv.org/abs/1602.02120v4.
data Set aA set of values a.
Bin :: Size -> a -> (Set a) -> (Set a) -> Set aTip :: Set atype Size = Int(\\) :: Ord a => Set a -> Set a -> Set aO\bigl(m \log\bigl(\frac{n}{m}+1\bigr)\bigr), \; 0 < m \leq n. See difference.
null :: Set a -> BoolO(1). Is this the empty set?
size :: Set a -> IntO(1). The number of elements in the set.
member :: Ord a => a -> Set a -> BoolO(\log n). Is the element in the set?
notMember :: Ord a => a -> Set a -> BoolO(\log n). Is the element not in the set?
lookupLT :: Ord a => a -> Set a -> Maybe aO(\log n). Find largest element smaller than the given one.
lookupLT 3 (fromList [3, 5]) == Nothing
lookupLT 5 (fromList [3, 5]) == Just 3lookupGT :: Ord a => a -> Set a -> Maybe aO(\log n). Find smallest element greater than the given one.
lookupGT 4 (fromList [3, 5]) == Just 5
lookupGT 5 (fromList [3, 5]) == NothinglookupLE :: Ord a => a -> Set a -> Maybe aO(\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 5lookupGE :: Ord a => a -> Set a -> Maybe aO(\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]) == NothingisSubsetOf :: Ord a => Set a -> Set a -> BoolO\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
isProperSubsetOf :: Ord a => Set a -> Set a -> BoolO\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
disjoint :: Ord a => Set a -> Set a -> BoolO\bigl(m \log\bigl(\frac{n}{m}+1\bigr)\bigr), \; 0 < m \leq n. Check whether two sets are disjoint
(i.e., their intersection is empty).
disjoint (fromList [2,4,6]) (fromList [1,3]) == True
disjoint (fromList [2,4,6,8]) (fromList [2,3,5,7]) == False
disjoint (fromList [1,2]) (fromList [1,2,3,4]) == False
disjoint (fromList []) (fromList []) == Truexs ``disjoint`` ys = null (xs ``intersection`` ys)
empty :: Set aO(1). The empty set.
singleton :: a -> Set aO(1). Create a singleton set.
insert :: Ord a => a -> Set a -> Set aO(\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.
delete :: Ord a => a -> Set a -> Set aO(\log n). Delete an element from a set.
pop :: Ord a => a -> Set a -> Maybe (Set a)O(\log n). Pop an element from the set.
Returns Nothing if the element is not a member of the set. Otherwise
returns Just the set with the element removed.
pop 1 (fromList [0,2,4]) == Nothing
pop 2 (fromList [0,2,4]) == Just (fromList [0,4])
alterF :: (Ord a, Functor f) => (Bool -> f Bool) -> a -> Set a -> f (Set a)O(\log n) ( can delete or insert alterF f x s)x in s depending on
whether an equal element is found in s.
In short:
'member' x \<$\> 'alterF' f x s = f ('member' x s)
Note that unlike insert, alterF will not replace an element equal to
the given value.
Note: alterF is a variant of the at combinator from Control.Lens.At.
-- Get whether the element is a member, and also insert or remove it.
getAndSet :: Ord a => a -> Bool -> Set a -> (Bool, Set a)
getAndSet x new = alterF (\\old -> (old, new)) x
-- Delete the element. If it is absent the result is Nothing.
mustDelete :: Ord a => a -> Set a -> Maybe (Set a)
mustDelete = alterF (\\b -> if b then Just False else Nothing)
powerSet :: Set a -> Set (Set a)O(2^n \log n). Calculate the power set of a set: the set of all its subsets.
t ``member`` powerSet s == t ``isSubsetOf`` s
Example:
powerSet (fromList [1,2,3]) =
fromList $ map fromList [[],[1],[1,2],[1,2,3],[1,3],[2],[2,3],[3]]
union :: Ord a => Set a -> Set a -> Set aO\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.
unions :: (Foldable f, Ord a) => f (Set a) -> Set aThe union of the sets in a Foldable structure : ().unions == foldl union empty
difference :: Ord a => Set a -> Set a -> Set aO\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 3intersection :: Ord a => Set a -> Set a -> Set aO\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]).
intersections :: Ord a => NonEmpty (Set a) -> Set aThe intersection of a series of sets. Intersections are performed left-to-right.
symmetricDifference :: Ord a => Set a -> Set a -> Set aO\bigl(m \log\bigl(\frac{n}{m}+1\bigr)\bigr), \; 0 < m \leq n.
The symmetric difference of two sets.
The result contains elements that appear in exactly one of the two sets.
symmetricDifference (fromList [0,2,4,6]) (fromList [0,3,6,9]) == fromList [2,3,4,9]
cartesianProduct :: Set a -> Set b -> Set (a, b)O(nm). Calculate the Cartesian product of two sets.
cartesianProduct xs ys = fromList $ liftA2 (,) (toList xs) (toList ys)
Example:
cartesianProduct (fromList [1,2]) (fromList [\'a\',\'b\']) =
fromList [(1,\'a\'), (1,\'b\'), (2,\'a\'), (2,\'b\')]
disjointUnion :: Set a -> Set b -> Set (Either a b)O(n+m). Calculate the disjoint union of two sets.
disjointUnion xs ys = map Left xs ``union`` map Right ys
Example:
disjointUnion (fromList [1,2]) (fromList ["hi", "bye"]) =
fromList [Left 1, Left 2, Right "hi", Right "bye"]
newtype Intersection aSets form a Semigroup under intersection.
Intersection :: Set a -> Intersection agetIntersection :: Intersection a -> Set afilter :: (a -> Bool) -> Set a -> Set aO(n). Keep all elements that satisfy the predicate.
filterA :: Applicative f => (a -> f Bool) -> Set a -> f (Set a)O(n). Keep all elements that satisfy the Applicative predicate.
takeWhileAntitone :: (a -> Bool) -> Set a -> Set aO(\log n). Take while a predicate on the elements holds.
The user is responsible for ensuring that for all elements j and k in the set,
j < k ==> p j >= p k. See note at spanAntitone.
takeWhileAntitone p = 'fromDistinctAscList' . 'Data.List.takeWhile' p . 'toList'
takeWhileAntitone p = 'filter' p
dropWhileAntitone :: (a -> Bool) -> Set a -> Set aO(\log n). Drop while a predicate on the elements holds.
The user is responsible for ensuring that for all elements j and k in the set,
j < k ==> p j >= p k. See note at spanAntitone.
dropWhileAntitone p = 'fromDistinctAscList' . 'Data.List.dropWhile' p . 'toList'
dropWhileAntitone p = 'filter' (not . p)
spanAntitone :: (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).
mapMaybe :: Ord b => (a -> Maybe b) -> Set a -> Set bO(n \log n). Map elements and collect the Just results.
If the function is monotonically non-decreasing, this function takes O(n)
time.
partition :: (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.
split :: Ord a => a -> Set a -> (Set a, Set a)O(\log n). The expression () is a pair split x set(set1,set2)
where set1 comprises the elements of set less than x and set2
comprises the elements of set greater than x.
splitMember :: 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.
splitRoot :: 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.
lookupIndex :: Ord a => a -> Set a -> Maybe IntO(\log n). Look up 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])) == FalsefindIndex :: Ord a => a -> Set a -> IntO(\log n). Return 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. Calls error when the element
is not a member of the set.
Note: This function is partial. Prefer lookupIndex.
findIndex 2 (fromList [5,3]) Error: element is not in the set
findIndex 3 (fromList [5,3]) == 0
findIndex 5 (fromList [5,3]) == 1
findIndex 6 (fromList [5,3]) Error: element is not in the setelemAt :: Int -> Set a -> aO(\log n). Retrieve an element by its index, i.e. by its zero-based
index in the sorted sequence of elements. If the index is out of range (less
than zero, greater or equal to size of the set), error is called.
Note: This function is partial.
elemAt 0 (fromList [5,3]) == 3
elemAt 1 (fromList [5,3]) == 5
elemAt 2 (fromList [5,3]) Error: index out of rangedeleteAt :: Int -> Set a -> Set aO(\log n). Delete the element at index, i.e. by its zero-based index in
the sorted sequence of elements. If the index is out of range (less than zero,
greater or equal to size of the set), error is called.
Note: This function is partial.
deleteAt 0 (fromList [5,3]) == singleton 5
deleteAt 1 (fromList [5,3]) == singleton 3
deleteAt 2 (fromList [5,3]) Error: index out of range
deleteAt (-1) (fromList [5,3]) Error: index out of rangetake :: Int -> Set a -> Set aO(\log n). Take a given number of elements in order, beginning
with the smallest ones.
take n = 'fromDistinctAscList' . 'Prelude.take' n . 'toAscList'
drop :: Int -> Set a -> Set aO(\log n). Drop a given number of elements in order, beginning
with the smallest ones.
drop n = 'fromDistinctAscList' . 'Prelude.drop' n . 'toAscList'
splitAt :: Int -> Set a -> (Set a, Set a)O(\log n). Split a set at a particular index.
splitAt !n !xs = ('take' n xs, 'drop' n xs)
map :: Ord b => (a -> b) -> Set a -> Set bO(n \log n).
is the set obtained by applying map f sf to each element of s.
If `f` is monotonically non-decreasing, this function takes O(n) time.
It's worth noting that the size of the result may be smaller if,
for some (x,y), x /= y && f x == f y
mapMonotonic :: (a -> b) -> Set a -> Set bO(n).
, but works only when mapMonotonic f s == map f sf is strictly increasing.
Semi-formally, we have:
and [x < y ==> f x < f y | x <- ls, y <- ls]
==> mapMonotonic f s == map f s
where ls = toList sWarning: This function should be used only if f is monotonically
strictly increasing. This precondition is not checked. Use map if the
precondition may not hold.
foldr :: (a -> b -> b) -> b -> Set a -> bO(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 (:) [] setfoldl :: (a -> b -> a) -> a -> Set b -> aO(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 (:)) [] setfoldr' :: (a -> b -> b) -> b -> Set a -> bO(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.
foldl' :: (a -> b -> a) -> a -> Set b -> aO(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.
fold :: (a -> b -> b) -> b -> Set a -> bUse Data.Set.foldr instead
O(n). Fold the elements in the set using the given right-associative
binary operator.
lookupMin :: Set a -> Maybe aO(\log n). The minimal element of the set. Returns Nothing if the set
is empty.
lookupMax :: Set a -> Maybe aO(\log n). The maximal element of the set. Returns Nothing if the set
is empty.
findMin :: Set a -> aO(\log n). The minimal element of the set. Calls error if the set is
empty.
Note: This function is partial. Prefer lookupMin.
findMax :: Set a -> aO(\log n). The maximal element of the set. Calls error if the set is
empty.
Note: This function is partial. Prefer lookupMax.
deleteMin :: Set a -> Set aO(\log n). Delete the minimal element. Returns an empty set if the set is empty.
deleteMax :: Set a -> Set aO(\log n). Delete the maximal element. Returns an empty set if the set is empty.
deleteFindMin :: Set a -> (a, Set a)O(\log n). Delete and find the minimal element.
Calls error if the set is empty.
Note: This function is partial. Prefer minView.
deleteFindMax :: Set a -> (a, Set a)O(\log n). Delete and find the maximal element.
Calls error if the set is empty.
Note: This function is partial. Prefer maxView.
maxView :: 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.
minView :: 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.
elems :: Set a -> [a]O(n). An alias of toAscList. The elements of a set in ascending order.
Subject to list fusion.
toList :: Set a -> [a]O(n). Convert the set to a list of elements. Subject to list fusion.
fromList :: Ord a => [a] -> Set aO(n \log n). Create a set from a list of elements.
If the elements are in non-decreasing order, this function takes O(n)
time.
toAscList :: Set a -> [a]O(n). Convert the set to an ascending list of elements. Subject to list fusion.
toDescList :: Set a -> [a]O(n). Convert the set to a descending list of elements. Subject to list
fusion.
fromAscList :: Eq a => [a] -> Set aO(n). Build a set from an ascending list in linear time.
Warning: This function should be used only if the elements are in
non-decreasing order. This precondition is not checked. Use fromList if the
precondition may not hold.
fromDistinctAscList :: [a] -> Set aO(n). Build a set from an ascending list of distinct elements in linear time.
Warning: This function should be used only if the elements are in
strictly increasing order. This precondition is not checked. Use fromList
if the precondition may not hold.
fromDescList :: Eq a => [a] -> Set aO(n). Build a set from a descending list in linear time.
Warning: This function should be used only if the elements are in
non-increasing order. This precondition is not checked. Use fromList if the
precondition may not hold.
fromDistinctDescList :: [a] -> Set aO(n). Build a set from a descending list of distinct elements in linear time.
Warning: This function should be used only if the elements are in
strictly decreasing order. This precondition is not checked. Use fromList
if the precondition may not hold.
showTree :: Show a => Set a -> StringO(n \log n). Show the tree that implements the set. The tree is shown
in a compressed, hanging format.
showTreeWith :: Show a => Bool -> Bool -> Set a -> StringO(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
|
+--1valid :: Ord a => Set a -> BoolO(n). Test if the internal set structure is valid.
data WhenMissing f aA tactic for dealing with elements present in one set but not the other in
merge or mergeA.
A tactic of type WhenMissing f a is an abstract representation of a
function of type a -> f Bool.
WhenMissing :: Set a -> f (Set a) -> a -> f Bool -> WhenMissing f amissingSubtree :: WhenMissing f a -> Set a -> f (Set a)missingElem :: WhenMissing f a -> a -> f Booltype SimpleWhenMissing = WhenMissing IdentityA tactic for dealing with elements present in one set but not the other in
merge.
A tactic of type SimpleWhenMissing a is an abstract representation
of a function of type a -> Bool.
dropMissing :: Applicative f => WhenMissing f aDrop all the elements that are missing from the other set.
dropMissing :: 'SimpleWhenMissing' a
dropMissing = filterMissing (\_ -> False)but dropMissing is more efficient.
preserveMissing :: Applicative f => WhenMissing f aPreserve the elements that are missing from the other set.
preserveMissing :: 'SimpleWhenMissing' a
preserveMissing = filterMissing (\_ -> True)but preserveMissing is more efficient.
filterMissing :: Applicative f => (a -> Bool) -> WhenMissing f aFilter the elements that are missing from the other set.
filterMissing :: (a -> Bool) -> 'SimpleWhenMissing' a
filterAMissing :: Applicative f => (a -> f Bool) -> WhenMissing f aFilter the elements that are missing from the other set using some
Applicative action.
whenMissing :: (a -> f Bool) -> (Set a -> f (Set a)) -> WhenMissing f aCreate a WhenMissing from two functions.
whenMissing must be called with two functions f and g such that
g = . filterA fg may be a more efficient way of applying f to all
elements in a Set.
Warning: It is the caller's responsibility to ensure the above property.
preserveMissing :: Applicative f => WhenMissing f a
preserveMissing = whenMissing f g
where
f _x = pure True
g s = pure s
-- Note that this satisfies g = filterA f
import Data.Functor.Const (Const(..))
import Data.Monoid (All(..))
-- For a usage of this, see examples on mergeA
isEmpty :: WhenMissing (Const All) a
isEmpty = whenMissing f g
where
f _x = Const (All False)
g s = Const (All (null s))
-- Note that this satisfies g = filterA f
runWhenMissing :: WhenMissing f a -> a -> f BoolAlong with filterAMissing, witnesses the isomorphism between
WhenMissing f a and a -> f Bool.
newtype WhenMatched f aA tactic for dealing with elements present in both sets in merge or
mergeA.
A tactic of type WhenMatched f a is an abstract representation of a
function of type a -> f Bool.
WhenMatched :: a -> f Bool -> WhenMatched f amatchedElem :: WhenMatched f a -> a -> f Booltype SimpleWhenMatched = WhenMatched IdentityA tactic for dealing with elements present in both sets in merge.
A tactic of type SimpleWhenMatched a is an abstract representation of a
function of type a -> Bool.
dropMatched :: Applicative f => WhenMatched f aWhen an element is found in both sets, drop the element.
preserveMatched :: Applicative f => WhenMatched f aWhen an element is found in both sets, keep the element.
filterMatched :: Applicative f => (a -> Bool) -> WhenMatched f aWhen an element is found in both sets, choose whether to keep the element in the merged set.
filterAMatched :: (a -> f Bool) -> WhenMatched f aWhen an element is found in both sets, choose whether to keep the element in the merged set.
runWhenMatched :: WhenMatched f a -> a -> f BoolAlong with filterAMatched, witnesses the isomorphism between
WhenMatched f a and a -> f Bool.
merge :: Ord a => SimpleWhenMissing a -> SimpleWhenMissing a -> SimpleWhenMatched a -> Set a -> Set a -> Set aMerge two sets.
merge takes two SimpleWhenMissing tactics, a SimpleWhenMatched tactic,
and two sets. It uses the tactics to merge the sets.
Consider
merge (filterMissing g1) (filterMissing g2) (filterMatched f) s1 s2
g1 = (==2)
g2 = (==3)
f = (==6)
s1 = [2, 4, 6, 8, 10, 12]
s2 = [3, 6, 9, 12]
merge will pass the elements to g1, g2, or f as appropriate,
producing a Bool for each element.
m1: [ 2, 4, 6, 8, 10 12]
m2: [ 3, 6, 9, 12]
result: [g1 2, g2 3, g1 4, f 6, g1 8, g2 9, g1 10, f 12]
= [True, True, False, True, False, False, False, False]
The result set contains the element for which we have True.
>>> merge (filterMissing g1) (filterMissing g2) (filterMatched f) s1 s2
fromList [2,3,6]The other tactics below are optimizations or simplifications of
filterMissing for special cases. Most importantly,
dropMissing drops all elements.
preserveMissing leaves all elements alone.
When merge is given three arguments, it is inlined at the call
site. To prevent excessive inlining, you should typically use merge
to define your custom combining functions.
What to do with elements in s1 but not s2
What to do with elements in s2 but not s1
What to do with elements in both s1 and s2
Set s1
Set s2
mergeA :: (Applicative f, Ord a) => WhenMissing f a -> WhenMissing f a -> WhenMatched f a -> Set a -> Set a -> f (Set a)An applicative version of merge.
mergeA takes two WhenMissing tactics, a WhenMatched tactic, and two
sets. It uses the tactics to merge the sets.
Behaves just like merge while allowing Applicative effects. Effects are
performed in increasing order of elements.
Consider,
mergeA (filterAMissing g1) (filterAMissing g2) (filterAMatched f) s1 s2
g1 x = (x == 2) <$ putStrLn ("g1 " ++ show x)
g2 x = (x == 3) <$ putStrLn ("g2 " ++ show x)
f x = (x == 6) <$ putStrLn ("f " ++ show x)
s1 = [2, 4, 6, 8, 10, 12]
s2 = [3, 6, 9, 12]
As with merge, the result set is [2,3,6]. Additionally, g1, g2, and
f perform IO effects, printing the elements in increasing order.
>>> mergeA (filterAMissing g1) (filterAMissing g2) (filterAMatched f) s1 s2
g1 2
g2 3
g1 4
f 6
g1 8
g2 9
g1 10
f 12
fromList [2,3,6]When mergeA is given three arguments, it is inlined at the call
site. To prevent excessive inlining, you should generally only use
mergeA to define custom combining functions.
data Pair a = Pair !a !a deriving Functor
instance Applicative Pair where
pure x = Pair x x
liftA2 f (Pair x1 y1) (Pair x2 y2) = Pair (f x1 x2) (f y1 y2)
-- | Calculate the union and intersection of two sets.
unionIntersection :: Ord a => Set a -> Set a -> (Set a, Set a)
unionIntersection m1 m2 =
case mergeA preserveAndDropMissing preserveAndDropMissing 'preserveMatched' m1 m2 of
Pair mu mi -> (mu, mi)
where
-- use Pair to build the union and intersection together
preserveAndDropMissing = 'whenMissing' (\\_x -> Pair True False) (\\s -> Pair s empty)
import Data.Functor.Const (Const(..))
import Data.Monoid (All(..))
-- | Whether the first set is a subset of the second set.
isSubsetOf :: Ord a => Set a -> Set a -> Bool
isSubsetOf m1 m2 =
getAll (getConst (mergeA isEmpty 'dropMissing' 'dropMatched' m1 m2))
where
isEmpty = 'whenMissing' (\\_x -> Const (All False)) (\\s -> Const (All (null s)))
What to do with elements in s1 but not s2
What to do with elements in s2 but not s1
What to do with elements in both s1 and s2
Set s1
Set s2
bin :: a -> Set a -> Set a -> Set abalanced :: Set a -> Boollink :: a -> Set a -> Set a -> Set alink2 :: Set a -> Set a -> Set abalanceL :: a -> Set a -> Set a -> Set abalanceR :: a -> Set a -> Set a -> Set aglue :: Set a -> Set a -> Set ainsertMin :: a -> Set a -> Set ainsertMax :: a -> Set a -> Set ainstance Lift a => Lift (Set a)instance Ord a => Monoid (Set a)mempty = empty
instance Ord a => Semigroup (Set a)(<>) = union
instance Foldable.Foldable SetFolds in order of increasing key.
instance (Data a, Ord a) => Data (Set a)instance Show a => Show (Intersection a)instance Eq a => Eq (Intersection a)instance Ord a => Ord (Intersection a)instance (Ord a) => Semigroup (Intersection a)instance (Ord a) => GHCExts.IsList (Set a)instance Eq a => Eq (Set a)instance Eq1 Setinstance Ord a => Ord (Set a)instance Ord1 Setinstance Show a => Show (Set a)instance Show1 Setinstance (Read a, Ord a) => Read (Set a)instance NFData a => NFData (Set a)instance NFData1 Setinstance Semigroup (MergeSet a)instance Monoid (MergeSet a)Community contribution. The service has not verified this documentation. The generator reported 15 diagnostics.
Target: Linux on x86-64. Some inferred types and resolved links are not available.
Generator: aihc-haddock-aa6debbd-web-3
Metadata snapshot: 634f718c0d8cd970c4e2c511d2345d8fd36c6dc32b06343c2b4e914ffccbce7f
Open the dependency plan · Download the documentation model