Skip to content

Commit 3ad35a8

Browse files
committed
Use NonEmpty in intersections
Using Foldable1 requires a conditional export at the moment, which we want to avoid. We can use Foldable1 once it is available is all base versions we support.
1 parent 41783ed commit 3ad35a8

File tree

6 files changed

+11
-45
lines changed

6 files changed

+11
-45
lines changed

containers-tests/tests/intset-properties.hs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,8 @@ import Data.List (nub,sort)
77
import qualified Data.List as List
88
import Data.Maybe (listToMaybe)
99
import Data.Monoid (mempty)
10-
#if MIN_VERSION_base(4,18,0)
1110
import Data.List.NonEmpty (NonEmpty(..))
1211
import qualified Data.List.NonEmpty as NE
13-
import qualified Data.Foldable1 as Foldable1
14-
#endif
1512
import qualified Data.Set as Set
1613
import IntSetValidity (valid)
1714
import Prelude hiding (lookup, null, map, filter, foldr, foldl, foldl')
@@ -87,10 +84,8 @@ main = defaultMain $ testGroup "intset-properties"
8784
, testProperty "prop_bitcount" prop_bitcount
8885
, testProperty "prop_alterF_list" prop_alterF_list
8986
, testProperty "prop_alterF_const" prop_alterF_const
90-
#if MIN_VERSION_base(4,18,0)
9187
, testProperty "intersections" prop_intersections
9288
, testProperty "intersections_lazy" prop_intersections_lazy
93-
#endif
9489
]
9590

9691
----------------------------------------------------------------
@@ -510,10 +505,9 @@ prop_alterF_const f k s =
510505
getConst (alterF (Const . applyFun f) k s )
511506
=== getConst (Set.alterF (Const . applyFun f) k (toSet s))
512507

513-
#if MIN_VERSION_base(4,18,0)
514508
prop_intersections :: (IntSet, [IntSet]) -> Property
515509
prop_intersections (s, ss) =
516-
intersections ss' === Foldable1.foldl1' intersection ss'
510+
intersections ss' === List.foldl' intersection s ss
517511
where
518512
ss' = s :| ss -- Work around missing Arbitrary NonEmpty instance
519513

@@ -523,4 +517,3 @@ prop_intersections_lazy ss = intersections ss' === empty
523517
ss' = NE.fromList $ ss ++ [empty] ++ undefined
524518
-- ^ result will certainly be empty at this point,
525519
-- so the rest of the list should not be demanded.
526-
#endif

containers-tests/tests/set-properties.hs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,8 @@ import Control.Monad (liftM, liftM3)
1616
import Data.Functor.Identity
1717
import Data.Foldable (all)
1818
import Control.Applicative (liftA2)
19-
#if MIN_VERSION_base(4,18,0)
2019
import Data.List.NonEmpty (NonEmpty(..))
2120
import qualified Data.List.NonEmpty as NE
22-
import qualified Data.Foldable1 as Foldable1
23-
#endif
2421

2522
#if __GLASGOW_HASKELL__ >= 806
2623
import Utils.NoThunks (whnfHasNoThunks)
@@ -117,10 +114,8 @@ main = defaultMain $ testGroup "set-properties"
117114
#endif
118115
, testProperty "eq" prop_eq
119116
, testProperty "compare" prop_compare
120-
#if MIN_VERSION_base(4,18,0)
121117
, testProperty "intersections" prop_intersections
122118
, testProperty "intersections_lazy" prop_intersections_lazy
123-
#endif
124119
]
125120

126121
-- A type with a peculiar Eq instance designed to make sure keys
@@ -748,10 +743,9 @@ prop_eq s1 s2 = (s1 == s2) === (toList s1 == toList s2)
748743
prop_compare :: Set Int -> Set Int -> Property
749744
prop_compare s1 s2 = compare s1 s2 === compare (toList s1) (toList s2)
750745

751-
#if MIN_VERSION_base(4,18,0)
752746
prop_intersections :: (Set Int, [Set Int]) -> Property
753747
prop_intersections (s, ss) =
754-
intersections ss' === Foldable1.foldl1' intersection ss'
748+
intersections ss' === List.foldl' intersection s ss
755749
where
756750
ss' = s :| ss -- Work around missing Arbitrary NonEmpty instance
757751

@@ -761,4 +755,3 @@ prop_intersections_lazy ss = intersections ss' === empty
761755
ss' = NE.fromList $ ss ++ [empty] ++ undefined
762756
-- ^ result will certainly be empty at this point,
763757
-- so the rest of the list should not be demanded.
764-
#endif

containers/src/Data/IntSet.hs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,7 @@ module Data.IntSet (
109109
, difference
110110
, (\\)
111111
, intersection
112-
#if MIN_VERSION_base(4,18,0)
113112
, intersections
114-
#endif
115113
, symmetricDifference
116114
, Intersection(..)
117115

containers/src/Data/IntSet/Internal.hs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,7 @@ module Data.IntSet.Internal (
125125
, unions
126126
, difference
127127
, intersection
128-
#if MIN_VERSION_base(4,18,0)
129128
, intersections
130-
#endif
131129
, symmetricDifference
132130
, Intersection(..)
133131

@@ -195,16 +193,13 @@ import Control.Applicative (Const(..))
195193
import Control.DeepSeq (NFData(rnf))
196194
import Data.Bits
197195
import qualified Data.List as List
196+
import Data.List.NonEmpty (NonEmpty(..))
198197
import Data.Maybe (fromMaybe)
199198
import Data.Semigroup
200199
(Semigroup(stimes), stimesIdempotent, stimesIdempotentMonoid)
201200
#if !(MIN_VERSION_base(4,11,0))
202201
import Data.Semigroup (Semigroup((<>)))
203202
#endif
204-
#if MIN_VERSION_base(4,18,0)
205-
import qualified Data.Foldable1 as Foldable1
206-
import Data.List.NonEmpty (NonEmpty(..))
207-
#endif
208203
import Utils.Containers.Internal.Prelude hiding
209204
(filter, foldr, foldl, foldl', null, map)
210205
import Prelude ()
@@ -667,24 +662,21 @@ intersection (Tip kx1 bm1) t2 = intersectBM t2
667662

668663
intersection Nil _ = Nil
669664

670-
#if MIN_VERSION_base(4,18,0)
671665
-- | The intersection of a series of sets. Intersections are performed
672666
-- left-to-right.
673667
--
674668
-- @since FIXME
675-
intersections :: Foldable1.Foldable1 f => f IntSet -> IntSet
676-
intersections ss = case Foldable1.toNonEmpty ss of
677-
s0 :| ss'
678-
| null s0 -> empty
679-
| otherwise -> List.foldr go id ss' s0
669+
intersections :: NonEmpty IntSet -> IntSet
670+
intersections (s0 :| ss)
671+
| null s0 = empty
672+
| otherwise = List.foldr go id ss s0
680673
where
681674
go s r acc
682675
| null acc' = empty
683676
| otherwise = r acc'
684677
where
685678
acc' = intersection acc s
686679
{-# INLINABLE intersections #-}
687-
#endif
688680

689681
-- | @IntSet@s form a 'Semigroup' under 'intersection'.
690682
--

containers/src/Data/Set.hs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,7 @@ module Data.Set (
117117
, difference
118118
, (\\)
119119
, intersection
120-
#if MIN_VERSION_base(4,18,0)
121120
, intersections
122-
#endif
123121
, symmetricDifference
124122
, cartesianProduct
125123
, disjointUnion

containers/src/Data/Set/Internal.hs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,7 @@ module Data.Set.Internal (
155155
, unions
156156
, difference
157157
, intersection
158-
#if MIN_VERSION_base(4,18,0)
159158
, intersections
160-
#endif
161159
, symmetricDifference
162160
, cartesianProduct
163161
, disjointUnion
@@ -250,10 +248,7 @@ import Data.Functor.Classes
250248
import Data.Functor.Identity (Identity)
251249
import qualified Data.Foldable as Foldable
252250
import Control.DeepSeq (NFData(rnf))
253-
#if MIN_VERSION_base(4,18,0)
254-
import qualified Data.Foldable1 as Foldable1
255251
import Data.List.NonEmpty (NonEmpty(..))
256-
#endif
257252

258253
import Utils.Containers.Internal.StrictPair
259254
import Utils.Containers.Internal.PtrEquality
@@ -901,24 +896,21 @@ intersection t1@(Bin _ x l1 r1) t2
901896
{-# INLINABLE intersection #-}
902897
#endif
903898

904-
#if MIN_VERSION_base(4,18,0)
905899
-- | The intersection of a series of sets. Intersections are performed
906900
-- left-to-right.
907901
--
908902
-- @since FIXME
909-
intersections :: (Foldable1.Foldable1 f, Ord a) => f (Set a) -> Set a
910-
intersections ss = case Foldable1.toNonEmpty ss of
911-
s0 :| ss'
912-
| null s0 -> empty
913-
| otherwise -> List.foldr go id ss' s0
903+
intersections :: Ord a => NonEmpty (Set a) -> Set a
904+
intersections (s0 :| ss)
905+
| null s0 = empty
906+
| otherwise = List.foldr go id ss s0
914907
where
915908
go s r acc
916909
| null acc' = empty
917910
| otherwise = r acc'
918911
where
919912
acc' = intersection acc s
920913
{-# INLINABLE intersections #-}
921-
#endif
922914

923915
-- | @Set@s form a 'Semigroup' under 'intersection'.
924916
--

0 commit comments

Comments
 (0)