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.Array

Primitive arrays of boxed values.

  • 2 types
  • 24 values
  • Packageprimitive-0.9.1.0
  • Exports26
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceArray.hs
datadata Array a
#

Boxed arrays.

Constructors

Instances26Monad, Functor, MonadFix, MonadFail, Applicative, Foldable, …
  • Monad ArrayDefined in primitive-0.9.1.0 · Data.Primitive.Array
  • Functor ArrayDefined in primitive-0.9.1.0 · Data.Primitive.Array
  • MonadFix ArrayDefined in primitive-0.9.1.0 · Data.Primitive.Array
  • MonadFail ArrayDefined in primitive-0.9.1.0 · Data.Primitive.Array
  • Applicative ArrayDefined in primitive-0.9.1.0 · Data.Primitive.Array
  • Foldable ArrayDefined in primitive-0.9.1.0 · Data.Primitive.Array
  • Traversable ArrayDefined in primitive-0.9.1.0 · Data.Primitive.Array
  • Alternative ArrayDefined in primitive-0.9.1.0 · Data.Primitive.Array
  • MonadPlus ArrayDefined in primitive-0.9.1.0 · Data.Primitive.Array
  • MonadZip ArrayDefined in primitive-0.9.1.0 · Data.Primitive.Array
  • Eq1 ArrayDefined in primitive-0.9.1.0 · Data.Primitive.Array
  • Ord1 ArrayDefined in primitive-0.9.1.0 · Data.Primitive.Array
  • Read1 ArrayDefined in primitive-0.9.1.0 · Data.Primitive.Array
  • Show1 ArrayDefined in primitive-0.9.1.0 · Data.Primitive.Array
  • NFData1 ArrayDefined in primitive-0.9.1.0 · Data.Primitive.Array
  • Lift a => Lift (Array a)Defined in primitive-0.9.1.0 · Data.Primitive.Array
  • IsList (Array a)Defined in primitive-0.9.1.0 · Data.Primitive.Array
  • Eq a => Eq (Array a)Defined in primitive-0.9.1.0 · Data.Primitive.Array
  • Data a => Data (Array a)Defined in primitive-0.9.1.0 · Data.Primitive.Array
  • Ord a => Ord (Array a)Defined in primitive-0.9.1.0 · Data.Primitive.Array

    Lexicographic ordering. Subject to change between major versions.

  • Read a => Read (Array a)Defined in primitive-0.9.1.0 · Data.Primitive.Array
  • Show a => Show (Array a)Defined in primitive-0.9.1.0 · Data.Primitive.Array
  • Semigroup (Array a)Defined in primitive-0.9.1.0 · Data.Primitive.Array
  • Monoid (Array a)Defined in primitive-0.9.1.0 · Data.Primitive.Array
  • NFData a => NFData (Array a)Defined in primitive-0.9.1.0 · Data.Primitive.Array
  • type Item (Array a) = aDefined in primitive-0.9.1.0 · Data.Primitive.Array
valuenewArray :: PrimMonad m => Int -> a -> m (MutableArray (PrimState m) a)
#

Create a new mutable array of the specified size and initialise all elements with the given value.

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

valueindexArray :: Array a -> Int -> a
#

Read a value from the immutable array at the given index.

Note: this function does not do bounds checking.

valueindexArrayM :: Applicative m => Array a -> Int -> m a
#

Read a value from the immutable array at the given index using an applicative. This allows us to be strict in the array while remaining lazy in the read element which is very useful for collective operations. Suppose we want to copy an array. We could do something like this:

copy marr arr ... = do ...
                       writeArray marr i (indexArray arr i) ...
                       ...

But since the arrays are lazy, the calls to indexArray will not be evaluated. Rather, marr will be filled with thunks each of which would retain a reference to arr. This is definitely not what we want!

With indexArrayM, we can instead write

copy marr arr ... = do ...
                       x <- indexArrayM arr i
                       writeArray marr i x
                       ...

Now, indexing is executed immediately although the returned element is still not evaluated.

Note: this function does not do bounds checking.

valueindexArray## :: Array a -> Int -> (# a #)
#

Read a value from the immutable array at the given index, returning the result in an unboxed unary tuple. This is currently used to implement folds.

Note: this function does not do bounds checking.

valuefreezeArray
  1. :: PrimMonad m
  2. => MutableArray (PrimState m) a

    source

  3. -> Int

    offset

  4. -> Int

    length

  5. -> m (Array a)
#

Create an immutable copy of a slice of an array.

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.

valuethawArray
  1. :: PrimMonad m
  2. => Array a

    source

  3. -> Int

    offset

  4. -> Int

    length

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

Create a mutable array from a slice of an immutable array.

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.

valuerunArray :: (forall s. ST s (MutableArray s a)) -> Array a
#

Execute the monadic action and freeze the resulting array.

runArray m = runST $ m >>= unsafeFreezeArray
valuecreateArray
  1. :: Int
  2. -> a
  3. -> forall s. MutableArray s a -> ST s ()
  4. -> Array a
#

Create an array of the given size with a default value, apply the monadic function and freeze the result. If the size is 0, return emptyArray (rather than a new copy thereof).

createArray 0 _ _ = emptyArray
createArray n x f = runArray $ do
  mary <- newArray n x
  f mary
  pure mary
valuecopyArray
  1. :: PrimMonad m
  2. => MutableArray (PrimState m) a

    destination array

  3. -> Int

    offset into destination array

  4. -> Array a

    source array

  5. -> Int

    offset into source array

  6. -> Int

    number of elements to copy

  7. -> m ()
#

Copy a slice of an immutable array to a mutable array.

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

valuecloneArray
  1. :: Array a

    source array

  2. -> Int

    offset into destination array

  3. -> Int

    number of elements to copy

  4. -> Array a
#

Return a newly allocated Array with the specified subrange of the provided Array.

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

valuecloneMutableArray
  1. :: PrimMonad m
  2. => MutableArray (PrimState m) a

    source array

  3. -> Int

    offset into destination array

  4. -> Int

    number of elements to copy

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

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

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

valuearrayFromListN :: Int -> [a] -> Array a
#

Create an array from a list of a known length. If the length of the list does not match the given length, this throws an exception.

valuetraverseArrayP :: PrimMonad m => (a -> m b) -> Array a -> m (Array b)
#

This is the fastest, most straightforward way to traverse an array, but it only works correctly with a sufficiently "affine" PrimMonad instance. In particular, it must only produce one result array. Control.Monad.Trans.List.ListT-transformed monads, for example, will not work right at all.