Community
Modules 31

Data.Set.Merge

This module defines an API for writing functions that merge two sets. The key functions are merge and mergeA. Each of these can be used with several different "merge tactics".

Simple merge tactic types

type SimpleWhenMissing = WhenMissing Identity

A 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.

type SimpleWhenMatched = WhenMatched Identity

A 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.

General combining function

merge :: Ord a => SimpleWhenMissing a -> SimpleWhenMissing a -> SimpleWhenMatched a -> Set a -> Set a -> Set a

Merge 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

WhenMissing tactics

dropMissing :: Applicative f => WhenMissing f a

Drop 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 a

Preserve 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 a

Filter the elements that are missing from the other set.

filterMissing :: (a -> Bool) -> 'SimpleWhenMissing' a

WhenMatched tactics

dropMatched :: Applicative f => WhenMatched f a

When an element is found in both sets, drop the element.

preserveMatched :: Applicative f => WhenMatched f a

When an element is found in both sets, keep the element.

filterMatched :: Applicative f => (a -> Bool) -> WhenMatched f a

When an element is found in both sets, choose whether to keep the element in the merged set.

Applicative merge tactic types

data WhenMissing f a

A 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.

newtype WhenMatched f a

A 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.

Applicative general combining function

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.

Examples

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

WhenMissing tactics

| The tactics described for merge work for mergeA as well. Furthermore, the following are available.

filterAMissing :: Applicative f => (a -> f Bool) -> WhenMissing f a

Filter the elements that are missing from the other set using some Applicative action.

whenMissing :: (a -> f Bool) -> (Set a -> f (Set a)) -> WhenMissing f a

Create a WhenMissing from two functions.

whenMissing must be called with two functions f and g such that g = filterA f. g 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.

Examples

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

WhenMatched tactics

| The tactics described for merge work for mergeA as well. Furthermore, the following are available.

filterAMatched :: (a -> f Bool) -> WhenMatched f a

When an element is found in both sets, choose whether to keep the element in the merged set.

Miscellaneous tactic functions

runWhenMissing :: WhenMissing f a -> a -> f Bool

Along with filterAMissing, witnesses the isomorphism between WhenMissing f a and a -> f Bool.

runWhenMatched :: WhenMatched f a -> a -> f Bool

Along with filterAMatched, witnesses the isomorphism between WhenMatched f a and a -> f Bool.

Build record and source files

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

Import a package

Enter a package name and an exact version from Hackage. The site contains imported versions only.