[Haskell-cafe] Faster set intersections?

Olaf Klinke olf at aatal-apotheke.de
Sun Dec 9 16:38:11 UTC 2018


> Note that a concrete set "concretizes" anything it touches.  Don't take
> unions of these sets, though, it'll just be a mess.
> 
> 
> Won't a union just be the same as intersection but using || instead of && ?
> 
> 
> -Jan-Willem Maessen

Unions of predicates and concrete sets are easy, thanks to Set.member:

union (Pred p) (Concrete s) = Pred (\k -> p k || member k s)

What you can not do, of course, is enumerate and fold these sets. 
There is a set type [1] which supports a litte bit more: 

Set a = Maybe ((a -> Bool) -> a)

It has unions, intersections and a Monad instance and can represent infinite sets. If the base type has an Ord instance, the set can be enumerated. If the base type has an Eq instance, so has (Set a). Some functions usually implemented using Foldable are also possible, e.g. minimum and maximum. 
Caveat: Performance can be poor, depending on how the function inside the set was defined. 

Cheers,
Olaf

[1] http://hackage.haskell.org/package/infinite-search


More information about the Haskell-Cafe mailing list