@@ -155,7 +155,9 @@ module Data.Set.Internal (
155155 , unions
156156 , difference
157157 , intersection
158+ #if MIN_VERSION_base(4,18,0)
158159 , intersections
160+ #endif
159161 , symmetricDifference
160162 , cartesianProduct
161163 , disjointUnion
@@ -240,7 +242,6 @@ import Control.Applicative (Const(..))
240242import qualified Data.List as List
241243import Data.Bits (shiftL , shiftR )
242244import Data.Semigroup (Semigroup (stimes ))
243- import Data.List.NonEmpty (NonEmpty (.. ))
244245#if !(MIN_VERSION_base(4,11,0))
245246import Data.Semigroup (Semigroup ((<>) ))
246247#endif
@@ -249,6 +250,10 @@ import Data.Functor.Classes
249250import Data.Functor.Identity (Identity )
250251import qualified Data.Foldable as Foldable
251252import Control.DeepSeq (NFData (rnf ))
253+ #if MIN_VERSION_base(4,18,0)
254+ import qualified Data.Foldable1 as Foldable1
255+ import Data.List.NonEmpty (NonEmpty (.. ))
256+ #endif
252257
253258import Utils.Containers.Internal.StrictPair
254259import Utils.Containers.Internal.PtrEquality
@@ -894,21 +899,37 @@ intersection t1@(Bin _ x l1 r1) t2
894899{-# INLINABLE intersection #-}
895900#endif
896901
897- -- | The intersection of a series of sets. Intersections are performed left-to-right.
898- intersections :: Ord a => NonEmpty (Set a ) -> Set a
899- intersections (s0 :| ss) = List. foldr go id ss s0
900- where
901- go s r acc
902- | null acc = empty
903- | otherwise = r (intersection acc s)
902+ #if MIN_VERSION_base(4,18,0)
903+ -- | The intersection of a series of sets. Intersections are performed
904+ -- left-to-right.
905+ --
906+ -- @since FIXME
907+ intersections :: (Foldable1. Foldable1 f , Ord a ) => f (Set a ) -> Set a
908+ intersections ss = case Foldable1. toNonEmpty ss of
909+ s0 :| ss'
910+ | null s0 -> empty
911+ | otherwise -> List. foldr go id ss' s0
912+ where
913+ go s r acc
914+ | null acc' = empty
915+ | otherwise = r acc'
916+ where
917+ acc' = intersection acc s
918+ {-# INLINABLE intersections #-}
919+ #endif
904920
905- -- | Sets form a 'Semigroup' under 'intersection'.
921+ -- | @Set@s form a 'Semigroup' under 'intersection'.
922+ --
923+ -- @since FIXME
906924newtype Intersection a = Intersection { getIntersection :: Set a }
907925 deriving (Show , Eq , Ord )
908926
909927instance (Ord a ) => Semigroup (Intersection a ) where
910928 (Intersection a) <> (Intersection b) = Intersection $ intersection a b
929+ {-# INLINABLE (<>) #-}
930+
911931 stimes = stimesIdempotent
932+ {-# INLINABLE stimes #-}
912933
913934{- -------------------------------------------------------------------
914935 Symmetric difference
0 commit comments