HORIZON HASKELLDocslts/ghc-10.0.x8fef28d2026-11-02Search packagesCtrl K
Theme: |

GHC 10.0.0.20260917 · lts/ghc-10.0.x · 8fef28d · 2026-11-02

Modulecontiguous-0.6.5.0Haskell2010

Data.Primitive.Contiguous

The contiguous package presents a common API to a number of contiguous array types and their mutable counterparts. This is enabled with the Contiguous typeclass, which parameterises over a contiguous array type and defines the core operations. However, the stable part of the interface is contained in this module, which combines those primitives into common, efficient array algorithms suitable for replacing pointer-heavy list manipulations.

Contents
  1. Accessors
  2. Length Information
  3. Indexing
  4. Monadic indexing
  5. Construction
  6. Initialisation
  7. Fixed Length
  8. Running
  9. Monadic initialisation
  10. Unfolding
  11. Enumeration
  12. Concatenation
  13. Splitting and Splicing
  14. Slicing
  15. Modifying arrays
  16. Permutations
  17. Resizing
  18. Elementwise operations
  19. Mapping
  20. Zipping
  21. Specific elements
  22. Working with predicates
  23. Filtering
  24. Searching
  25. Comparing for equality
  26. Folds
  27. Zipping Folds
  28. Traversals
  29. Typeclass method defaults
  30. Prefix sums (scans)
  31. Conversions
  32. Lists
  33. Other array types
  34. Between mutable and immutable variants
  35. Hashing
  36. Forcing an array and its contents
  37. Classes
  38. Re-Exports
Synopsis
  • Packagecontiguous-0.6.5.0
  • Exports177
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceContiguous.hs

Accessors

Length Information

Indexing

methodindex# :: Element arr b => arr b -> Int -> (# b #)

Index into an array at the given index, yielding an unboxed one-tuple of the element.

Monadic indexing

methodindexM :: (Element arr b, Monad m) => arr b -> Int -> m b

Indexing in a monad.

The monad allows operations to be strict in the array when necessary. Suppose array copying is implemented like this:

copy mv v = ... write mv i (v ! i) ...

For lazy arrays, v ! i would not be not be evaluated, which means that mv would unnecessarily retain a reference to v in each element written.

With indexM, copying can be implemented like this instead:

copy mv v = ... do
  x <- indexM v i
  write mv i x

Here, no references to v are retained because indexing (but not the elements) is evaluated eagerly.

Construction

Initialisation

valueiterateN :: (Contiguous arr, Element arr a) => Int -> (a -> a) -> a -> arr a

Apply a function n times to a value and construct an array where each consecutive element is the result of an additional application of this function. The zeroth element is the original value.

iterateN 5 (+ 1) 0 = fromListN 5 [0,1,2,3,4]

Fixed Length

Running

methodrun :: (forall s. ST s (arr a)) -> arr a

Run an effectful computation that produces an array.

Monadic initialisation

valueiterateMutableNM
  1. :: (Contiguous arr, Element arr a, PrimMonad m)
  2. => Int
  3. -> a -> m a
  4. -> a
  5. -> m (Mutable arr (PrimState m) a)

Apply a monadic function n times to a value and construct a mutable array where each consecutive element is the result of an additional application of this function. The zeroth element is the original value.

Unfolding

valueunfoldr
  1. :: (Contiguous arr, Element arr a)
  2. => b -> Maybe (a, b)
  3. -> b
  4. -> arr a

Construct an array by repeatedly applying a generator function to a seed. The generator function yields Just the next element and the new seed or Nothing if there are no more elements.

Example1 expression
unfoldr (\n -> if n == 0 then Nothing else Just (n,n-1) 10    <10,9,8,7,6,5,4,3,2,1>
valueunfoldrN
  1. :: (Contiguous arr, Element arr a)
  2. => Int
  3. -> b -> Maybe (a, b)
  4. -> b
  5. -> arr a

Construct an array with at most n elements by repeatedly applying the generator function to a seed. The generator function yields Just the next element and the new seed or Nothing if there are no more elements.

valueunfoldrMutable
  1. :: (Contiguous arr, Element arr a, PrimMonad m)
  2. => b -> Maybe (a, b)
  3. -> b
  4. -> m (Mutable arr (PrimState m) a)

Construct a mutable array by repeatedly applying a generator function to a seed. The generator function yields Just the next element and the new seed or Nothing if there are no more elements.

Example1 expression
unfoldrMutable (\n -> if n == 0 then Nothing else Just (n,n-1) 10    <10,9,8,7,6,5,4,3,2,1>

Enumeration

Concatenation

Splitting and Splicing

methodinsertAt :: Element arr b => arr b -> Int -> b -> arr b

Copy a slice of an array and then insert an element into that array.

The default implementation performs a memset which would be unnecessary except that the garbage collector might trace the uninitialized array.

Was previously insertSlicing @since 0.6.0

Slicing

datadata Slice (arr :: Type -> Type) a

Slices of immutable arrays: packages an offset and length with a backing array.

Instances5Contiguous, Element, Mutable, MutableSliced, Sliced
datadata MutableSlice (arr :: Type -> Type) s a

Slices of mutable arrays: packages an offset and length with a mutable backing array.

Modifying arrays

valuereplaceAt :: (Contiguous arr, Element arr a) => arr a -> Int -> a -> arr a

Create a copy of an array except the element at the index is replaced with the given value.

Permutations

Resizing

methodresize
  1. :: (PrimMonad m, Element arr b)
  2. => Mutable arr (PrimState m) b
  3. -> Int
  4. -> m (Mutable arr (PrimState m) b)

Resize an array into one with the given size. If the array is grown, then reading from any newly introduced element before writing to it is undefined behavior. The current behavior is that anything backed by MutableByteArray# ends with uninitialized memory at these indices. But for SmallMutableArray or Array, these are set to an error thunk, so reading from them and forcing the result causes the program to crash. For UnliftedArray, the new elements have undefined values of an unknown type. If the array is not grown, it may (or may not) be modified in place.

Elementwise operations

Mapping

valuemap
  1. :: (Contiguous arr1, Element arr1 b, Contiguous arr2, Element arr2 c)
  2. => b -> c
  3. -> arr1 b
  4. -> arr2 c

Map over the elements of an array.

Note that because a new array must be created, the resulting array type can be different than the original.

valuemap'
  1. :: (Contiguous arr1, Element arr1 b, Contiguous arr2, Element arr2 c)
  2. => b -> c
  3. -> arr1 b
  4. -> arr2 c

Map strictly over the elements of an array.

Note that because a new array must be created, the resulting array type can be different than the original.

valueimap'
  1. :: (Contiguous arr1, Element arr1 b, Contiguous arr2, Element arr2 c)
  2. => Int -> b -> c
  3. -> arr1 b
  4. -> arr2 c

Map strictly over the elements of an array with the index.

Note that because a new array must be created, the resulting array type can be different than the original.

Zipping

valuezip
  1. :: (Contiguous arr1, Contiguous arr2, Contiguous arr3, Element arr1 a, Element arr2 b, Element arr3 (a, b))
  2. => arr1 a
  3. -> arr2 b
  4. -> arr3 (a, b)

zip takes two arrays and returns an array of corresponding pairs.

zip [1, 2] ['a', 'b'] = [(1, 'a'), (2, 'b')]

If one input array is shorter than the other, excess elements of the longer array are discarded:

zip [1] ['a', 'b'] = [(1, 'a')]
zip [1, 2] ['a'] = [(1, 'a')]

Specific elements

Working with predicates

Filtering

Searching

Comparing for equality

valuesame :: ContiguousU arr => arr a -> arr a -> Bool

This function does not behave deterministically. Optimization level and inlining can affect its results. However, the one thing that can be counted on is that if it returns True, the two immutable arrays are definitely the same. This is useful as shortcut for equality tests. However, keep in mind that a result of False tells us nothing about the arguments.

Folds

valueifoldl'
  1. :: (Contiguous arr, Element arr a)
  2. => b -> Int -> a -> b
  3. -> b
  4. -> arr a
  5. -> b

Strict left fold over the elements of an array, where the accumulating function cares about the index of the element.

valueifoldr
  1. :: (Contiguous arr, Element arr a)
  2. => Int -> a -> b -> b
  3. -> b
  4. -> arr a
  5. -> b

Right fold over the element of an array, lazy in the accumulator, provides index to the step function.

valueifoldr'
  1. :: (Contiguous arr, Element arr a)
  2. => Int -> a -> b -> b
  3. -> b
  4. -> arr a
  5. -> b

Strict right fold over the elements of an array, where the accumulating function cares about the index of the element.

valueasum
  1. :: (Contiguous arr, Element arr (f a), Alternative f)
  2. => arr (f a)
  3. -> f a

The sum of a collection of actions, generalizing concat.

Example1 expression
asum (C.fromList ['Just' "Hello", 'Nothing', Just "World"] :: Array String)Just "Hello"

Zipping Folds

Traversals

valuetraverse_
  1. :: (Contiguous arr, Element arr a, Applicative f)
  2. => a -> f b
  3. -> arr a
  4. -> f ()

Map each element of the array to an action, evaluate these actions from left to right, and ignore the results. For a version that doesn't ignore the results, see traverse.

valueitraverse_
  1. :: (Contiguous arr, Element arr a, Applicative f)
  2. => Int -> a -> f b
  3. -> arr a
  4. -> f ()

Map each element of the array and its index to an action, evaluate these actions from left to right, and ignore the results. For a version that doesn't ignore the results, see itraverse.

valuemapM
  1. :: (Contiguous arr1, Contiguous arr2, Element arr1 a, Element arr2 b, Monad m)
  2. => a -> m b
  3. -> arr1 a
  4. -> m (arr2 b)

Map each element of a structure to a monadic action, evaluate these actions from left to right, and collect the results. for a version that ignores the results see mapM_.

Typeclass method defaults

Prefix sums (scans)

valuescanl
  1. :: (Contiguous arr1, Contiguous arr2, Element arr1 a, Element arr2 b)
  2. => b -> a -> b
  3. -> b
  4. -> arr1 a
  5. -> arr2 b

scanl is similar to foldl, but returns an array of successive reduced values from the left:

scanl f z [x1, x2, ...] = [z, f z x1, f (f z x1) x2, ...]

Note that

last (toList (scanl f z xs)) == foldl f z xs.

Conversions

Lists

valuefromListN :: (Contiguous arr, Element arr a) => Int -> [a] -> arr a

Given an Int that is representative of the length of the list, convert the list into a mutable array of the given length.

Note: calls error if the given length is incorrect.

valueunsafeFromListN
  1. :: (Contiguous arr, Element arr a)
  2. => Int

    length of list

  3. -> [a]

    list

  4. -> arr a

Create an array from a list. If the given length does not match the actual length, this function has undefined behavior.

valueunsafeFromListReverseN
  1. :: (Contiguous arr, Element arr a)
  2. => Int
  3. -> [a]
  4. -> arr a

Create an array from a list, reversing the order of the elements. If the given length does not match the actual length, this function has undefined behavior.

Other array types

methodlift :: Unlifted arr b -> arr b

Lift an array (i.e. point to the data through an intervening thunk).

methodunlift :: arr b -> Unlifted arr b

Unlift an array (i.e. point to the data without an intervening thunk).

Between mutable and immutable variants

Hashing

Forcing an array and its contents

Classes

classclass Contiguous (arr :: Type -> Type) where

The Contiguous typeclass as an interface to a multitude of contiguous structures.

Some functions do not make sense on slices; for those, see ContiguousU.

Associated types

  • type family Mutable (arr :: Type -> Type) :: Type -> Type -> Type

    The Mutable counterpart to the array.

  • type family Element (arr :: Type -> Type) :: Type -> Constraint

    The constraint needed to store elements in the array.

  • type family Sliced (arr :: Type -> Type) :: Type -> Type

    The slice type of this array. The slice of a raw array type t should be 'Slice t', whereas the slice of a slice should be the same slice type.

  • type family MutableSliced (arr :: Type -> Type) :: Type -> Type -> Type

    The mutable slice type of this array. The mutable slice of a raw array type t should be 'MutableSlice t', whereas the mutable slice of a mutable slice should be the same slice type.

Instances6Contiguous
classclass Contiguous arr => ContiguousU (arr :: Type -> Type) where

The ContiguousU typeclass is an extension of the Contiguous typeclass, but includes operations that make sense only on unsliced contiguous structures.

Instances5ContiguousU
classclass Always a

A typeclass that is satisfied by all types. This is used used to provide a fake constraint for Array and SmallArray.

Instances1Always
  • Always aDefined in contiguous-0.6.5.0 · Data.Primitive.Contiguous.Class

Re-Exports

datadata Array a

Boxed arrays.

Instances36Monad, 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
  • Contiguous ArrayDefined in contiguous-0.6.5.0 · Data.Primitive.Contiguous.Class
  • ContiguousU ArrayDefined in contiguous-0.6.5.0 · Data.Primitive.Contiguous.Class
  • 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
  • PrimUnlifted (Array a)Defined in primitive-unlifted-2.2.0.0 · Data.Primitive.Unlifted.Class
  • type Item (Array a) = aDefined in primitive-0.9.1.0 · Data.Primitive.Array
  • type Unlifted (Array a) = Array# aDefined in primitive-unlifted-2.2.0.0 · Data.Primitive.Unlifted.Class
  • type Element Array = AlwaysDefined in contiguous-0.6.5.0 · Data.Primitive.Contiguous.Class
  • type Mutable Array = MutableArrayDefined in contiguous-0.6.5.0 · Data.Primitive.Contiguous.Class
  • type MutableSliced Array = MutableSlice ArrayDefined in contiguous-0.6.5.0 · Data.Primitive.Contiguous.Class
  • type Sliced Array = Slice ArrayDefined in contiguous-0.6.5.0 · Data.Primitive.Contiguous.Class
  • type Unlifted Array = Array#Defined in contiguous-0.6.5.0 · Data.Primitive.Contiguous.Class
  • type UnliftedMut Array = MutableArray#Defined in contiguous-0.6.5.0 · Data.Primitive.Contiguous.Class
datadata MutableArray s a

Mutable boxed arrays associated with a primitive state token.

Instances4Eq, Data, PrimUnlifted, Unlifted
datadata SmallArray a
#
Instances36Monad, Functor, MonadFix, MonadFail, Applicative, Foldable, …
datadata SmallMutableArray s a
#
Instances4Eq, Data, PrimUnlifted, Unlifted
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.

Instances19Contiguous, ContiguousU, Lift, IsList, Eq, Ord, …
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.

Instances4Eq, NFData, PrimUnlifted, Unlifted

A type synonym for an UnliftedArray_ containing lifted values of a particular type. As a general rule, this type synonym should not be used in class instances—use UnliftedArray_ with an equality constraint instead. It also should not be used when defining newtypes or datatypes, unless those will have restrictive type roles regardless—use UnliftedArray_ instead.