containers: intersections for Set, along with Semigroup newtype
Eric Mertens
emertens at gmail.com
Sun Dec 6 18:58:24 UTC 2020
First consider how `and` and `or` work for booleans.
and (x ++ y) == and x && and y
For this to work we need `and [] == True`
or (x ++ y) == or x || or y
For this to work we need `or [] == False`
and and or are duals of each other.
There’s an analogue here to union and intersection which are also duals of each other.
We have: unions (x ++ y) == union (unions x) (unions y)
This requires `union [] == []` since any list xs could be split as `[] ++ xs`
We'd like to have: intersections (x ++ y) == intersect (intersections x) (intersections y)
For this kind of splitting property to make sense for intersections we’d need `intersections [] == listOfAllElementsOfThisType`, but it’s not easy to construct that list of all elements in general.
So instead we punt on the problem and refuse to define intersections on an empty list.
-glguy
> On Dec 6, 2020, at 10:50 AM, Sven Panne <svenpanne at gmail.com> wrote:
>
> Am So., 6. Dez. 2020 um 07:20 Uhr schrieb Reed Mullanix <reedmullanix at gmail.com <mailto:reedmullanix at gmail.com>>:
> [...]
> intersections :: Ord a => NonEmpty (Set a) -> Set a
> intersections (s :| ss) = Foldable.foldl' intersection s ss
> [...]
>
> Why NonEmpty? I would expect "intersections [] = Set.empty", because the result contains all the elements which are in all sets, i.e. none. That's at least my intuition, is there some law which this would violate?
> _______________________________________________
> Libraries mailing list
> Libraries at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/libraries/attachments/20201206/f4895e59/attachment.html>
More information about the Libraries
mailing list