Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Seq folds as coerced FingerTree folds #1076

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion containers-tests/containers-tests.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ library

other-modules:
Utils.Containers.Internal.Prelude
Utils.Containers.Internal.Coercions
Utils.Containers.Internal.PtrEquality
Utils.Containers.Internal.State
Utils.Containers.Internal.StrictMaybe
Expand Down
1 change: 0 additions & 1 deletion containers/containers.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ Library
Utils.Containers.Internal.State
Utils.Containers.Internal.StrictMaybe
Utils.Containers.Internal.PtrEquality
Utils.Containers.Internal.Coercions
Utils.Containers.Internal.EqOrdUtil
if impl(ghc)
other-modules:
Expand Down
49 changes: 30 additions & 19 deletions containers/src/Data/Sequence/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
{-# LANGUAGE DeriveLift #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TemplateHaskellQuotes #-}
{-# LANGUAGE Trustworthy #-}
Expand Down Expand Up @@ -244,8 +245,6 @@ import qualified Data.List
import Data.Array (Ix, Array)
import qualified Data.Array

import Utils.Containers.Internal.Coercions ((.#), (.^#))

import Data.Functor.Identity (Identity(..))

import Utils.Containers.Internal.StrictPair (StrictPair (..), toPair)
Expand Down Expand Up @@ -395,33 +394,45 @@ fmapSeq f (Seq xs) = Seq (fmap (fmap f) xs)
#-}
#endif

getSeq :: Seq a -> FingerTree (Elem a)
getSeq (Seq xs) = xs

instance Foldable Seq where
foldMap f = foldMap (f .# getElem) .# getSeq
foldr f z = foldr (f .# getElem) z .# getSeq
foldl f z = foldl (f .^# getElem) z .# getSeq
#ifdef __GLASGOW_HASKELL__
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need foldMap for other implementations too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean? We have foldMap implementations.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Am I missing something? It looks like you just hid it behind an ifdef.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, you mean Haskell implementations, I misunderstood. Those definitions are in the #else part of this #ifdef.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it seem fine, @treeowl?

foldMap :: forall m a. Monoid m => (a -> m) -> Seq a -> m
foldMap = coerce (foldMap :: (Elem a -> m) -> FingerTree (Elem a) -> m)

#if __GLASGOW_HASKELL__
{-# INLINABLE foldMap #-}
{-# INLINABLE foldr #-}
{-# INLINABLE foldl #-}
#endif
foldr :: forall a b. (a -> b -> b) -> b -> Seq a -> b
foldr = coerce (foldr :: (Elem a -> b -> b) -> b -> FingerTree (Elem a) -> b)

foldr' f z = foldr' (f .# getElem) z .# getSeq
foldl' f z = foldl' (f .^# getElem) z .# getSeq
foldl :: forall b a. (b -> a -> b) -> b -> Seq a -> b
foldl = coerce (foldl :: (b -> Elem a -> b) -> b -> FingerTree (Elem a) -> b)

#if __GLASGOW_HASKELL__
{-# INLINABLE foldr' #-}
{-# INLINABLE foldl' #-}
#endif
foldr' :: forall a b. (a -> b -> b) -> b -> Seq a -> b
foldr' = coerce (foldr' :: (Elem a -> b -> b) -> b -> FingerTree (Elem a) -> b)

foldl' :: forall b a. (b -> a -> b) -> b -> Seq a -> b
foldl' = coerce (foldl' :: (b -> Elem a -> b) -> b -> FingerTree (Elem a) -> b)

foldr1 :: forall a. (a -> a -> a) -> Seq a -> a
foldr1 = coerce (foldr1 :: (Elem a -> Elem a -> Elem a) -> FingerTree (Elem a) -> Elem a)

foldl1 :: forall a. (a -> a -> a) -> Seq a -> a
foldl1 = coerce (foldl1 :: (Elem a -> Elem a -> Elem a) -> FingerTree (Elem a) -> Elem a)
#else
foldMap f (Seq xs) = foldMap (f . getElem) xs

foldr f z (Seq xs) = foldr (f . getElem) z xs

foldl f z (Seq xs) = foldl (\z' x -> f z' (getElem x)) z xs

foldr' f z (Seq xs) = foldr' (f . getElem) z xs

foldl' f z (Seq xs) = foldl' (\z' x -> f z' (getElem x)) z xs

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

foldl1 f (Seq xs) = getElem (foldl1 f' xs)
where f' (Elem x) (Elem y) = Elem (f x y)
#endif

length = length
{-# INLINE length #-}
Expand Down
44 changes: 0 additions & 44 deletions containers/src/Utils/Containers/Internal/Coercions.hs

This file was deleted.