Skip to content

Commit 5942d91

Browse files
authored
Use NonEmpty in intersections (#1052)
Using Foldable1 requires a conditional export at the moment which we want to avoid, going by the PVP FAQ. We can use Foldable1 once it is available is all base versions we support.
1 parent e8dbba8 commit 5942d91

File tree

6 files changed

+17
-51
lines changed

6 files changed

+17
-51
lines changed

containers-tests/tests/intset-properties.hs

Lines changed: 4 additions & 11 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', foldMap)
@@ -88,10 +85,8 @@ main = defaultMain $ testGroup "intset-properties"
8885
, testProperty "prop_bitcount" prop_bitcount
8986
, testProperty "prop_alterF_list" prop_alterF_list
9087
, testProperty "prop_alterF_const" prop_alterF_const
91-
#if MIN_VERSION_base(4,18,0)
9288
, testProperty "intersections" prop_intersections
9389
, testProperty "intersections_lazy" prop_intersections_lazy
94-
#endif
9590
]
9691

9792
----------------------------------------------------------------
@@ -514,17 +509,15 @@ prop_alterF_const f k s =
514509
getConst (alterF (Const . applyFun f) k s )
515510
=== getConst (Set.alterF (Const . applyFun f) k (toSet s))
516511

517-
#if MIN_VERSION_base(4,18,0)
518512
prop_intersections :: (IntSet, [IntSet]) -> Property
519513
prop_intersections (s, ss) =
520-
intersections ss' === Foldable1.foldl1' intersection ss'
514+
intersections ss' === List.foldl' intersection s ss
521515
where
522516
ss' = s :| ss -- Work around missing Arbitrary NonEmpty instance
523517

524518
prop_intersections_lazy :: [IntSet] -> Property
525519
prop_intersections_lazy ss = intersections ss' === empty
526520
where
527-
ss' = NE.fromList $ ss ++ [empty] ++ undefined
528-
-- ^ result will certainly be empty at this point,
529-
-- so the rest of the list should not be demanded.
530-
#endif
521+
ss' = NE.fromList $ ss ++ [empty] ++ error "too strict"
522+
--- ^ result will certainly be empty at this point,
523+
-- so the rest of the list should not be demanded.

containers-tests/tests/set-properties.hs

Lines changed: 4 additions & 11 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,17 +743,15 @@ 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

758752
prop_intersections_lazy :: [Set Int] -> Property
759753
prop_intersections_lazy ss = intersections ss' === empty
760754
where
761-
ss' = NE.fromList $ ss ++ [empty] ++ undefined
762-
-- ^ result will certainly be empty at this point,
763-
-- so the rest of the list should not be demanded.
764-
#endif
755+
ss' = NE.fromList $ ss ++ [empty] ++ error "too strict"
756+
--- ^ result will certainly be empty at this point,
757+
-- so the rest of the list should not be demanded.

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

@@ -196,16 +194,13 @@ import Control.Applicative (Const(..))
196194
import Control.DeepSeq (NFData(rnf))
197195
import Data.Bits
198196
import qualified Data.List as List
197+
import Data.List.NonEmpty (NonEmpty(..))
199198
import Data.Maybe (fromMaybe)
200199
import Data.Semigroup
201200
(Semigroup(stimes), stimesIdempotent, stimesIdempotentMonoid)
202201
#if !(MIN_VERSION_base(4,11,0))
203202
import Data.Semigroup (Semigroup((<>)))
204203
#endif
205-
#if MIN_VERSION_base(4,18,0)
206-
import qualified Data.Foldable1 as Foldable1
207-
import Data.List.NonEmpty (NonEmpty(..))
208-
#endif
209204
import Utils.Containers.Internal.Prelude hiding
210205
(filter, foldr, foldl, foldl', foldMap, null, map)
211206
import Prelude ()
@@ -671,24 +666,21 @@ intersection (Tip kx1 bm1) t2 = intersectBM t2
671666

672667
intersection Nil _ = Nil
673668

674-
#if MIN_VERSION_base(4,18,0)
675669
-- | The intersection of a series of sets. Intersections are performed
676670
-- left-to-right.
677671
--
678672
-- @since FIXME
679-
intersections :: Foldable1.Foldable1 f => f IntSet -> IntSet
680-
intersections ss = case Foldable1.toNonEmpty ss of
681-
s0 :| ss'
682-
| null s0 -> empty
683-
| otherwise -> List.foldr go id ss' s0
673+
intersections :: NonEmpty IntSet -> IntSet
674+
intersections (s0 :| ss)
675+
| null s0 = empty
676+
| otherwise = List.foldr go id ss s0
684677
where
685678
go s r acc
686679
| null acc' = empty
687680
| otherwise = r acc'
688681
where
689682
acc' = intersection acc s
690683
{-# INLINABLE intersections #-}
691-
#endif
692684

693685
-- | @IntSet@s form a 'Semigroup' under 'intersection'.
694686
--

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
@@ -904,24 +899,21 @@ intersection t1@(Bin _ x l1 r1) t2
904899
{-# INLINABLE intersection #-}
905900
#endif
906901

907-
#if MIN_VERSION_base(4,18,0)
908902
-- | The intersection of a series of sets. Intersections are performed
909903
-- left-to-right.
910904
--
911905
-- @since FIXME
912-
intersections :: (Foldable1.Foldable1 f, Ord a) => f (Set a) -> Set a
913-
intersections ss = case Foldable1.toNonEmpty ss of
914-
s0 :| ss'
915-
| null s0 -> empty
916-
| otherwise -> List.foldr go id ss' s0
906+
intersections :: Ord a => NonEmpty (Set a) -> Set a
907+
intersections (s0 :| ss)
908+
| null s0 = empty
909+
| otherwise = List.foldr go id ss s0
917910
where
918911
go s r acc
919912
| null acc' = empty
920913
| otherwise = r acc'
921914
where
922915
acc' = intersection acc s
923916
{-# INLINABLE intersections #-}
924-
#endif
925917

926918
-- | @Set@s form a 'Semigroup' under 'intersection'.
927919
--

0 commit comments

Comments
 (0)