Skip to content

Commit

Permalink
Drop functions which don't enforce strictness
Browse files Browse the repository at this point in the history
These functions require user to maintain invariant that all element of vector is
in WHNF. And it's well known that this insvariant is difficult to enforce and
its violations are usually invisible.

Such spec leaks may be invisibe during testing only to cause pproblem in
production. So it's better to remove them unless good use case for them appears
  • Loading branch information
Shimuuar committed Mar 30, 2024
1 parent 8f60f2a commit db3529f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 46 deletions.
42 changes: 5 additions & 37 deletions vector/src/Data/Vector/Strict.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,9 @@
-- Immutable strict boxed vectors (that is, polymorphic arrays capable
-- of holding any Haskell value). It is possible to create vector
-- which contain bottom elements, either by using mutable interfaces
-- (see "Data.Vector.Strict.Mutable") or functions that don't preserve
-- strictness ('lazyFromArray').
-- (see "Data.Vector.Strict.Mutable")
--
-- For unboxed arrays, use "Data.Vector.Unboxed".

module Data.Vector.Strict (
-- * Boxed vectors
Vector, MVector,
Expand Down Expand Up @@ -163,9 +161,9 @@ module Data.Vector.Strict (
-- ** Lists
toList, Data.Vector.Strict.fromList, Data.Vector.Strict.fromListN,
-- ** Lazy vectors
toLazy, fromLazy, lazyFromLazy,
toLazy, fromLazy,
-- ** Arrays
toArray, fromArray, lazyFromArray, toArraySlice, unsafeFromArraySlice, unsafeLazyFromArraySlice,
toArray, fromArray, toArraySlice, unsafeFromArraySlice,

-- ** Other vector types
G.convert,
Expand Down Expand Up @@ -2476,11 +2474,6 @@ toLazy (Vector v) = v
fromLazy :: V.Vector a -> Vector a
fromLazy vec = liftRnfV (`seq` ()) v `seq` v where v = Vector vec

-- | /O(1)/ Convert lazy array to strict array. This function does not
-- evaluate vector elements.
lazyFromLazy :: V.Vector a -> Vector a
lazyFromLazy = Vector


-- Conversions - Arrays
-- -----------------------------
Expand All @@ -2492,15 +2485,7 @@ fromArray :: Array a -> Vector a
{-# INLINE fromArray #-}
fromArray arr = liftRnfV (`seq` ()) vec `seq` vec
where
vec = lazyFromArray arr

-- | /O(1)/ Convert an array to a vector. This function does not touch
-- content of array so resulting vector may contain bottoms.
--
-- @since NEXT
lazyFromArray :: Array a -> Vector a
{-# INLINE lazyFromArray #-}
lazyFromArray = Vector . V.fromArray
vec = Vector $ V.fromArray arr

-- | /O(n)/ Convert a vector to an array.
--
Expand Down Expand Up @@ -2537,25 +2522,8 @@ unsafeFromArraySlice ::
-> Vector a
{-# INLINE unsafeFromArraySlice #-}
unsafeFromArraySlice arr offset len = liftRnfV (`seq` ()) vec `seq` vec
where vec = unsafeLazyFromArraySlice arr offset len
where vec = Vector (V.unsafeFromArraySlice arr offset len)

-- | /O(1)/ Convert an array slice to a vector. This function does not touch
-- content of array so resulting vector may contain bottoms.
--
-- This function is very unsafe, because constructing an invalid
-- vector can yield almost all other safe functions in this module
-- unsafe. These are equivalent:
--
-- > unsafeFromArraySlice len offset === unsafeTake len . unsafeDrop offset . fromArray
--
-- @since 0.13.0.0
unsafeLazyFromArraySlice ::
Array a -- ^ Immutable boxed array.
-> Int -- ^ Offset
-> Int -- ^ Length
-> Vector a
{-# INLINE unsafeLazyFromArraySlice #-}
unsafeLazyFromArraySlice arr o l = Vector (V.unsafeFromArraySlice arr o l)


-- Conversions - Mutable vectors
Expand Down
11 changes: 2 additions & 9 deletions vector/src/Data/Vector/Strict/Mutable.hs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ module Data.Vector.Strict.Mutable (
-- ** Filling and copying
set, copy, move, unsafeCopy, unsafeMove,
-- ** Lazy arrays
toLazy, fromLazy, lazyFromLazy,
toLazy, fromLazy,
-- ** Arrays
fromMutableArray, toMutableArray,

Expand Down Expand Up @@ -717,14 +717,7 @@ toLazy :: MVector s a -> MV.MVector s a
{-# INLINE toLazy #-}
toLazy (MVector vec) = vec

-- | /O(1)/ Convert lazy mutable vector to strict mutable
-- vector. Vectors will share mutable buffer. This function does not
-- evaluate vector elements to WHNF.
lazyFromLazy :: MV.MVector s a -> MVector s a
{-# INLINE lazyFromLazy #-}
lazyFromLazy = MVector

-- | /O(1)/ Convert lazy mutable vector to strict mutable
-- | /O(n)/ Convert lazy mutable vector to strict mutable
-- vector. Vectors will share mutable buffer. This function evaluates
-- vector elements to WHNF.
fromLazy :: PrimMonad m => MV.MVector (PrimState m) a -> m (MVector (PrimState m) a)
Expand Down

0 comments on commit db3529f

Please sign in to comment.