Skip to content

Commit 3117213

Browse files
authored
Implement Seq folds as coerced FingerTree folds (#1076)
* foldr1 and foldl1 are now implemented as coercions. * Other folds were implemented using coercions in a complicated manner, which is now simplified. GHC is smart enough to compile these to the same Core, so there is no change in runtime behavior.
1 parent 89b39f6 commit 3117213

File tree

4 files changed

+30
-65
lines changed

4 files changed

+30
-65
lines changed

containers-tests/containers-tests.cabal

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ library
116116

117117
other-modules:
118118
Utils.Containers.Internal.Prelude
119-
Utils.Containers.Internal.Coercions
120119
Utils.Containers.Internal.PtrEquality
121120
Utils.Containers.Internal.State
122121
Utils.Containers.Internal.StrictMaybe

containers/containers.cabal

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ Library
8181
Utils.Containers.Internal.State
8282
Utils.Containers.Internal.StrictMaybe
8383
Utils.Containers.Internal.PtrEquality
84-
Utils.Containers.Internal.Coercions
8584
Utils.Containers.Internal.EqOrdUtil
8685
if impl(ghc)
8786
other-modules:

containers/src/Data/Sequence/Internal.hs

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
{-# LANGUAGE DeriveLift #-}
88
{-# LANGUAGE StandaloneDeriving #-}
99
{-# LANGUAGE FlexibleInstances #-}
10+
{-# LANGUAGE InstanceSigs #-}
1011
{-# LANGUAGE ScopedTypeVariables #-}
1112
{-# LANGUAGE TemplateHaskellQuotes #-}
1213
{-# LANGUAGE Trustworthy #-}
@@ -244,8 +245,6 @@ import qualified Data.List
244245
import Data.Array (Ix, Array)
245246
import qualified Data.Array
246247

247-
import Utils.Containers.Internal.Coercions ((.#), (.^#))
248-
249248
import Data.Functor.Identity (Identity(..))
250249

251250
import Utils.Containers.Internal.StrictPair (StrictPair (..), toPair)
@@ -395,33 +394,45 @@ fmapSeq f (Seq xs) = Seq (fmap (fmap f) xs)
395394
#-}
396395
#endif
397396

398-
getSeq :: Seq a -> FingerTree (Elem a)
399-
getSeq (Seq xs) = xs
400-
401397
instance Foldable Seq where
402-
foldMap f = foldMap (f .# getElem) .# getSeq
403-
foldr f z = foldr (f .# getElem) z .# getSeq
404-
foldl f z = foldl (f .^# getElem) z .# getSeq
398+
#ifdef __GLASGOW_HASKELL__
399+
foldMap :: forall m a. Monoid m => (a -> m) -> Seq a -> m
400+
foldMap = coerce (foldMap :: (Elem a -> m) -> FingerTree (Elem a) -> m)
405401

406-
#if __GLASGOW_HASKELL__
407-
{-# INLINABLE foldMap #-}
408-
{-# INLINABLE foldr #-}
409-
{-# INLINABLE foldl #-}
410-
#endif
402+
foldr :: forall a b. (a -> b -> b) -> b -> Seq a -> b
403+
foldr = coerce (foldr :: (Elem a -> b -> b) -> b -> FingerTree (Elem a) -> b)
411404

412-
foldr' f z = foldr' (f .# getElem) z .# getSeq
413-
foldl' f z = foldl' (f .^# getElem) z .# getSeq
405+
foldl :: forall b a. (b -> a -> b) -> b -> Seq a -> b
406+
foldl = coerce (foldl :: (b -> Elem a -> b) -> b -> FingerTree (Elem a) -> b)
414407

415-
#if __GLASGOW_HASKELL__
416-
{-# INLINABLE foldr' #-}
417-
{-# INLINABLE foldl' #-}
418-
#endif
408+
foldr' :: forall a b. (a -> b -> b) -> b -> Seq a -> b
409+
foldr' = coerce (foldr' :: (Elem a -> b -> b) -> b -> FingerTree (Elem a) -> b)
410+
411+
foldl' :: forall b a. (b -> a -> b) -> b -> Seq a -> b
412+
foldl' = coerce (foldl' :: (b -> Elem a -> b) -> b -> FingerTree (Elem a) -> b)
413+
414+
foldr1 :: forall a. (a -> a -> a) -> Seq a -> a
415+
foldr1 = coerce (foldr1 :: (Elem a -> Elem a -> Elem a) -> FingerTree (Elem a) -> Elem a)
416+
417+
foldl1 :: forall a. (a -> a -> a) -> Seq a -> a
418+
foldl1 = coerce (foldl1 :: (Elem a -> Elem a -> Elem a) -> FingerTree (Elem a) -> Elem a)
419+
#else
420+
foldMap f (Seq xs) = foldMap (f . getElem) xs
421+
422+
foldr f z (Seq xs) = foldr (f . getElem) z xs
423+
424+
foldl f z (Seq xs) = foldl (\z' x -> f z' (getElem x)) z xs
425+
426+
foldr' f z (Seq xs) = foldr' (f . getElem) z xs
427+
428+
foldl' f z (Seq xs) = foldl' (\z' x -> f z' (getElem x)) z xs
419429

420430
foldr1 f (Seq xs) = getElem (foldr1 f' xs)
421431
where f' (Elem x) (Elem y) = Elem (f x y)
422432

423433
foldl1 f (Seq xs) = getElem (foldl1 f' xs)
424434
where f' (Elem x) (Elem y) = Elem (f x y)
435+
#endif
425436

426437
length = length
427438
{-# INLINE length #-}

containers/src/Utils/Containers/Internal/Coercions.hs

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

0 commit comments

Comments
 (0)