Skip to content

Commit b1a1e2f

Browse files
jwaldmanntreeowl
authored andcommitted
remove foldlStrict, generalize type of unions, see #520 (#524)
1 parent a0395c7 commit b1a1e2f

File tree

8 files changed

+34
-65
lines changed

8 files changed

+34
-65
lines changed

Data/IntMap/Internal.hs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -303,14 +303,14 @@ import Data.Functor.Classes
303303
import Control.DeepSeq (NFData(rnf))
304304
import Data.Bits
305305
import qualified Data.Foldable as Foldable
306+
import Data.Foldable (Foldable())
306307
import Data.Maybe (fromMaybe)
307308
import Data.Typeable
308309
import Prelude hiding (lookup, map, filter, foldr, foldl, null)
309310

310311
import Data.IntSet.Internal (Key)
311312
import qualified Data.IntSet.Internal as IntSet
312313
import Utils.Containers.Internal.BitUtil
313-
import Utils.Containers.Internal.StrictFold
314314
import Utils.Containers.Internal.StrictPair
315315

316316
#if __GLASGOW_HASKELL__
@@ -1003,18 +1003,18 @@ alterF f k m = (<$> f mv) $ \fres ->
10031003
-- > unions [(fromList [(5, "A3"), (3, "B3")]), (fromList [(5, "A"), (7, "C")]), (fromList [(5, "a"), (3, "b")])]
10041004
-- > == fromList [(3, "B3"), (5, "A3"), (7, "C")]
10051005

1006-
unions :: [IntMap a] -> IntMap a
1006+
unions :: Foldable f => f (IntMap a) -> IntMap a
10071007
unions xs
1008-
= foldlStrict union empty xs
1008+
= Foldable.foldl' union empty xs
10091009

10101010
-- | The union of a list of maps, with a combining operation.
10111011
--
10121012
-- > unionsWith (++) [(fromList [(5, "a"), (3, "b")]), (fromList [(5, "A"), (7, "C")]), (fromList [(5, "A3"), (3, "B3")])]
10131013
-- > == fromList [(3, "bB3"), (5, "aAA3"), (7, "C")]
10141014

1015-
unionsWith :: (a->a->a) -> [IntMap a] -> IntMap a
1015+
unionsWith :: Foldable f => (a->a->a) -> f (IntMap a) -> IntMap a
10161016
unionsWith f ts
1017-
= foldlStrict (unionWith f) empty ts
1017+
= Foldable.foldl' (unionWith f) empty ts
10181018

10191019
-- | /O(n+m)/. The (left-biased) union of two maps.
10201020
-- It prefers the first map when duplicate keys are encountered,
@@ -3031,7 +3031,7 @@ foldlFB = foldlWithKey
30313031

30323032
fromList :: [(Key,a)] -> IntMap a
30333033
fromList xs
3034-
= foldlStrict ins empty xs
3034+
= Foldable.foldl' ins empty xs
30353035
where
30363036
ins t (k,x) = insert k x t
30373037

@@ -3052,7 +3052,7 @@ fromListWith f xs
30523052

30533053
fromListWithKey :: (Key -> a -> a -> a) -> [(Key,a)] -> IntMap a
30543054
fromListWithKey f xs
3055-
= foldlStrict ins empty xs
3055+
= Foldable.foldl' ins empty xs
30563056
where
30573057
ins t (k,x) = insertWithKey f k x t
30583058

Data/IntMap/Strict.hs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -334,12 +334,13 @@ import Data.IntMap.Internal
334334
import Data.IntMap.Internal.DeprecatedDebug (showTree, showTreeWith)
335335
import qualified Data.IntSet.Internal as IntSet
336336
import Utils.Containers.Internal.BitUtil
337-
import Utils.Containers.Internal.StrictFold
338337
import Utils.Containers.Internal.StrictPair
339338
#if !MIN_VERSION_base(4,8,0)
340339
import Data.Functor((<$>))
341340
#endif
342341
import Control.Applicative (Applicative (..), liftA2)
342+
import qualified Data.Foldable as Foldable
343+
import Data.Foldable (Foldable())
343344

344345
{--------------------------------------------------------------------
345346
Query
@@ -638,9 +639,9 @@ alterF f k m = (<$> f mv) $ \fres ->
638639
-- > unionsWith (++) [(fromList [(5, "a"), (3, "b")]), (fromList [(5, "A"), (7, "C")]), (fromList [(5, "A3"), (3, "B3")])]
639640
-- > == fromList [(3, "bB3"), (5, "aAA3"), (7, "C")]
640641

641-
unionsWith :: (a->a->a) -> [IntMap a] -> IntMap a
642+
unionsWith :: Foldable f => (a->a->a) -> f (IntMap a) -> IntMap a
642643
unionsWith f ts
643-
= foldlStrict (unionWith f) empty ts
644+
= Foldable.foldl' (unionWith f) empty ts
644645

645646
-- | /O(n+m)/. The union with a combining function.
646647
--
@@ -1049,7 +1050,7 @@ fromSet f (IntSet.Tip kx bm) = buildTree f kx bm (IntSet.suffixBitMask + 1)
10491050

10501051
fromList :: [(Key,a)] -> IntMap a
10511052
fromList xs
1052-
= foldlStrict ins empty xs
1053+
= Foldable.foldl' ins empty xs
10531054
where
10541055
ins t (k,x) = insert k x t
10551056

@@ -1069,7 +1070,7 @@ fromListWith f xs
10691070

10701071
fromListWithKey :: (Key -> a -> a -> a) -> [(Key,a)] -> IntMap a
10711072
fromListWithKey f xs
1072-
= foldlStrict ins empty xs
1073+
= Foldable.foldl' ins empty xs
10731074
where
10741075
ins t (k,x) = insertWithKey f k x t
10751076

Data/IntSet/Internal.hs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,6 @@ import Data.Typeable
201201
import Prelude hiding (filter, foldr, foldl, null, map)
202202

203203
import Utils.Containers.Internal.BitUtil
204-
import Utils.Containers.Internal.StrictFold
205204
import Utils.Containers.Internal.StrictPair
206205

207206
#if __GLASGOW_HASKELL__
@@ -217,6 +216,8 @@ import qualified GHC.Exts as GHCExts
217216
import GHC.Prim (indexInt8OffAddr#)
218217
#endif
219218

219+
import qualified Data.Foldable as Foldable
220+
import Data.Foldable (Foldable())
220221

221222
infixl 9 \\{-This comment teaches CPP correct behaviour -}
222223

@@ -499,9 +500,9 @@ deleteBM _ _ Nil = Nil
499500
Union
500501
--------------------------------------------------------------------}
501502
-- | The union of a list of sets.
502-
unions :: [IntSet] -> IntSet
503+
unions :: Foldable f => f IntSet -> IntSet
503504
unions xs
504-
= foldlStrict union empty xs
505+
= Foldable.foldl' union empty xs
505506

506507

507508
-- | /O(n+m)/. The union of two sets.
@@ -1044,7 +1045,7 @@ foldlFB = foldl
10441045
-- | /O(n*min(n,W))/. Create a set from a list of integers.
10451046
fromList :: [Key] -> IntSet
10461047
fromList xs
1047-
= foldlStrict ins empty xs
1048+
= Foldable.foldl' ins empty xs
10481049
where
10491050
ins t x = insert x t
10501051

Data/Map/Internal.hs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -378,13 +378,13 @@ import Control.Applicative (Const (..))
378378
import Control.DeepSeq (NFData(rnf))
379379
import Data.Bits (shiftL, shiftR)
380380
import qualified Data.Foldable as Foldable
381+
import Data.Foldable (Foldable())
381382
import Data.Typeable
382383
import Prelude hiding (lookup, map, filter, foldr, foldl, null, splitAt, take, drop)
383384

384385
import qualified Data.Set.Internal as Set
385386
import Data.Set.Internal (Set)
386387
import Utils.Containers.Internal.PtrEquality (ptrEq)
387-
import Utils.Containers.Internal.StrictFold
388388
import Utils.Containers.Internal.StrictPair
389389
import Utils.Containers.Internal.StrictMaybe
390390
import Utils.Containers.Internal.BitQueue
@@ -1782,9 +1782,9 @@ maxView t = case maxViewWithKey t of
17821782
-- > unions [(fromList [(5, "A3"), (3, "B3")]), (fromList [(5, "A"), (7, "C")]), (fromList [(5, "a"), (3, "b")])]
17831783
-- > == fromList [(3, "B3"), (5, "A3"), (7, "C")]
17841784

1785-
unions :: Ord k => [Map k a] -> Map k a
1785+
unions :: (Foldable f, Ord k) => f (Map k a) -> Map k a
17861786
unions ts
1787-
= foldlStrict union empty ts
1787+
= Foldable.foldl' union empty ts
17881788
#if __GLASGOW_HASKELL__
17891789
{-# INLINABLE unions #-}
17901790
#endif
@@ -1795,9 +1795,9 @@ unions ts
17951795
-- > unionsWith (++) [(fromList [(5, "a"), (3, "b")]), (fromList [(5, "A"), (7, "C")]), (fromList [(5, "A3"), (3, "B3")])]
17961796
-- > == fromList [(3, "bB3"), (5, "aAA3"), (7, "C")]
17971797

1798-
unionsWith :: Ord k => (a->a->a) -> [Map k a] -> Map k a
1798+
unionsWith :: (Foldable f, Ord k) => (a->a->a) -> f (Map k a) -> Map k a
17991799
unionsWith f ts
1800-
= foldlStrict (unionWith f) empty ts
1800+
= Foldable.foldl' (unionWith f) empty ts
18011801
#if __GLASGOW_HASKELL__
18021802
{-# INLINABLE unionsWith #-}
18031803
#endif
@@ -3348,7 +3348,7 @@ fromList ((kx0, x0) : xs0) | not_ordered kx0 xs0 = fromList' (Bin 1 kx0 x0 Tip T
33483348
not_ordered kx ((ky,_) : _) = kx >= ky
33493349
{-# INLINE not_ordered #-}
33503350

3351-
fromList' t0 xs = foldlStrict ins t0 xs
3351+
fromList' t0 xs = Foldable.foldl' ins t0 xs
33523352
where ins t (k,x) = insert k x t
33533353

33543354
go !_ t [] = t
@@ -3397,7 +3397,7 @@ fromListWith f xs
33973397

33983398
fromListWithKey :: Ord k => (k -> a -> a -> a) -> [(k,a)] -> Map k a
33993399
fromListWithKey f xs
3400-
= foldlStrict ins empty xs
3400+
= Foldable.foldl' ins empty xs
34013401
where
34023402
ins t (k,x) = insertWithKey f k x t
34033403
#if __GLASGOW_HASKELL__

Data/Map/Strict/Internal.hs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,6 @@ import Control.Applicative (Applicative (..), (<$>))
406406
#endif
407407
import qualified Data.Set.Internal as Set
408408
import qualified Data.Map.Internal as L
409-
import Utils.Containers.Internal.StrictFold
410409
import Utils.Containers.Internal.StrictPair
411410

412411
import Data.Bits (shiftL, shiftR)
@@ -418,6 +417,8 @@ import Data.Coerce
418417
import Data.Functor.Identity (Identity (..))
419418
#endif
420419

420+
import qualified Data.Foldable as Foldable
421+
import Data.Foldable (Foldable())
421422

422423
-- $strictness
423424
--
@@ -951,9 +952,9 @@ updateMaxWithKey f (Bin _ kx x l r) = balanceL kx x l (updateMaxWithKey f r)
951952
-- > unionsWith (++) [(fromList [(5, "a"), (3, "b")]), (fromList [(5, "A"), (7, "C")]), (fromList [(5, "A3"), (3, "B3")])]
952953
-- > == fromList [(3, "bB3"), (5, "aAA3"), (7, "C")]
953954

954-
unionsWith :: Ord k => (a->a->a) -> [Map k a] -> Map k a
955+
unionsWith :: (Foldable f, Ord k) => (a->a->a) -> f (Map k a) -> Map k a
955956
unionsWith f ts
956-
= foldlStrict (unionWith f) empty ts
957+
= Foldable.foldl' (unionWith f) empty ts
957958
#if __GLASGOW_HASKELL__
958959
{-# INLINABLE unionsWith #-}
959960
#endif
@@ -1487,7 +1488,7 @@ fromList ((kx0, x0) : xs0) | not_ordered kx0 xs0 = x0 `seq` fromList' (Bin 1 kx0
14871488
not_ordered kx ((ky,_) : _) = kx >= ky
14881489
{-# INLINE not_ordered #-}
14891490

1490-
fromList' t0 xs = foldlStrict ins t0 xs
1491+
fromList' t0 xs = Foldable.foldl' ins t0 xs
14911492
where ins t (k,x) = insert k x t
14921493

14931494
go !_ t [] = t
@@ -1536,7 +1537,7 @@ fromListWith f xs
15361537

15371538
fromListWithKey :: Ord k => (k -> a -> a -> a) -> [(k,a)] -> Map k a
15381539
fromListWithKey f xs
1539-
= foldlStrict ins empty xs
1540+
= Foldable.foldl' ins empty xs
15401541
where
15411542
ins t (k,x) = insertWithKey f k x t
15421543
#if __GLASGOW_HASKELL__

Data/Set/Internal.hs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,6 @@ import Data.Foldable (Foldable (foldMap))
246246
import Data.Typeable
247247
import Control.DeepSeq (NFData(rnf))
248248

249-
import Utils.Containers.Internal.StrictFold
250249
import Utils.Containers.Internal.StrictPair
251250
import Utils.Containers.Internal.PtrEquality
252251

@@ -705,8 +704,8 @@ deleteMax Tip = Tip
705704
Union.
706705
--------------------------------------------------------------------}
707706
-- | The union of a list of sets: (@'unions' == 'foldl' 'union' 'empty'@).
708-
unions :: Ord a => [Set a] -> Set a
709-
unions = foldlStrict union empty
707+
unions :: (Foldable f, Ord a) => f (Set a) -> Set a
708+
unions = Foldable.foldl' union empty
710709
#if __GLASGOW_HASKELL__
711710
{-# INLINABLE unions #-}
712711
#endif
@@ -973,7 +972,7 @@ fromList (x0 : xs0) | not_ordered x0 xs0 = fromList' (Bin 1 x0 Tip Tip) xs0
973972
not_ordered x (y : _) = x >= y
974973
{-# INLINE not_ordered #-}
975974

976-
fromList' t0 xs = foldlStrict ins t0 xs
975+
fromList' t0 xs = Foldable.foldl' ins t0 xs
977976
where ins t x = insert x t
978977

979978
go !_ t [] = t

Utils/Containers/Internal/StrictFold.hs

Lines changed: 0 additions & 20 deletions
This file was deleted.

containers.cabal

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ Library
8282

8383
other-modules:
8484
Utils.Containers.Internal.State
85-
Utils.Containers.Internal.StrictFold
8685
Utils.Containers.Internal.StrictMaybe
8786
Utils.Containers.Internal.PtrEquality
8887
Utils.Containers.Internal.Coercions
@@ -209,7 +208,6 @@ benchmark lookupge-intmap
209208
Data.IntSet.Internal
210209
LookupGE_IntMap
211210
Utils.Containers.Internal.BitUtil
212-
Utils.Containers.Internal.StrictFold
213211
Utils.Containers.Internal.StrictPair
214212
ghc-options: -O2
215213
cpp-options: -DTESTING
@@ -238,7 +236,6 @@ benchmark lookupge-map
238236
Utils.Containers.Internal.BitQueue
239237
Utils.Containers.Internal.BitUtil
240238
Utils.Containers.Internal.PtrEquality
241-
Utils.Containers.Internal.StrictFold
242239
Utils.Containers.Internal.StrictMaybe
243240
Utils.Containers.Internal.StrictPair
244241
ghc-options: -O2
@@ -273,7 +270,6 @@ Test-suite map-lazy-properties
273270
Utils.Containers.Internal.BitQueue
274271
Utils.Containers.Internal.BitUtil
275272
Utils.Containers.Internal.PtrEquality
276-
Utils.Containers.Internal.StrictFold
277273
Utils.Containers.Internal.StrictMaybe
278274
Utils.Containers.Internal.StrictPair
279275
type: exitcode-stdio-1.0
@@ -307,7 +303,6 @@ Test-suite map-strict-properties
307303
Utils.Containers.Internal.BitQueue
308304
Utils.Containers.Internal.BitUtil
309305
Utils.Containers.Internal.PtrEquality
310-
Utils.Containers.Internal.StrictFold
311306
Utils.Containers.Internal.StrictMaybe
312307
Utils.Containers.Internal.StrictPair
313308
type: exitcode-stdio-1.0
@@ -355,7 +350,6 @@ Test-suite set-properties
355350
Data.Set.Internal
356351
Utils.Containers.Internal.BitUtil
357352
Utils.Containers.Internal.PtrEquality
358-
Utils.Containers.Internal.StrictFold
359353
Utils.Containers.Internal.StrictPair
360354
type: exitcode-stdio-1.0
361355
cpp-options: -DTESTING
@@ -385,7 +379,6 @@ Test-suite intmap-lazy-properties
385379
Data.IntSet.Internal
386380
IntMapValidity
387381
Utils.Containers.Internal.BitUtil
388-
Utils.Containers.Internal.StrictFold
389382
Utils.Containers.Internal.StrictPair
390383
type: exitcode-stdio-1.0
391384
cpp-options: -DTESTING
@@ -414,7 +407,6 @@ Test-suite intmap-strict-properties
414407
Data.IntSet.Internal
415408
IntMapValidity
416409
Utils.Containers.Internal.BitUtil
417-
Utils.Containers.Internal.StrictFold
418410
Utils.Containers.Internal.StrictPair
419411
type: exitcode-stdio-1.0
420412
cpp-options: -DTESTING -DSTRICT
@@ -442,7 +434,6 @@ Test-suite intset-properties
442434
IntSetValidity
443435
Utils.Containers.Internal.BitUtil
444436
Utils.Containers.Internal.PtrEquality
445-
Utils.Containers.Internal.StrictFold
446437
Utils.Containers.Internal.StrictPair
447438
type: exitcode-stdio-1.0
448439
cpp-options: -DTESTING
@@ -480,7 +471,6 @@ Test-suite deprecated-properties
480471
Utils.Containers.Internal.BitQueue
481472
Utils.Containers.Internal.BitUtil
482473
Utils.Containers.Internal.PtrEquality
483-
Utils.Containers.Internal.StrictFold
484474
Utils.Containers.Internal.StrictMaybe
485475
Utils.Containers.Internal.StrictPair
486476
type: exitcode-stdio-1.0
@@ -549,7 +539,6 @@ test-suite map-strictness-properties
549539
Utils.Containers.Internal.BitQueue
550540
Utils.Containers.Internal.BitUtil
551541
Utils.Containers.Internal.PtrEquality
552-
Utils.Containers.Internal.StrictFold
553542
Utils.Containers.Internal.StrictMaybe
554543
Utils.Containers.Internal.StrictPair
555544
type: exitcode-stdio-1.0
@@ -577,7 +566,6 @@ test-suite intmap-strictness-properties
577566
Data.IntMap.Strict
578567
Data.IntSet.Internal
579568
Utils.Containers.Internal.BitUtil
580-
Utils.Containers.Internal.StrictFold
581569
Utils.Containers.Internal.StrictPair
582570
type: exitcode-stdio-1.0
583571
other-extensions: CPP, BangPatterns
@@ -602,7 +590,6 @@ test-suite intset-strictness-properties
602590
Data.IntSet
603591
Data.IntSet.Internal
604592
Utils.Containers.Internal.BitUtil
605-
Utils.Containers.Internal.StrictFold
606593
Utils.Containers.Internal.StrictPair
607594
type: exitcode-stdio-1.0
608595
other-extensions: CPP, BangPatterns

0 commit comments

Comments
 (0)