Skip to content

Commit 9779154

Browse files
authored
Merge pull request #149 from phadej/lifted
Data.Functor.Classes instances
2 parents 71c1a69 + 762916f commit 9779154

File tree

6 files changed

+87
-21
lines changed

6 files changed

+87
-21
lines changed

Data/Vector.hs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,10 @@ import Prelude hiding ( length, null,
191191
enumFromTo, enumFromThenTo,
192192
mapM, mapM_, sequence, sequence_ )
193193

194+
#if MIN_VERSION_base(4,9,0)
195+
import Data.Functor.Classes (Eq1 (..), Ord1 (..), Read1 (..), Show1 (..))
196+
#endif
197+
194198
import Data.Typeable ( Typeable )
195199
import Data.Data ( Data(..) )
196200
import Text.Read ( Read(..), readListPrecDefault )
@@ -228,6 +232,14 @@ instance Read a => Read (Vector a) where
228232
readPrec = G.readPrec
229233
readListPrec = readListPrecDefault
230234

235+
#if MIN_VERSION_base(4,9,0)
236+
instance Show1 Vector where
237+
liftShowsPrec = G.liftShowsPrec
238+
239+
instance Read1 Vector where
240+
liftReadsPrec = G.liftReadsPrec
241+
#endif
242+
231243
#if __GLASGOW_HASKELL__ >= 708
232244

233245
instance Exts.IsList (Vector a) where
@@ -293,6 +305,14 @@ instance Ord a => Ord (Vector a) where
293305
{-# INLINE (>=) #-}
294306
xs >= ys = Bundle.cmp (G.stream xs) (G.stream ys) /= LT
295307

308+
#if MIN_VERSION_base(4,9,0)
309+
instance Eq1 Vector where
310+
liftEq eq xs ys = Bundle.eqBy eq (G.stream xs) (G.stream ys)
311+
312+
instance Ord1 Vector where
313+
liftCompare cmp xs ys = Bundle.cmpBy cmp (G.stream xs) (G.stream ys)
314+
#endif
315+
296316
instance Semigroup (Vector a) where
297317
{-# INLINE (<>) #-}
298318
(<>) = (++)

Data/Vector/Fusion/Bundle.hs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ module Data.Vector.Fusion.Bundle (
7373
-- * Monadic combinators
7474
mapM, mapM_, zipWithM, zipWithM_, filterM, foldM, fold1M, foldM', fold1M',
7575

76-
eq, cmp
76+
eq, cmp, eqBy, cmpBy
7777
) where
7878

7979
import Data.Vector.Generic.Base ( Vector )
@@ -98,6 +98,10 @@ import Prelude hiding ( length, null,
9898
enumFromTo, enumFromThenTo,
9999
mapM, mapM_ )
100100

101+
#if MIN_VERSION_base(4,9,0)
102+
import Data.Functor.Classes (Eq1 (..), Ord1 (..))
103+
#endif
104+
101105
import GHC.Base ( build )
102106

103107
-- Data.Vector.Internal.Check is unused
@@ -486,14 +490,22 @@ scanl1' = M.scanl1'
486490
-- -----------
487491

488492
-- | Check if two 'Bundle's are equal
489-
eq :: Eq a => Bundle v a -> Bundle v a -> Bool
493+
eq :: (Eq a) => Bundle v a -> Bundle v a -> Bool
490494
{-# INLINE eq #-}
491-
eq x y = unId (M.eq x y)
495+
eq = eqBy (==)
496+
497+
eqBy :: (a -> b -> Bool) -> Bundle v a -> Bundle v b -> Bool
498+
{-# INLINE eqBy #-}
499+
eqBy e x y = unId (M.eqBy e x y)
492500

493501
-- | Lexicographically compare two 'Bundle's
494-
cmp :: Ord a => Bundle v a -> Bundle v a -> Ordering
502+
cmp :: (Ord a) => Bundle v a -> Bundle v a -> Ordering
495503
{-# INLINE cmp #-}
496-
cmp x y = unId (M.cmp x y)
504+
cmp = cmpBy compare
505+
506+
cmpBy :: (a -> b -> Ordering) -> Bundle v a -> Bundle v b -> Ordering
507+
{-# INLINE cmpBy #-}
508+
cmpBy c x y = unId (M.cmpBy c x y)
497509

498510
instance Eq a => Eq (M.Bundle Id v a) where
499511
{-# INLINE (==) #-}
@@ -503,6 +515,16 @@ instance Ord a => Ord (M.Bundle Id v a) where
503515
{-# INLINE compare #-}
504516
compare = cmp
505517

518+
#if MIN_VERSION_base(4,9,0)
519+
instance Eq1 (M.Bundle Id v) where
520+
{-# INLINE liftEq #-}
521+
liftEq = eqBy
522+
523+
instance Ord1 (M.Bundle Id v) where
524+
{-# INLINE liftCompare #-}
525+
liftCompare = cmpBy
526+
#endif
527+
506528
-- Monadic combinators
507529
-- -------------------
508530

Data/Vector/Fusion/Bundle/Monadic.hs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ module Data.Vector.Fusion.Bundle.Monadic (
4040
zip, zip3, zip4, zip5, zip6,
4141

4242
-- * Comparisons
43-
eq, cmp,
43+
eqBy, cmpBy,
4444

4545
-- * Filtering
4646
filter, filterM, takeWhile, takeWhileM, dropWhile, dropWhileM,
@@ -424,14 +424,14 @@ zip6 = zipWith6 (,,,,,)
424424
-- -----------
425425

426426
-- | Check if two 'Bundle's are equal
427-
eq :: (Monad m, Eq a) => Bundle m v a -> Bundle m v a -> m Bool
428-
{-# INLINE_FUSED eq #-}
429-
eq x y = sElems x `S.eq` sElems y
427+
eqBy :: (Monad m) => (a -> b -> Bool) -> Bundle m v a -> Bundle m v b -> m Bool
428+
{-# INLINE_FUSED eqBy #-}
429+
eqBy eq x y = S.eqBy eq (sElems x) (sElems y)
430430

431431
-- | Lexicographically compare two 'Bundle's
432-
cmp :: (Monad m, Ord a) => Bundle m v a -> Bundle m v a -> m Ordering
433-
{-# INLINE_FUSED cmp #-}
434-
cmp x y = sElems x `S.cmp` sElems y
432+
cmpBy :: (Monad m) => (a -> b -> Ordering) -> Bundle m v a -> Bundle m v b -> m Ordering
433+
{-# INLINE_FUSED cmpBy #-}
434+
cmpBy cmp x y = S.cmpBy cmp (sElems x) (sElems y)
435435

436436
-- Filtering
437437
-- ---------

Data/Vector/Fusion/Stream/Monadic.hs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ module Data.Vector.Fusion.Stream.Monadic (
3737
zip, zip3, zip4, zip5, zip6,
3838

3939
-- * Comparisons
40-
eq, cmp,
40+
eqBy, cmpBy,
4141

4242
-- * Filtering
4343
filter, filterM, uniq, mapMaybe, takeWhile, takeWhileM, dropWhile, dropWhileM,
@@ -625,9 +625,9 @@ zip6 = zipWith6 (,,,,,)
625625
-- -----------
626626

627627
-- | Check if two 'Stream's are equal
628-
eq :: (Monad m, Eq a) => Stream m a -> Stream m a -> m Bool
629-
{-# INLINE_FUSED eq #-}
630-
eq (Stream step1 t1) (Stream step2 t2) = eq_loop0 SPEC t1 t2
628+
eqBy :: (Monad m) => (a -> b -> Bool) -> Stream m a -> Stream m b -> m Bool
629+
{-# INLINE_FUSED eqBy #-}
630+
eqBy eq (Stream step1 t1) (Stream step2 t2) = eq_loop0 SPEC t1 t2
631631
where
632632
eq_loop0 !_ s1 s2 = do
633633
r <- step1 s1
@@ -640,7 +640,7 @@ eq (Stream step1 t1) (Stream step2 t2) = eq_loop0 SPEC t1 t2
640640
r <- step2 s2
641641
case r of
642642
Yield y s2'
643-
| x == y -> eq_loop0 SPEC s1 s2'
643+
| eq x y -> eq_loop0 SPEC s1 s2'
644644
| otherwise -> return False
645645
Skip s2' -> eq_loop1 SPEC x s1 s2'
646646
Done -> return False
@@ -653,9 +653,9 @@ eq (Stream step1 t1) (Stream step2 t2) = eq_loop0 SPEC t1 t2
653653
Done -> return True
654654

655655
-- | Lexicographically compare two 'Stream's
656-
cmp :: (Monad m, Ord a) => Stream m a -> Stream m a -> m Ordering
657-
{-# INLINE_FUSED cmp #-}
658-
cmp (Stream step1 t1) (Stream step2 t2) = cmp_loop0 SPEC t1 t2
656+
cmpBy :: (Monad m) => (a -> b -> Ordering) -> Stream m a -> Stream m b -> m Ordering
657+
{-# INLINE_FUSED cmpBy #-}
658+
cmpBy cmp (Stream step1 t1) (Stream step2 t2) = cmp_loop0 SPEC t1 t2
659659
where
660660
cmp_loop0 !_ s1 s2 = do
661661
r <- step1 s1
@@ -667,7 +667,7 @@ cmp (Stream step1 t1) (Stream step2 t2) = cmp_loop0 SPEC t1 t2
667667
cmp_loop1 !_ x s1 s2 = do
668668
r <- step2 s2
669669
case r of
670-
Yield y s2' -> case x `compare` y of
670+
Yield y s2' -> case x `cmp` y of
671671
EQ -> cmp_loop0 SPEC s1 s2'
672672
c -> return c
673673
Skip s2' -> cmp_loop1 SPEC x s1 s2'

Data/Vector/Generic.hs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,11 @@ module Data.Vector.Generic (
158158

159159
-- ** Comparisons
160160
eq, cmp,
161+
eqBy, cmpBy,
161162

162163
-- ** Show and Read
163164
showsPrec, readPrec,
165+
liftShowsPrec, liftReadsPrec,
164166

165167
-- ** @Data@ and @Typeable@
166168
gfoldl, dataCast, mkType
@@ -2131,6 +2133,11 @@ eq :: (Vector v a, Eq a) => v a -> v a -> Bool
21312133
{-# INLINE eq #-}
21322134
xs `eq` ys = stream xs == stream ys
21332135

2136+
-- | /O(n)/
2137+
eqBy :: (Vector v a, Vector v b) => (a -> b -> Bool) -> v a -> v b -> Bool
2138+
{-# INLINE eqBy #-}
2139+
eqBy e xs ys = Bundle.eqBy e (stream xs) (stream ys)
2140+
21342141
-- | /O(n)/ Compare two vectors lexicographically. All 'Vector' instances are
21352142
-- also instances of 'Ord' and it is usually more appropriate to use those. This
21362143
-- function is primarily intended for implementing 'Ord' instances for new
@@ -2139,6 +2146,10 @@ cmp :: (Vector v a, Ord a) => v a -> v a -> Ordering
21392146
{-# INLINE cmp #-}
21402147
cmp xs ys = compare (stream xs) (stream ys)
21412148

2149+
-- | /O(n)/
2150+
cmpBy :: (Vector v a, Vector v b) => (a -> b -> Ordering) -> v a -> v b -> Ordering
2151+
cmpBy c xs ys = Bundle.cmpBy c (stream xs) (stream ys)
2152+
21422153
-- Show
21432154
-- ----
21442155

@@ -2147,13 +2158,21 @@ showsPrec :: (Vector v a, Show a) => Int -> v a -> ShowS
21472158
{-# INLINE showsPrec #-}
21482159
showsPrec _ = shows . toList
21492160

2161+
liftShowsPrec :: (Vector v a) => (Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> v a -> ShowS
2162+
{-# INLINE liftShowsPrec #-}
2163+
liftShowsPrec _ s _ = s . toList
2164+
21502165
-- | Generic definition of 'Text.Read.readPrec'
21512166
readPrec :: (Vector v a, Read a) => Read.ReadPrec (v a)
21522167
{-# INLINE readPrec #-}
21532168
readPrec = do
21542169
xs <- Read.readPrec
21552170
return (fromList xs)
21562171

2172+
-- | /Note:/ uses 'ReadS'
2173+
liftReadsPrec :: (Vector v a) => (Int -> Read.ReadS a) -> ReadS [a] -> Int -> Read.ReadS (v a)
2174+
liftReadsPrec _ r _ s = [ (fromList v, s') | (v, s') <- r s ]
2175+
21572176
-- Data and Typeable
21582177
-- -----------------
21592178

changelog

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
Changes in version 0.12.0.0
2+
3+
* Add `Eq1`, `Ord1`, `Show1`, and `Read1` `Vector` instances, and related
4+
helper functions.
5+
16
Changes in version 0.11.0.0
27

38
* Define `Applicative` instances for `Data.Vector.Fusion.Util.{Box,Id}`

0 commit comments

Comments
 (0)