HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Moduleprimitive-0.9.1.0Haskell2010

Data.Primitive.PrimArray

Arrays of unboxed primitive types. The functions provided by this module match the behavior of those provided by Data.Primitive.ByteArray, and the underlying types and primops that back them are the same. However, the type constructors PrimArray and MutablePrimArray take one additional argument compared to their respective counterparts ByteArray and MutableByteArray. This argument is used to designate the type of element in the array. Consequently, all functions in this module accept length and indices in terms of elements, not bytes.

  • 2 types
  • 61 values
  • Packageprimitive-0.9.1.0
  • Exports63
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourcePrimArray.hs

Types

2 declarations
datadata PrimArray a
#

Arrays of unboxed elements. This accepts types like Double, Char, Int and Word, as well as their fixed-length variants (Word8, Word16, etc.). Since the elements are unboxed, a PrimArray is strict in its elements. This differs from the behavior of Array, which is lazy in its elements.

Constructors

Instances9Lift, IsList, Eq, Ord, Show, Semigroup, …
  • Lift (PrimArray a)Defined in primitive-0.9.1.0 · Data.Primitive.PrimArray
  • Prim a => IsList (PrimArray a)Defined in primitive-0.9.1.0 · Data.Primitive.PrimArray
  • (Eq a, Prim a) => Eq (PrimArray a)Defined in primitive-0.9.1.0 · Data.Primitive.PrimArray
  • (Ord a, Prim a) => Ord (PrimArray a)Defined in primitive-0.9.1.0 · Data.Primitive.PrimArray

    Lexicographic ordering. Subject to change between major versions.

  • (Show a, Prim a) => Show (PrimArray a)Defined in primitive-0.9.1.0 · Data.Primitive.PrimArray
  • Semigroup (PrimArray a)Defined in primitive-0.9.1.0 · Data.Primitive.PrimArray
  • Monoid (PrimArray a)Defined in primitive-0.9.1.0 · Data.Primitive.PrimArray
  • NFData (PrimArray a)Defined in primitive-0.9.1.0 · Data.Primitive.PrimArray
  • type Item (PrimArray a) = aDefined in primitive-0.9.1.0 · Data.Primitive.PrimArray
datadata MutablePrimArray s a
#

Mutable primitive arrays associated with a primitive state token. These can be written to and read from in a monadic context that supports sequencing, such as IO or ST. Typically, a mutable primitive array will be built and then converted to an immutable primitive array using unsafeFreezePrimArray. However, it is also acceptable to simply discard a mutable primitive array since it lives in managed memory and will be garbage collected when no longer referenced.

Instances2Eq, NFData

Allocation

5 declarations

Resize a mutable primitive array. The new size is given in elements.

This will either resize the array in-place or, if not possible, allocate the contents into a new, unpinned array and copy the original array's contents.

To avoid undefined behaviour, the original MutablePrimArray shall not be accessed anymore after a resizeMutablePrimArray has been performed. Moreover, no reference to the old one should be kept in order to allow garbage collection of the original MutablePrimArray in case a new MutablePrimArray had to be allocated.

Element Access

3 declarations
valueindexPrimArray :: Prim a => PrimArray a -> Int -> a
#

Read a primitive value from the primitive array.

Note: this function does not do bounds checking.

Freezing and Thawing

6 declarations
valuefreezePrimArray
  1. :: (PrimMonad m, Prim a)
  2. => MutablePrimArray (PrimState m) a

    source

  3. -> Int

    offset in elements

  4. -> Int

    length in elements

  5. -> m (PrimArray a)
#

Create an immutable copy of a slice of a primitive array. The offset and length are given in elements.

This operation makes a copy of the specified section, so it is safe to continue using the mutable array afterward.

Note: The provided array should contain the full subrange specified by the two Ints, but this is not checked.

valuethawPrimArray
  1. :: (PrimMonad m, Prim a)
  2. => PrimArray a

    source

  3. -> Int

    offset in elements

  4. -> Int

    length in elements

  5. -> m (MutablePrimArray (PrimState m) a)
#

Create a mutable primitive array from a slice of an immutable primitive array. The offset and length are given in elements.

This operation makes a copy of the specified slice, so it is safe to use the immutable array afterward.

Note: The provided array should contain the full subrange specified by the two Ints, but this is not checked.

valuecreatePrimArray
  1. :: Prim a
  2. => Int
  3. -> forall s. MutablePrimArray s a -> ST s ()
  4. -> PrimArray a
#

Create an uninitialized array of the given length, apply the function to it, and freeze the result.

Note: this function does not check if the input is non-negative.

@since FIXME

Block Operations

8 declarations
valuecopyPrimArrayToPtr
  1. :: (PrimMonad m, Prim a)
  2. => Ptr a

    destination pointer

  3. -> PrimArray a

    source array

  4. -> Int

    offset into source array

  5. -> Int

    number of elements to copy

  6. -> m ()
#

Copy a slice of an immutable primitive array to a pointer. The offset and length are given in elements of type a. This function assumes that the Prim instance of a agrees with the Storable instance.

Note: this function does not do bounds or overlap checking.

valuecopyMutablePrimArrayToPtr
  1. :: (PrimMonad m, Prim a)
  2. => Ptr a

    destination pointer

  3. -> MutablePrimArray (PrimState m) a

    source array

  4. -> Int

    offset into source array

  5. -> Int

    number of elements to copy

  6. -> m ()
#

Copy a slice of a mutable primitive array to a pointer. The offset and length are given in elements of type a. This function assumes that the Prim instance of a agrees with the Storable instance.

Note: this function does not do bounds or overlap checking.

valuecopyPtrToMutablePrimArray
  1. :: (PrimMonad m, Prim a)
  2. => MutablePrimArray (PrimState m) a

    destination array

  3. -> Int

    destination offset

  4. -> Ptr a

    source pointer

  5. -> Int

    number of elements

  6. -> m ()
#

Copy from a pointer to a mutable primitive array. The offset and length are given in elements of type a. This function assumes that the Prim instance of a agrees with the Storable instance.

Note: this function does not do bounds or overlap checking.

valueclonePrimArray
  1. :: Prim a
  2. => PrimArray a

    source array

  3. -> Int

    offset into destination array

  4. -> Int

    number of elements to copy

  5. -> PrimArray a
#

Return a newly allocated array with the specified subrange of the provided array. The provided array should contain the full subrange specified by the two Ints, but this is not checked.

Information

10 declarations

Check whether or not the primitive array is pinned. Pinned primitive arrays cannot be moved by the garbage collector. It is safe to use primArrayContents on such arrays. This function is only available when compiling with GHC 8.2 or newer.

List Conversion

3 declarations

Folding

5 declarations

Effectful Folding

2 declarations

Map/Create

7 declarations

Effectful Map/Create

0 declarations

The naming conventions adopted in this section are explained in the documentation of the Data.Primitive module.

Lazy Applicative

valuetraversePrimArray
  1. :: (Applicative f, Prim a, Prim b)
  2. => (a -> f b)

    mapping function

  3. -> PrimArray a

    primitive array

  4. -> f (PrimArray b)
#

Traverse a primitive array. The traversal performs all of the applicative effects before forcing the resulting values and writing them to the new primitive array. Consequently:

Example1 expression
traversePrimArray (\x -> print x $> bool x undefined (x == 2)) (fromList [1, 2, 3 :: Int])123*** Exception: Prelude.undefined

The function traversePrimArrayP always outperforms this function, but it requires a PrimMonad constraint, and it forces the values as it performs the effects.

Strict Primitive Monadic

valuetraversePrimArrayP
  1. :: (PrimMonad m, Prim a, Prim b)
  2. => a -> m b
  3. -> PrimArray a
  4. -> m (PrimArray b)
#

Traverse a primitive array. The traversal forces the resulting values and writes them to the new primitive array as it performs the monadic effects. Consequently:

Example1 expression
traversePrimArrayP (\x -> print x $> bool x undefined (x == 2)) (fromList [1, 2, 3 :: Int])12*** Exception: Prelude.undefined

In many situations, traversePrimArrayP can replace traversePrimArray, changing the strictness characteristics of the traversal but typically improving the performance. Consider the following short-circuiting traversal:

incrPositiveA :: PrimArray Int -> Maybe (PrimArray Int)
incrPositiveA xs = traversePrimArray (\x -> bool Nothing (Just (x + 1)) (x > 0)) xs

This can be rewritten using traversePrimArrayP. To do this, we must change the traversal context to MaybeT (ST s), which has a PrimMonad instance:

incrPositiveB :: PrimArray Int -> Maybe (PrimArray Int)
incrPositiveB xs = runST $ runMaybeT $ traversePrimArrayP
  (\x -> bool (MaybeT (return Nothing)) (MaybeT (return (Just (x + 1)))) (x > 0))
  xs

Benchmarks demonstrate that the second implementation runs 150 times faster than the first. It also results in fewer allocations.