Skip to content

Commit a3092d4

Browse files
committed
Rework overview of module for unboxed vectors
- Mention that vector are not _necessarily_ unboxed - Enumerate all standard deriving methods. (And drop "Implementing unboxed vectors for new data types can be very easy", it always felt like mockery.
1 parent ce8ed00 commit a3092d4

File tree

1 file changed

+34
-26
lines changed

1 file changed

+34
-26
lines changed

vector/src/Data/Vector/Unboxed.hs

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,42 @@
1313
-- Stability : experimental
1414
-- Portability : non-portable
1515
--
16-
-- Adaptive unboxed vectors. The implementation is based on type families
16+
-- Adaptive unboxed vectors. The implementation is based on data families
1717
-- and picks an efficient, specialised representation for every element type.
18-
-- For example, unboxed vectors of pairs are represented as pairs of unboxed
19-
-- vectors.
18+
-- For example, vector of fixed size primitives are backed by
19+
-- 'Data.Vector.Primitive.Vector', unboxed vectors of tuples are represented
20+
-- as tuples of unboxed vectors (see 'zip'\/'unzip'). Note that vector is
21+
-- only adaptive types could pick boxed representation for data type\/field
22+
-- of record. However all library instances are backed by unboxed array(s).
2023
--
21-
-- Implementing unboxed vectors for new data types can be very easy. Here is
22-
-- how the library does this for 'Complex' by simply wrapping vectors of
23-
-- pairs.
24+
-- Defining new instances of unboxed vectors is somewhat complicated since
25+
-- it requires defining two data family and two type class instances. Latter
26+
-- two could be generated using @GeneralizedNewtypeDeriving@ or @DerivingVia@
27+
--
28+
-- >>> :set -XTypeFamilies -XStandaloneDeriving -XMultiParamTypeClasses -XGeneralizedNewtypeDeriving
29+
-- >>>
30+
-- >>> import qualified Data.Vector.Generic as VG
31+
-- >>> import qualified Data.Vector.Generic.Mutable as VGM
32+
-- >>> import qualified Data.Vector.Unboxed as VU
33+
-- >>>
34+
-- >>> newtype Foo = Foo Int
35+
-- >>>
36+
-- >>> newtype instance VU.MVector s Foo = MV_Int (VU.MVector s Int)
37+
-- >>> newtype instance VU.Vector Foo = V_Int (VU.Vector Int)
38+
-- >>> deriving instance VGM.MVector VU.MVector Foo
39+
-- >>> deriving instance VG.Vector VU.Vector Foo
40+
-- >>> instance VU.Unbox Foo
41+
--
42+
-- For other data types we have several newtype wrappers for use with
43+
-- @DerivingVia@. See documentation of 'As' and 'IsoUnbox' for defining
44+
-- unboxed vector of product types. 'UnboxViaPrim' could be used to define
45+
-- vector of instances of 'Data.Vector.Primitive.Prim'. Similarly
46+
-- 'DoNotUnboxStrict'/'DoNotUnboxLazy'/'DoNotUnboxNormalForm' could be used
47+
-- to represent polymorphic fields as boxed vectors.
48+
--
49+
-- Or if everything else fails instances could be written by hand.
50+
-- Here is how the library does this for 'Complex' by simply wrapping
51+
-- vectors of pairs.
2452
--
2553
-- @
2654
-- newtype instance 'MVector' s ('Complex' a) = MV_Complex ('MVector' s (a,a))
@@ -38,26 +66,6 @@
3866
--
3967
-- instance ('RealFloat' a, 'Unbox' a) => 'Unbox' ('Complex' a)
4068
-- @
41-
--
42-
-- For newtypes, defining instances is easier since one could use
43-
-- @GeneralizedNewtypeDeriving@ in order to derive instances for
44-
-- 'Data.Vector.Generic.Vector' and 'Data.Vector.Generic.Mutable.MVector',
45-
-- since they're very cumbersome to write by hand:
46-
--
47-
-- >>> :set -XTypeFamilies -XStandaloneDeriving -XMultiParamTypeClasses -XGeneralizedNewtypeDeriving
48-
-- >>>
49-
-- >>> import qualified Data.Vector.Generic as VG
50-
-- >>> import qualified Data.Vector.Generic.Mutable as VGM
51-
-- >>> import qualified Data.Vector.Unboxed as VU
52-
-- >>>
53-
-- >>> newtype Foo = Foo Int
54-
-- >>>
55-
-- >>> newtype instance VU.MVector s Foo = MV_Int (VU.MVector s Int)
56-
-- >>> newtype instance VU.Vector Foo = V_Int (VU.Vector Int)
57-
-- >>> deriving instance VGM.MVector VU.MVector Foo
58-
-- >>> deriving instance VG.Vector VU.Vector Foo
59-
-- >>> instance VU.Unbox Foo
60-
6169
module Data.Vector.Unboxed (
6270
-- * Unboxed vectors
6371
Vector(V_UnboxAs, V_UnboxViaPrim), MVector(..), Unbox,

0 commit comments

Comments
 (0)