haskell / containers

Assorted concrete container types

Home Page:https://hackage.haskell.org/package/containers

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Set difference and union in one

noughtmare opened this issue · comments

commented

Set difference and union sometimes have to be computed together, such as in semi-naive fixed point computation:

seminaive :: Ord a => Set a -> (Set a -> Set a) -> Set a
seminaive x f = go Set.empty x where
  go !done !todo
    | Set.null todo' = done
    | otherwise = go (done `Set.union` todo') (f todo')
    where todo' = todo Set.\\ done

If there was a combined operation:

differenceAndUnion :: Ord a => Set a -> Set a -> (Set a, Set a)

Then I can write it more efficiently:

seminaive :: Ord a => Set a -> (Set a -> Set a) -> Set a
seminaive x f = go Set.empty x where
  go !done !todo
    | Set.null dif = uni
    | otherwise = go uni (f dif)
    where (dif, uni) = Set.differenceAndUnion todo done

This feels way too specialized. Most combinations of operations can probably be implemented more efficiently by having a combined operation, but that doesn't mean we should do it. This isn't something I'd expect any library to implement, on the contrary, I'd feel like this would be bloat.

I've been wanting to make an equivalent of the Data.Map mergeA interface for sets for a long time, but I've never gotten around to it. That would include this operation.

commented

@treeowl that sounds much better than what I proposed here!

Which applicative would you use? I believe the straightforward Writer (Set a) wouldn't have the right time complexity, or would it?

I'd think

data Pair a = Pair a a

We'd have to make sure it generated good code in typical cases.