Skip to content

Commit e0cfcea

Browse files
committed
Define Semigroup instances for base>=4.9
See ekmett/semigroups#56 for more details
1 parent ce65000 commit e0cfcea

File tree

5 files changed

+59
-4
lines changed

5 files changed

+59
-4
lines changed

Data/IntMap/Base.hs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,9 @@ import Data.Monoid (Monoid(..))
222222
import Data.Traversable (Traversable(traverse))
223223
import Data.Word (Word)
224224
#endif
225+
#if MIN_VERSION_base(4,9,0)
226+
import Data.Semigroup (Semigroup((<>), stimes), stimesIdempotentMonoid)
227+
#endif
225228

226229
import Control.DeepSeq (NFData(rnf))
227230
import Control.Monad (liftM)
@@ -305,8 +308,16 @@ infixl 9 \\{-This comment teaches CPP correct behaviour -}
305308

306309
instance Monoid (IntMap a) where
307310
mempty = empty
308-
mappend = union
309311
mconcat = unions
312+
#if !(MIN_VERSION_base(4,9,0))
313+
mappend = union
314+
#else
315+
mappend = (<>)
316+
317+
instance Semigroup (IntMap a) where
318+
(<>) = union
319+
stimes = stimesIdempotentMonoid
320+
#endif
310321

311322
instance Foldable.Foldable IntMap where
312323
fold = go

Data/IntSet/Base.hs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,9 @@ import Data.Maybe (fromMaybe)
173173
import Data.Monoid (Monoid(..))
174174
import Data.Word (Word)
175175
#endif
176+
#if MIN_VERSION_base(4,9,0)
177+
import Data.Semigroup (Semigroup((<>), stimes), stimesIdempotentMonoid)
178+
#endif
176179
import Data.Typeable
177180
import Prelude hiding (filter, foldr, foldl, null, map)
178181

@@ -247,8 +250,16 @@ type Key = Int
247250

248251
instance Monoid IntSet where
249252
mempty = empty
250-
mappend = union
251253
mconcat = unions
254+
#if !(MIN_VERSION_base(4,9,0))
255+
mappend = union
256+
#else
257+
mappend = (<>)
258+
259+
instance Semigroup IntSet where
260+
(<>) = union
261+
stimes = stimesIdempotentMonoid
262+
#endif
252263

253264
#if __GLASGOW_HASKELL__
254265

Data/Map/Base.hs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,9 @@ import Control.Applicative (Applicative(..), (<$>))
275275
import Data.Monoid (Monoid(..))
276276
import Data.Traversable (Traversable(traverse))
277277
#endif
278+
#if MIN_VERSION_base(4,9,0)
279+
import Data.Semigroup (Semigroup((<>), stimes), stimesIdempotentMonoid)
280+
#endif
278281

279282
import Control.DeepSeq (NFData(rnf))
280283
import Data.Bits (shiftL, shiftR)
@@ -340,8 +343,16 @@ type role Map nominal representational
340343

341344
instance (Ord k) => Monoid (Map k v) where
342345
mempty = empty
343-
mappend = union
344346
mconcat = unions
347+
#if !(MIN_VERSION_base(4,9,0))
348+
mappend = union
349+
#else
350+
mappend = (<>)
351+
352+
instance (Ord k) => Semigroup (Map k v) where
353+
(<>) = union
354+
stimes = stimesIdempotentMonoid
355+
#endif
345356

346357
#if __GLASGOW_HASKELL__
347358

Data/Sequence.hs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,9 @@ import Data.Foldable (Foldable(foldl, foldl1, foldr, foldr1, foldMap), foldl', t
175175
#if MIN_VERSION_base(4,8,0)
176176
import Data.Foldable (foldr')
177177
#endif
178+
#if MIN_VERSION_base(4,9,0)
179+
import Data.Semigroup (Semigroup((<>)))
180+
#endif
178181
import Data.Traversable
179182
import Data.Typeable
180183

@@ -543,7 +546,14 @@ instance Read a => Read (Seq a) where
543546

544547
instance Monoid (Seq a) where
545548
mempty = empty
549+
#if !(MIN_VERSION_base(4,9,0))
546550
mappend = (><)
551+
#else
552+
mappend = (<>)
553+
554+
instance Semigroup (Seq a) where
555+
(<>) = (><)
556+
#endif
547557

548558
INSTANCE_TYPEABLE1(Seq,seqTc,"Seq")
549559

Data/Set/Base.hs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,9 @@ import Data.Bits (shiftL, shiftR)
199199
#if !MIN_VERSION_base(4,8,0)
200200
import Data.Monoid (Monoid(..))
201201
#endif
202+
#if MIN_VERSION_base(4,9,0)
203+
import Data.Semigroup (Semigroup((<>), stimes), stimesIdempotentMonoid)
204+
#endif
202205
import qualified Data.Foldable as Foldable
203206
import Data.Typeable
204207
import Control.DeepSeq (NFData(rnf))
@@ -245,8 +248,17 @@ type role Set nominal
245248

246249
instance Ord a => Monoid (Set a) where
247250
mempty = empty
248-
mappend = union
249251
mconcat = unions
252+
#if !(MIN_VERSION_base(4,9,0))
253+
mappend = union
254+
#else
255+
mappend = (<>)
256+
257+
instance Ord a => Semigroup (Set a) where
258+
(<>) = union
259+
stimes = stimesIdempotentMonoid
260+
#endif
261+
250262

251263
instance Foldable.Foldable Set where
252264
fold = go

0 commit comments

Comments
 (0)