7
7
{-# LANGUAGE TypeFamilies #-}
8
8
{-# LANGUAGE TypeApplications #-}
9
9
{-# LANGUAGE ScopedTypeVariables #-}
10
+ {-# LANGUAGE GeneralizedNewtypeDeriving #-}
10
11
-- |
11
12
-- Module : Data.Vector.Strict
12
13
-- Copyright : (c) Roman Leshchinskiy 2008-2010
@@ -185,7 +186,7 @@ import Control.DeepSeq ( NFData(rnf)
185
186
#endif
186
187
)
187
188
188
- import Control.Monad ( MonadPlus (.. ), liftM , ap )
189
+ import Control.Monad ( MonadPlus (.. ), ap )
189
190
#if !MIN_VERSION_base(4,13,0)
190
191
import Control.Monad (fail )
191
192
#endif
@@ -197,9 +198,9 @@ import Control.Monad.Zip
197
198
import Data.Function ( fix )
198
199
199
200
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 )
203
204
204
205
import Data.Functor.Classes (Eq1 (.. ), Ord1 (.. ), Read1 (.. ), Show1 (.. ))
205
206
import Data.Typeable ( Typeable )
@@ -216,7 +217,19 @@ import qualified GHC.Exts as Exts (IsList(..))
216
217
217
218
-- | Strict boxed vectors, supporting efficient slicing.
218
219
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)
220
233
221
234
liftRnfV :: (a -> () ) -> Vector a -> ()
222
235
liftRnfV elemRnf = foldl' (\ _ -> elemRnf) ()
@@ -240,10 +253,10 @@ instance Read a => Read (Vector a) where
240
253
readListPrec = readListPrecDefault
241
254
242
255
instance Show1 Vector where
243
- liftShowsPrec = G. liftShowsPrec
256
+ liftShowsPrec = G. liftShowsPrec
244
257
245
258
instance Read1 Vector where
246
- liftReadsPrec = G. liftReadsPrec
259
+ liftReadsPrec = G. liftReadsPrec
247
260
248
261
instance Exts. IsList (Vector a ) where
249
262
type Item (Vector a ) = a
@@ -276,51 +289,33 @@ instance G.Vector Vector a where
276
289
{-# INLINE elemseq #-}
277
290
elemseq _ = seq
278
291
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
280
296
instance Eq a => Eq (Vector a ) where
281
297
{-# INLINE (==) #-}
282
- xs == ys = Bundle. eq ( G. stream xs) ( G. stream ys )
298
+ (==) = coerce ( (==) @ ( V. Vector a ) )
283
299
284
- -- See http://trac.haskell.org/ vector/ticket/12
300
+ -- See NOTE: [GND for strict vector]
285
301
instance Ord a => Ord (Vector a ) where
286
302
{-# INLINE compare #-}
287
- compare xs ys = Bundle. cmp (G. stream xs) (G. stream ys)
288
-
303
+ compare = coerce (compare @ (V. Vector a ))
289
304
{-# INLINE (<) #-}
290
- xs < ys = Bundle. cmp (G. stream xs) (G. stream ys) == LT
291
-
305
+ (<) = coerce ((<) @ (V. Vector a ))
292
306
{-# INLINE (<=) #-}
293
- xs <= ys = Bundle. cmp (G. stream xs) (G. stream ys) /= GT
294
-
307
+ (<=) = coerce ((<=) @ (V. Vector a ))
295
308
{-# INLINE (>) #-}
296
- xs > ys = Bundle. cmp (G. stream xs) (G. stream ys) == GT
297
-
309
+ (>) = coerce ((>) @ (V. Vector a ))
298
310
{-# INLINE (>=) #-}
299
- xs >= ys = Bundle. cmp ( G. stream xs) ( G. stream ys) /= LT
311
+ (>=) = coerce ( (>=) @ ( V. Vector a ))
300
312
301
313
instance Eq1 Vector where
302
314
liftEq eq xs ys = Bundle. eqBy eq (G. stream xs) (G. stream ys)
303
315
304
316
instance Ord1 Vector where
305
317
liftCompare cmp xs ys = Bundle. cmpBy cmp (G. stream xs) (G. stream ys)
306
318
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
-
324
319
instance Functor Vector where
325
320
{-# INLINE fmap #-}
326
321
fmap = map
@@ -400,49 +395,6 @@ instance Applicative.Alternative Vector where
400
395
{-# INLINE (<|>) #-}
401
396
(<|>) = (++)
402
397
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
-
446
398
instance Traversable. Traversable Vector where
447
399
{-# INLINE traverse #-}
448
400
traverse f xs =
0 commit comments