containers-0.8.1: Assorted concrete container types
Safe HaskellSafe
LanguageHaskell2010

Data.Set.Merge

Description

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

Since: 0.8.1

Synopsis

Simple merge tactic types

type SimpleWhenMissing = WhenMissing Identity Source #

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.

Since: 0.8.1

type SimpleWhenMatched = WhenMatched Identity Source #

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.

Since: 0.8.1

General combining function

merge Source #

Arguments

:: Ord a 
=> SimpleWhenMissing a

What to do with elements in s1 but not s2

-> SimpleWhenMissing a

What to do with elements in s2 but not s1

-> SimpleWhenMatched a

What to do with elements in both s1 and s2

-> Set a

Set s1

-> Set a

Set s2

-> 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,

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.

Since: 0.8.1

WhenMissing tactics

dropMissing :: forall (f :: Type -> Type) a. Applicative f => WhenMissing f a Source #

Drop all the elements that are missing from the other set.

dropMissing :: SimpleWhenMissing a
dropMissing = filterMissing (\_ -> False)

but dropMissing is more efficient.

Since: 0.8.1

preserveMissing :: forall (f :: Type -> Type) a. Applicative f => WhenMissing f a Source #

Preserve the elements that are missing from the other set.

preserveMissing :: SimpleWhenMissing a
preserveMissing = filterMissing (\_ -> True)

but preserveMissing is more efficient.

Since: 0.8.1

filterMissing :: forall (f :: Type -> Type) a. Applicative f => (a -> Bool) -> WhenMissing f a Source #

Filter the elements that are missing from the other set.

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

Since: 0.8.1

WhenMatched tactics

dropMatched :: forall (f :: Type -> Type) a. Applicative f => WhenMatched f a Source #

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

Since: 0.8.1

preserveMatched :: forall (f :: Type -> Type) a. Applicative f => WhenMatched f a Source #

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

Since: 0.8.1

filterMatched :: forall (f :: Type -> Type) a. Applicative f => (a -> Bool) -> WhenMatched f a Source #

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

Since: 0.8.1

Applicative merge tactic types

data WhenMissing (f :: Type -> Type) a Source #

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.

Since: 0.8.1

data WhenMatched (f :: Type -> Type) a Source #

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.

Since: 0.8.1

Applicative general combining function

mergeA Source #

Arguments

:: (Applicative f, Ord a) 
=> WhenMissing f a

What to do with elements in s1 but not s2

-> WhenMissing f a

What to do with elements in s2 but not s1

-> WhenMatched f a

What to do with elements in both s1 and s2

-> Set a

Set s1

-> Set a

Set s2

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

Expand
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)))

Since: 0.8.1

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 Source #

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

Since: 0.8.1

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

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

Expand
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

Since: 0.8.1

WhenMatched tactics

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

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

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

Since: 0.8.1

Miscellaneous tactic functions

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

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

Since: 0.8.1

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

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

Since: 0.8.1