Skip to content

Commit ccb0966

Browse files
committed
Move compatibility patterns into *.Pattern modules
We have to define pattern in separate module since it must be defined in separate module in order to syntax > import Data.Vector.Mutable (MVector(..)) to work. Only patterns defined in another module could be exported together with type so compatibility pattern must be defined in module other that one with safe API. Also DEPRECATED pragma deprecates name in both namespaces. So we can't define patterns in Unsafe modules. This means we need dedicated module for patterns. I went with *.Pattern name since we have Data.Vector.Storable.Internal and it's imported by unsafe modules.
1 parent fa2b1e1 commit ccb0966

16 files changed

Lines changed: 135 additions & 54 deletions

File tree

vector/src/Data/Vector/Mutable.hs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121

2222
module Data.Vector.Mutable (
2323
-- * Mutable boxed vectors
24-
MVector, IOVector, STVector,
25-
pattern MVector,
24+
MVector(MVector), IOVector, STVector,
2625

2726
-- * Accessors
2827

@@ -74,20 +73,15 @@ module Data.Vector.Mutable (
7473
) where
7574

7675
import qualified Data.Vector.Generic.Mutable as G
77-
import Data.Vector.Mutable.Unsafe (MVector)
78-
import qualified Data.Vector.Mutable.Unsafe as U
76+
import Data.Vector.Mutable.Unsafe (MVector(..))
77+
import Data.Vector.Pattern
7978
import Data.Primitive.Array
8079
import Control.Monad.Primitive
8180

8281
import Prelude( Ord, Bool, Ordering(..), Int, Maybe, (<$>) )
8382

8483
#include "vector.h"
8584

86-
pattern MVector :: Int -> Int -> MutableArray s a -> MVector s a
87-
pattern MVector i j arr = U.UnsafeMVector i j arr
88-
{-# COMPLETE MVector #-}
89-
{-# DEPRECATED MVector "Use MVector exported from \"Data.Vector.Mutable.Unsafe\"" #-}
90-
9185
type IOVector = MVector RealWorld
9286
type STVector s = MVector s
9387

@@ -102,14 +96,14 @@ fromMutableArray :: PrimMonad m => MutableArray (PrimState m) a -> m (MVector (P
10296
{-# INLINE fromMutableArray #-}
10397
fromMutableArray marr =
10498
let size = sizeofMutableArray marr
105-
in MVector 0 size <$> cloneMutableArray marr 0 size
99+
in UnsafeMVector 0 size <$> cloneMutableArray marr 0 size
106100

107101
-- | /O(n)/ Make a copy of a mutable vector into a new mutable array.
108102
--
109103
-- @since 0.12.2.0
110104
toMutableArray :: PrimMonad m => MVector (PrimState m) a -> m (MutableArray (PrimState m) a)
111105
{-# INLINE toMutableArray #-}
112-
toMutableArray (MVector offset size marr) = cloneMutableArray marr offset size
106+
toMutableArray (UnsafeMVector offset size marr) = cloneMutableArray marr offset size
113107

114108

115109
-- Length information

vector/src/Data/Vector/Mutable/Unsafe.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
{-# LANGUAGE MultiParamTypeClasses #-}
55
{-# LANGUAGE RoleAnnotations #-}
66
{-# LANGUAGE TypeFamilies #-}
7+
{-# LANGUAGE PatternSynonyms #-}
78
-- |
89
-- This module exposes internal representation of mutable lazy boxed
910
-- vector and functions that work on that representation directly (as

vector/src/Data/Vector/Pattern.hs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{-# LANGUAGE PatternSynonyms #-}
2+
-- See NOTE: [Constructor deprecation]
3+
--
4+
--
5+
-- This module should be removed once deprecated constructors are dropped.
6+
module Data.Vector.Pattern
7+
( pattern MVector
8+
) where
9+
10+
import Data.Primitive.Array
11+
import Data.Vector.Mutable.Unsafe
12+
13+
14+
pattern MVector :: Int -> Int -> MutableArray s a -> MVector s a
15+
pattern MVector i j arr = UnsafeMVector i j arr
16+
{-# COMPLETE MVector #-}
17+
{-# DEPRECATED MVector "Use MVector exported from \"Data.Vector.Mutable.Unsafe\"" #-}

vector/src/Data/Vector/Primitive.hs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
module Data.Vector.Primitive (
2727
-- * Primitive vectors
28-
Vector, MVector(MVector), pattern Vector,
28+
Vector(Vector), MVector(MVector),
2929

3030
-- * Accessors
3131

@@ -165,11 +165,10 @@ module Data.Vector.Primitive (
165165
import Control.Applicative (Applicative)
166166
import qualified Data.Vector.Generic as G
167167
import Data.Vector.Primitive.Unsafe (Vector,unsafeCoerceVector,unsafeCast)
168-
import qualified Data.Vector.Primitive.Unsafe as U
169-
import Data.Vector.Primitive.Mutable.Unsafe (MVector)
170-
import Data.Vector.Primitive.Mutable (pattern MVector)
168+
import Data.Vector.Primitive.Mutable (MVector)
169+
import Data.Vector.Primitive.Pattern
170+
171171
import Data.Primitive ( Prim )
172-
import Data.Primitive.ByteArray
173172

174173
import Control.Monad.ST ( ST )
175174
import Control.Monad.Primitive
@@ -178,11 +177,6 @@ import Prelude
178177
( Eq, Ord, Num, Enum, Monoid, Traversable, Monad, Bool, Ordering(..), Int, Maybe, Either
179178
, (==))
180179

181-
pattern Vector :: Int -> Int -> ByteArray -> Vector a
182-
pattern Vector i j arr = U.UnsafeVector i j arr
183-
{-# COMPLETE Vector #-}
184-
{-# DEPRECATED Vector "Use Vector constructor exported from \"Data.Vector.Primitive.Unsafe\"" #-}
185-
186180
-- Length
187181
-- ------
188182

vector/src/Data/Vector/Primitive/Mutable.hs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
{-# LANGUAGE MultiParamTypeClasses #-}
44
{-# LANGUAGE RoleAnnotations #-}
55
{-# LANGUAGE ScopedTypeVariables #-}
6-
{-# LANGUAGE PatternSynonyms #-}
76
-- |
87
-- Module : Data.Vector.Primitive.Mutable
98
-- Copyright : (c) Roman Leshchinskiy 2008-2010
@@ -20,8 +19,7 @@
2019

2120
module Data.Vector.Primitive.Mutable (
2221
-- * Mutable vectors of primitive types
23-
MVector, IOVector, STVector,
24-
pattern MVector,
22+
MVector(MVector), IOVector, STVector,
2523

2624
-- * Accessors
2725

@@ -73,22 +71,16 @@ module Data.Vector.Primitive.Mutable (
7371

7472
import qualified Data.Vector.Generic.Mutable as G
7573
import Data.Primitive ( Prim )
76-
import Data.Primitive.ByteArray
7774
import Data.Vector.Primitive.Mutable.Unsafe
7875
(MVector,unsafeCoerceMVector,unsafeCast)
79-
import qualified Data.Vector.Primitive.Mutable.Unsafe as U
76+
import Data.Vector.Primitive.Pattern
8077
import Control.Monad.Primitive
8178

8279
import Prelude ( Ord, Bool, Int, Maybe, Ordering(..) )
8380

8481
#include "vector.h"
8582

8683

87-
pattern MVector :: Int -> Int -> MutableByteArray s -> MVector s a
88-
pattern MVector i j arr = U.UnsafeMVector i j arr
89-
{-# COMPLETE MVector #-}
90-
{-# DEPRECATED MVector "Use MVector exported from \"Data.Vector.Primitive.Mutable.Unsafe\"" #-}
91-
9284
type IOVector = MVector RealWorld
9385
type STVector s = MVector s
9486

vector/src/Data/Vector/Primitive/Mutable/Unsafe.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
{-# LANGUAGE TypeFamilies #-}
55
{-# LANGUAGE RoleAnnotations #-}
66
{-# LANGUAGE ScopedTypeVariables #-}
7+
{-# LANGUAGE PatternSynonyms #-}
78
-- |
89
-- This module exposes internal representation of mutable vectors backed by
910
-- single 'ByteArray' and functions that work on that representation
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
{-# LANGUAGE PatternSynonyms #-}
2+
-- NOTE: [Constructor deprecation]
3+
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4+
--
5+
-- Constructor could be imported by users in two ways:
6+
--
7+
-- > import Data.Vector.Mutable qualified as MV
8+
-- > import Data.Vector.Mutable (MVector(..))
9+
--
10+
-- Latter method causes problems for us. For that to work pattern
11+
-- synonym must be exported as `, MVector(MVector)` and it's only
12+
-- possible if pattern is imported from another module. It couldn't
13+
-- be Unsafe module becasuse of DEPRECATED pragma quirk. It
14+
-- deprecates both type constructor and data constructor. Quoting GHC
15+
-- 8.8 user guide:
16+
--
17+
-- > If both are in scope, there is currently no way to specify one
18+
-- > without the other
19+
--
20+
-- Since 9.10 is possible to disambiguate but we need to support older
21+
-- compilers so only way is to define pattern for deprecated
22+
-- constructor in separate module.
23+
--
24+
-- This forces us to put compatibility patterns into separate module.
25+
--
26+
--
27+
--
28+
-- This module should be removed once deprecated constructors are
29+
-- dropped.
30+
module Data.Vector.Primitive.Pattern
31+
( pattern MVector
32+
, pattern Vector
33+
) where
34+
35+
import Data.Primitive.ByteArray
36+
import Data.Vector.Primitive.Mutable.Unsafe
37+
import Data.Vector.Primitive.Unsafe
38+
39+
40+
pattern MVector :: Int -> Int -> MutableByteArray s -> MVector s a
41+
pattern MVector i j arr = UnsafeMVector i j arr
42+
{-# COMPLETE MVector #-}
43+
{-# DEPRECATED MVector "Use MVector exported from \"Data.Vector.Primitive.Mutable.Unsafe\"" #-}
44+
45+
pattern Vector :: Int -> Int -> ByteArray -> Vector a
46+
pattern Vector i j arr = UnsafeVector i j arr
47+
{-# COMPLETE Vector #-}
48+
{-# DEPRECATED Vector "Use Vector constructor exported from \"Data.Vector.Primitive.Unsafe\"" #-}
49+

vector/src/Data/Vector/Primitive/Unsafe.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
{-# LANGUAGE TypeFamilies #-}
55
{-# LANGUAGE RoleAnnotations #-}
66
{-# LANGUAGE ScopedTypeVariables #-}
7+
{-# LANGUAGE PatternSynonyms #-}
78
-- |
89
-- This module exposes internal representation of vectors backed by
910
-- single 'ByteArray' and functions that work on that representation
@@ -41,6 +42,8 @@ import Data.Coerce
4142
import Unsafe.Coerce
4243

4344
import Data.Vector.Primitive.Mutable.Unsafe (MVector(..))
45+
46+
4447
----------------------------------------------------------------
4548
-- Immutable
4649
----------------------------------------------------------------
@@ -59,6 +62,7 @@ data Vector a = UnsafeVector
5962

6063
type instance G.Mutable Vector = MVector
6164

65+
6266
-- | /O(1)/ Unsafely coerce an immutable vector from one element type to another,
6367
-- representationally equal type. The operation just changes the type of the
6468
-- underlying pointer and does not modify the elements.

vector/src/Data/Vector/Storable/Mutable.hs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121

2222
module Data.Vector.Storable.Mutable(
2323
-- * Mutable vectors of 'Storable' types
24-
MVector, IOVector, STVector,
25-
pattern MVector,
24+
MVector(MVector), IOVector, STVector,
2625

2726
-- * Accessors
2827

@@ -82,23 +81,16 @@ import qualified Data.Vector.Generic.Mutable as G
8281
import Data.Vector.Storable.Mutable.Unsafe(
8382
MVector,IOVector,STVector,unsafeCast,unsafeWith,unsafeCoerceMVector,
8483
unsafeToForeignPtr,unsafeToForeignPtr0,unsafeFromForeignPtr,unsafeFromForeignPtr0)
85-
import qualified Data.Vector.Storable.Mutable.Unsafe as U
84+
import Data.Vector.Storable.Pattern
8685
import Foreign.Storable
8786

8887
import Control.Monad.Primitive
89-
import Foreign.ForeignPtr (ForeignPtr)
9088

9189
import Prelude (Int, Ord, Bool, Maybe, Ordering(..) )
9290

9391
#include "vector.h"
9492

9593

96-
pattern MVector :: Int -> ForeignPtr a -> MVector s a
97-
pattern MVector i ptr = U.UnsafeMVector i ptr
98-
{-# COMPLETE MVector #-}
99-
{-# DEPRECATED MVector "Use MVector exported from Data.Vector.Strict.Mutable.Unsafe" #-}
100-
101-
10294
-- Length information
10395
-- ------------------
10496

vector/src/Data/Vector/Storable/Mutable/Unsafe.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
{-# LANGUAGE MultiParamTypeClasses #-}
55
{-# LANGUAGE RoleAnnotations #-}
66
{-# LANGUAGE ScopedTypeVariables #-}
7+
{-# LANGUAGE PatternSynonyms #-}
8+
{-# LANGUAGE ExplicitNamespaces #-}
79
-- |
810
-- This module exposes internal representation of mutable vectors
911
-- based on 'Storable' and functions that work on that representation
@@ -65,6 +67,7 @@ data MVector s a = UnsafeMVector
6567
-- ^ Underlying buffer as a `ForeignPtr`, which is allowed to be mutated
6668
}
6769

70+
6871
type IOVector = MVector RealWorld
6972
type STVector s = MVector s
7073

0 commit comments

Comments
 (0)