summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Gamari <ben@smart-cactus.org>2017-08-14 20:58:16 -0400
committerBen Gamari <ben@smart-cactus.org>2017-08-14 21:32:17 -0400
commit441c52de0621ac68a2248cf691b4de31fba48a34 (patch)
treebe97549925f8ab6d8670423ad89992aaf3021dc9
parenta520adcce27908c799b64214618cf9b33572dc37 (diff)
downloadhaskell-441c52de0621ac68a2248cf691b4de31fba48a34.tar.gz
Add Semigroup/Monoid instances to ST monad
Fixes #14107. Signed-off-by: Philipp Middendorf <middendorf@plapadoo.de> Reviewers: austin, hvr, bgamari, RyanGlScott Reviewed By: bgamari Subscribers: RyanGlScott, rwbarton, thomie GHC Trac Issues: #14107 Differential Revision: https://phabricator.haskell.org/D3845
-rw-r--r--libraries/base/Data/Semigroup.hs5
-rw-r--r--libraries/base/GHC/ST.hs5
-rw-r--r--libraries/base/changelog.md2
3 files changed, 12 insertions, 0 deletions
diff --git a/libraries/base/Data/Semigroup.hs b/libraries/base/Data/Semigroup.hs
index fae207ef97..8631b117be 100644
--- a/libraries/base/Data/Semigroup.hs
+++ b/libraries/base/Data/Semigroup.hs
@@ -72,6 +72,7 @@ import Prelude hiding (foldr1)
import Control.Applicative
import Control.Monad
import Control.Monad.Fix
+import Control.Monad.ST(ST)
import Data.Bifoldable
import Data.Bifunctor
import Data.Bitraversable
@@ -725,6 +726,10 @@ instance Semigroup (Proxy s) where
instance Semigroup a => Semigroup (IO a) where
(<>) = liftA2 (<>)
+-- | @since 4.11.0.0
+instance Semigroup a => Semigroup (ST s a) where
+ (<>) = liftA2 (<>)
+
#if !defined(mingw32_HOST_OS)
-- | @since 4.10.0.0
instance Semigroup Event where
diff --git a/libraries/base/GHC/ST.hs b/libraries/base/GHC/ST.hs
index 4e00c0e85f..a245b9fc50 100644
--- a/libraries/base/GHC/ST.hs
+++ b/libraries/base/GHC/ST.hs
@@ -77,6 +77,11 @@ instance Monad (ST s) where
case (k r) of { ST k2 ->
(k2 new_s) }})
+-- | @since 4.11.0.0
+instance Monoid a => Monoid (ST s a) where
+ mempty = pure mempty
+ mappend = liftA2 mappend
+
data STret s a = STret (State# s) a
-- liftST is useful when we want a lifted result from an ST computation. See
diff --git a/libraries/base/changelog.md b/libraries/base/changelog.md
index 708676fe65..ab304a317f 100644
--- a/libraries/base/changelog.md
+++ b/libraries/base/changelog.md
@@ -14,6 +14,8 @@
* Remove the deprecated `Typeable{1..7}` type synonyms (#14047)
+ * Add instances `Semigroup` and `Monoid` for `Control.Monad.ST` (#14107).
+
## 4.10.0.0 *April 2017*
* Bundled with GHC *TBA*