Skip to content

Commit 3dcf821

Browse files
committed
Derive some of the instances
However we can't derive Eq & Ord since DerivingStrategies are only available since 8.2
1 parent 3530f8f commit 3dcf821

File tree

1 file changed

+31
-79
lines changed

1 file changed

+31
-79
lines changed

vector/src/Data/Vector/Strict.hs

Lines changed: 31 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
{-# LANGUAGE TypeFamilies #-}
88
{-# LANGUAGE TypeApplications #-}
99
{-# LANGUAGE ScopedTypeVariables #-}
10+
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
1011
-- |
1112
-- Module : Data.Vector.Strict
1213
-- Copyright : (c) Roman Leshchinskiy 2008-2010
@@ -185,7 +186,7 @@ import Control.DeepSeq ( NFData(rnf)
185186
#endif
186187
)
187188

188-
import Control.Monad ( MonadPlus(..), liftM, ap )
189+
import Control.Monad ( MonadPlus(..), ap )
189190
#if !MIN_VERSION_base(4,13,0)
190191
import Control.Monad (fail)
191192
#endif
@@ -197,9 +198,9 @@ import Control.Monad.Zip
197198
import Data.Function ( fix )
198199

199200
import Prelude
200-
( Eq, Ord, Num, Enum, Monoid, Functor, Monad, Show, Bool, Ordering(..), Int, Maybe, Either
201-
, compare, mempty, mappend, mconcat, return, showsPrec, fmap, otherwise, id, flip, const
202-
, (>>=), (+), (-), (<), (<=), (>), (>=), (==), (/=), (&&), (.), ($), seq ,undefined)
201+
( Eq(..), Ord(..), Num, Enum, Monoid, Functor, Monad, Show, Bool, Ordering(..), Int, Maybe, Either
202+
, return, showsPrec, fmap, otherwise, id, flip, const
203+
, (>>=), (+), (-), (.), ($), seq)
203204

204205
import Data.Functor.Classes (Eq1 (..), Ord1 (..), Read1 (..), Show1 (..))
205206
import Data.Typeable ( Typeable )
@@ -216,7 +217,19 @@ import qualified GHC.Exts as Exts (IsList(..))
216217

217218
-- | Strict boxed vectors, supporting efficient slicing.
218219
newtype Vector a = Vector (V.Vector a)
219-
deriving ( Typeable )
220+
deriving (Typeable, Foldable.Foldable, Semigroup, Monoid)
221+
222+
-- NOTE: [GND for strict vector]
223+
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
224+
--
225+
-- Strict boxed vectors (both mutable an immutable) are newtypes over
226+
-- lazy ones. This makes it possible to use GND to derive instances.
227+
-- However one must take care to preserve strictness since Vector
228+
-- instance for lazy vectors would be used.
229+
--
230+
-- In general it's OK to derive instances where vectors are passed as
231+
-- parameters (e.g. Eq, Ord) and not OK to derive ones where new
232+
-- vector is created (e.g. Read, Functor)
220233

221234
liftRnfV :: (a -> ()) -> Vector a -> ()
222235
liftRnfV elemRnf = foldl' (\_ -> elemRnf) ()
@@ -240,10 +253,10 @@ instance Read a => Read (Vector a) where
240253
readListPrec = readListPrecDefault
241254

242255
instance Show1 Vector where
243-
liftShowsPrec = G.liftShowsPrec
256+
liftShowsPrec = G.liftShowsPrec
244257

245258
instance Read1 Vector where
246-
liftReadsPrec = G.liftReadsPrec
259+
liftReadsPrec = G.liftReadsPrec
247260

248261
instance Exts.IsList (Vector a) where
249262
type Item (Vector a) = a
@@ -276,51 +289,33 @@ instance G.Vector Vector a where
276289
{-# INLINE elemseq #-}
277290
elemseq _ = seq
278291

279-
-- See http://trac.haskell.org/vector/ticket/12
292+
-- See NOTE: [GND for strict vector]
293+
--
294+
-- Deriving strategies are only available since 8.2. So we can't use
295+
-- deriving newtype until we drop support for 8.0
280296
instance Eq a => Eq (Vector a) where
281297
{-# INLINE (==) #-}
282-
xs == ys = Bundle.eq (G.stream xs) (G.stream ys)
298+
(==) = coerce ((==) @(V.Vector a))
283299

284-
-- See http://trac.haskell.org/vector/ticket/12
300+
-- See NOTE: [GND for strict vector]
285301
instance Ord a => Ord (Vector a) where
286302
{-# INLINE compare #-}
287-
compare xs ys = Bundle.cmp (G.stream xs) (G.stream ys)
288-
303+
compare = coerce (compare @(V.Vector a))
289304
{-# INLINE (<) #-}
290-
xs < ys = Bundle.cmp (G.stream xs) (G.stream ys) == LT
291-
305+
(<) = coerce ((<) @(V.Vector a))
292306
{-# INLINE (<=) #-}
293-
xs <= ys = Bundle.cmp (G.stream xs) (G.stream ys) /= GT
294-
307+
(<=) = coerce ((<=) @(V.Vector a))
295308
{-# INLINE (>) #-}
296-
xs > ys = Bundle.cmp (G.stream xs) (G.stream ys) == GT
297-
309+
(>) = coerce ((>) @(V.Vector a))
298310
{-# INLINE (>=) #-}
299-
xs >= ys = Bundle.cmp (G.stream xs) (G.stream ys) /= LT
311+
(>=) = coerce ((>=) @(V.Vector a))
300312

301313
instance Eq1 Vector where
302314
liftEq eq xs ys = Bundle.eqBy eq (G.stream xs) (G.stream ys)
303315

304316
instance Ord1 Vector where
305317
liftCompare cmp xs ys = Bundle.cmpBy cmp (G.stream xs) (G.stream ys)
306318

307-
instance Semigroup (Vector a) where
308-
{-# INLINE (<>) #-}
309-
(<>) = (++)
310-
311-
{-# INLINE sconcat #-}
312-
sconcat = G.concatNE
313-
314-
instance Monoid (Vector a) where
315-
{-# INLINE mempty #-}
316-
mempty = empty
317-
318-
{-# INLINE mappend #-}
319-
mappend = (<>)
320-
321-
{-# INLINE mconcat #-}
322-
mconcat = concat
323-
324319
instance Functor Vector where
325320
{-# INLINE fmap #-}
326321
fmap = map
@@ -400,49 +395,6 @@ instance Applicative.Alternative Vector where
400395
{-# INLINE (<|>) #-}
401396
(<|>) = (++)
402397

403-
instance Foldable.Foldable Vector where
404-
{-# INLINE foldr #-}
405-
foldr = foldr
406-
407-
{-# INLINE foldl #-}
408-
foldl = foldl
409-
410-
{-# INLINE foldr1 #-}
411-
foldr1 = foldr1
412-
413-
{-# INLINE foldl1 #-}
414-
foldl1 = foldl1
415-
416-
{-# INLINE foldr' #-}
417-
foldr' = foldr'
418-
419-
{-# INLINE foldl' #-}
420-
foldl' = foldl'
421-
422-
{-# INLINE toList #-}
423-
toList = toList
424-
425-
{-# INLINE length #-}
426-
length = length
427-
428-
{-# INLINE null #-}
429-
null = null
430-
431-
{-# INLINE elem #-}
432-
elem = elem
433-
434-
{-# INLINE maximum #-}
435-
maximum = maximum
436-
437-
{-# INLINE minimum #-}
438-
minimum = minimum
439-
440-
{-# INLINE sum #-}
441-
sum = sum
442-
443-
{-# INLINE product #-}
444-
product = product
445-
446398
instance Traversable.Traversable Vector where
447399
{-# INLINE traverse #-}
448400
traverse f xs =

0 commit comments

Comments
 (0)