Skip to content

Commit 6405653

Browse files
committed
Merge pull request #171 from hvr/pr/semigroups
Semigroup instances
2 parents ce65000 + f9e530a commit 6405653

File tree

5 files changed

+68
-13
lines changed

5 files changed

+68
-13
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: 19 additions & 9 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

@@ -2449,28 +2459,28 @@ unrollPQ cmp = unrollPQ'
24492459
where
24502460
{-# INLINE unrollPQ' #-}
24512461
unrollPQ' (PQueue x ts) = x:mergePQs0 ts
2452-
(<>) = mergePQ cmp
2462+
(<+>) = mergePQ cmp
24532463
mergePQs0 Nil = []
24542464
mergePQs0 (t :& Nil) = unrollPQ' t
2455-
mergePQs0 (t1 :& t2 :& ts) = mergePQs (t1 <> t2) ts
2465+
mergePQs0 (t1 :& t2 :& ts) = mergePQs (t1 <+> t2) ts
24562466
mergePQs t ts = t `seq` case ts of
24572467
Nil -> unrollPQ' t
2458-
t1 :& Nil -> unrollPQ' (t <> t1)
2459-
t1 :& t2 :& ts' -> mergePQs (t <> (t1 <> t2)) ts'
2468+
t1 :& Nil -> unrollPQ' (t <+> t1)
2469+
t1 :& t2 :& ts' -> mergePQs (t <+> (t1 <+> t2)) ts'
24602470

24612471
-- | 'toPQ', given an ordering function and a mechanism for queueifying
24622472
-- elements, converts a 'FingerTree' to a 'PQueue'.
24632473
toPQ :: (e -> e -> Ordering) -> (a -> PQueue e) -> FingerTree a -> Maybe (PQueue e)
24642474
toPQ _ _ Empty = Nothing
24652475
toPQ _ f (Single x) = Just (f x)
2466-
toPQ cmp f (Deep _ pr m sf) = Just (maybe (pr' <> sf') ((pr' <> sf') <>) (toPQ cmp fNode m))
2476+
toPQ cmp f (Deep _ pr m sf) = Just (maybe (pr' <+> sf') ((pr' <+> sf') <+>) (toPQ cmp fNode m))
24672477
where
24682478
fDigit digit = case fmap f digit of
24692479
One a -> a
2470-
Two a b -> a <> b
2471-
Three a b c -> a <> b <> c
2472-
Four a b c d -> (a <> b) <> (c <> d)
2473-
(<>) = mergePQ cmp
2480+
Two a b -> a <+> b
2481+
Three a b c -> a <+> b <+> c
2482+
Four a b c d -> (a <+> b) <+> (c <+> d)
2483+
(<+>) = mergePQ cmp
24742484
fNode = fDigit . nodeToDigit
24752485
pr' = fDigit pr
24762486
sf' = fDigit sf

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)