[commit: ghc] master: Add Semigroup/Monoid instances to ST monad (441c52d)

git at git.haskell.org git at git.haskell.org
Tue Aug 15 01:35:50 UTC 2017


Repository : ssh://git@git.haskell.org/ghc

On branch  : master
Link       : http://ghc.haskell.org/trac/ghc/changeset/441c52de0621ac68a2248cf691b4de31fba48a34/ghc

>---------------------------------------------------------------

commit 441c52de0621ac68a2248cf691b4de31fba48a34
Author: Ben Gamari <ben at smart-cactus.org>
Date:   Mon Aug 14 20:58:16 2017 -0400

    Add Semigroup/Monoid instances to ST monad
    
    Fixes #14107.
    
    Signed-off-by: Philipp Middendorf <middendorf at 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


>---------------------------------------------------------------

441c52de0621ac68a2248cf691b4de31fba48a34
 libraries/base/Data/Semigroup.hs | 5 +++++
 libraries/base/GHC/ST.hs         | 5 +++++
 libraries/base/changelog.md      | 2 ++
 3 files changed, 12 insertions(+)

diff --git a/libraries/base/Data/Semigroup.hs b/libraries/base/Data/Semigroup.hs
index fae207e..8631b11 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 4e00c0e..a245b9f 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 708676f..ab304a3 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*
 



More information about the ghc-commits mailing list