summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoe Hermaszewski <git@monoid.al>2020-11-27 16:28:09 +0800
committerMarge Bot <ben+marge-bot@smart-cactus.org>2020-12-22 10:38:24 -0500
commit932ee6de679faa54840a3902d528f12ccfe44d16 (patch)
tree6072d57829dfd0fea81ad696d8b68666c615d46b
parent293100ad15e07733bdd9a69922dc84f77a334e58 (diff)
downloadhaskell-932ee6de679faa54840a3902d528f12ccfe44d16.tar.gz
Add Monoid instances for Product and Compose
Semigroup too of course
-rw-r--r--libraries/base/Data/Functor/Compose.hs3
-rw-r--r--libraries/base/Data/Functor/Product.hs8
-rw-r--r--libraries/base/changelog.md3
3 files changed, 14 insertions, 0 deletions
diff --git a/libraries/base/Data/Functor/Compose.hs b/libraries/base/Data/Functor/Compose.hs
index e0879acdad..97ad997691 100644
--- a/libraries/base/Data/Functor/Compose.hs
+++ b/libraries/base/Data/Functor/Compose.hs
@@ -1,6 +1,7 @@
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE GADTs #-}
+{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE Trustworthy #-}
@@ -42,6 +43,8 @@ newtype Compose f g a = Compose { getCompose :: f (g a) }
deriving ( Data -- ^ @since 4.9.0.0
, Generic -- ^ @since 4.9.0.0
, Generic1 -- ^ @since 4.9.0.0
+ , Semigroup -- ^ @since 4.16.0.0
+ , Monoid -- ^ @since 4.16.0.0
)
-- Instances of lifted Prelude classes
diff --git a/libraries/base/Data/Functor/Product.hs b/libraries/base/Data/Functor/Product.hs
index a3678e910e..27e3b3a9f8 100644
--- a/libraries/base/Data/Functor/Product.hs
+++ b/libraries/base/Data/Functor/Product.hs
@@ -124,3 +124,11 @@ instance (MonadFix f, MonadFix g) => MonadFix (Product f g) where
-- | @since 4.9.0.0
instance (MonadZip f, MonadZip g) => MonadZip (Product f g) where
mzipWith f (Pair x1 y1) (Pair x2 y2) = Pair (mzipWith f x1 x2) (mzipWith f y1 y2)
+
+-- | @since 4.16.0.0
+instance (Semigroup (f a), Semigroup (g a)) => Semigroup (Product f g a) where
+ Pair x1 y1 <> Pair x2 y2 = Pair (x1 <> x2) (y1 <> y2)
+
+-- | @since 4.16.0.0
+instance (Monoid (f a), Monoid (g a)) => Monoid (Product f g a) where
+ mempty = Pair mempty mempty
diff --git a/libraries/base/changelog.md b/libraries/base/changelog.md
index 197388a652..95face49e9 100644
--- a/libraries/base/changelog.md
+++ b/libraries/base/changelog.md
@@ -8,6 +8,9 @@
in order to define instances for `Nat`. Also, different instances for `Nat` and `Natural`
won't typecheck anymore.
+ * Add `Semigroup` and `Monoid` instances for `Data.Functor.Product` and
+ `Data.Functor.Compose`.
+
## 4.15.0.0 *TBA*
* `openFile` now calls the `open` system call with an `interruptible` FFI