[Git][ghc/ghc][wip/sconcat-first] Add specialized sconcat implementation for Data.Monoid.First and Data.Semigroup.First
Jade (@Jade)
gitlab at gitlab.haskell.org
Fri Feb 16 22:06:20 UTC 2024
Jade pushed to branch wip/sconcat-first at Glasgow Haskell Compiler / GHC
Commits:
f955a5dd by Jade at 2024-02-16T23:10:13+01:00
Add specialized sconcat implementation for Data.Monoid.First and Data.Semigroup.First
Approved CLC Proposal: https://github.com/haskell/core-libraries-committee/issues/246
Fixes: #24346
- - - - -
8 changed files:
- libraries/base/changelog.md
- libraries/base/src/Data/Monoid.hs
- libraries/base/src/Data/Semigroup.hs
- + testsuite/tests/lib/base/First-Monoid-sconcat.hs
- + testsuite/tests/lib/base/First-Monoid-sconcat.stdout
- + testsuite/tests/lib/base/First-Semigroup-sconcat.hs
- + testsuite/tests/lib/base/First-Semigroup-sconcat.stdout
- testsuite/tests/lib/base/all.T
Changes:
=====================================
libraries/base/changelog.md
=====================================
@@ -5,6 +5,7 @@
* Add `permutations` and `permutations1` to `Data.List.NonEmpty` ([CLC proposal #68](https://github.com/haskell/core-libraries-committee/issues/68))
* Add a `RULE` to `Prelude.lookup`, allowing it to participate in list fusion ([CLC proposal #175](https://github.com/haskell/core-libraries-committee/issues/175))
* Implement `stimes` for `instance Semigroup (Endo a)` explicitly ([CLC proposal #4](https://github.com/haskell/core-libraries-committee/issues/4))
+ * Implement `sconcat` for `instance Semigroup Data.Semigroup.First` and `instance Semigroup Data.Monoid.First` explicitly, increasing laziness ([CLC proposal #246](https://github.com/haskell/core-libraries-committee/issues/246))
* Add laws relating between `Foldable` / `Traversable` with `Bifoldable` / `Bitraversable` ([CLC proposal #205](https://github.com/haskell/core-libraries-committee/issues/205))
* The `Enum Int64` and `Enum Word64` instances now use native operations on 32-bit platforms, increasing performance by up to 1.5x on i386 and up to 5.6x with the JavaScript backend. ([CLC proposal #187](https://github.com/haskell/core-libraries-committee/issues/187))
* Update to [Unicode 15.1.0](https://www.unicode.org/versions/Unicode15.1.0/).
=====================================
libraries/base/src/Data/Monoid.hs
=====================================
@@ -156,6 +156,10 @@ instance Semigroup (First a) where
First Nothing <> b = b
a <> _ = a
stimes = stimesIdempotentMonoid
+ sconcat (first@(First m) :| rest)
+ | Nothing <- m = mconcat rest
+ | otherwise = first
+
-- | @since 2.01
instance Monoid (First a) where
=====================================
libraries/base/src/Data/Semigroup.hs
=====================================
@@ -114,6 +114,7 @@ import Data.Bitraversable
import Data.Coerce
import Data.Data
import GHC.Generics
+import Data.List.NonEmpty (NonEmpty (..))
import qualified GHC.List as List
-- $setup
@@ -455,6 +456,7 @@ instance Enum a => Enum (First a) where
instance Semigroup (First a) where
a <> _ = a
stimes = stimesIdempotent
+ sconcat (x :| _) = x
-- | @since 4.9.0.0
instance Functor First where
=====================================
testsuite/tests/lib/base/First-Monoid-sconcat.hs
=====================================
@@ -0,0 +1,10 @@
+module Main where
+
+import Data.Monoid (First(..))
+import Data.Semigroup (sconcat)
+import Data.List.NonEmpty (NonEmpty(..))
+
+main = do
+ print . sconcat $ First Nothing :| First (Just 1) : undefined
+ print . sconcat $ First (Just 2) :| undefined
+ print . sconcat $ First Nothing :| First Nothing : First (Just 3) : []
=====================================
testsuite/tests/lib/base/First-Monoid-sconcat.stdout
=====================================
@@ -0,0 +1,3 @@
+First {getFirst = Just 1}
+First {getFirst = Just 2}
+First {getFirst = Just 3}
=====================================
testsuite/tests/lib/base/First-Semigroup-sconcat.hs
=====================================
@@ -0,0 +1,8 @@
+module Main where
+
+import Data.Semigroup (sconcat, First(..))
+import Data.List.NonEmpty (NonEmpty(..))
+
+main = do
+ print . sconcat $ First 1 :| undefined
+ print . sconcat $ First 1 :| First 2 : []
=====================================
testsuite/tests/lib/base/First-Semigroup-sconcat.stdout
=====================================
@@ -0,0 +1,2 @@
+First {getFirst = 1}
+First {getFirst = 1}
=====================================
testsuite/tests/lib/base/all.T
=====================================
@@ -9,3 +9,5 @@ test('T17472', normal, compile_and_run, [''])
test('T19569b', omit_ghci, compile_and_run, [''])
test('Monoid_ByteArray', normal, compile_and_run, [''])
test('Unsnoc', normal, compile_and_run, [''])
+test('First-Semigroup-sconcat', normal, compile_and_run, [''])
+test('First-Monoid-sconcat', normal, compile_and_run, [''])
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/f955a5ddbd08a6e4149b9349bfab2de070dacf46
--
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/f955a5ddbd08a6e4149b9349bfab2de070dacf46
You're receiving this email because of your account on gitlab.haskell.org.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-commits/attachments/20240216/2c3c7815/attachment-0001.html>
More information about the ghc-commits
mailing list