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

Modulecontiguous-0.6.4.2Haskell2010

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.

  • 10 types
  • 3 classes
  • 122 values
  • Packagecontiguous-0.6.4.2
  • Exports175
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceContiguous.hs

Accessors

0 declarations

Length Information

methodnull :: arr b -> Bool
#

Test whether the array is empty.

Indexing

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

Index into an array at the given index.

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

0 declarations

Initialisation

methodempty :: arr a
#

The empty array.

methodtripleton :: Element arr a => a -> a -> a -> arr a
#

Create a tripleton array.

methodquintupleton :: Element arr a => a -> a -> a -> a -> a -> arr a
#

Create a quintupleton array.

methodsextupleton :: Element arr a => a -> a -> a -> a -> a -> a -> arr a
#

Create a sextupleton array.

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

Construct an array of the given length by applying the function to each index.

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]
valueiterateMutableN
  1. :: (Contiguous arr, Element arr a, PrimMonad m)
  2. => Int
  3. -> a -> a
  4. -> a
  5. -> m (Mutable arr (PrimState m) a)
#

Apply a 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.

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

6 declarations
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

6 declarations
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.

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

Variant of modifyAt that forces the result before installing it in the array.

Permutations

Resizing

Elementwise operations

0 declarations

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.

valuemapMaybe
  1. :: (Contiguous arr1, Element arr1 a, Contiguous arr2, Element arr2 b)
  2. => a -> Maybe b
  3. -> arr1 a
  4. -> arr2 b
#

The mapMaybe function is a version of map which can throw out elements. In particular, the functional arguments returns something of type Maybe b. If this is Nothing, no element is added on to the result array. If it is Just b, then b is included in the result array.

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

0 declarations

Filtering

valueifilter
  1. :: (Contiguous arr, Element arr a)
  2. => Int -> a -> Bool
  3. -> arr a
  4. -> arr a
#

Drop elements that do not satisfy the predicate which is applied to values and their indices.

Searching

valuefind :: (Contiguous arr, Element arr a) => (a -> Bool) -> arr a -> Maybe a
#

find takes a predicate and an array, and returns the leftmost element of the array matching the prediate, or Nothing if there is no such element.

Comparing for equality

methodequals :: (Element arr b, Eq b) => arr b -> arr b -> Bool
#

Test the two arrays for equality.

methodequalsMut :: Mutable arr s a -> Mutable arr s a -> Bool
#

Test the two mutable arrays for pointer equality. Does not check equality of elements.

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

18 declarations
valuefoldl :: (Contiguous arr, Element arr a) => (b -> a -> b) -> b -> arr a -> b
#

Left fold over the elements of an array.

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

Strict left fold over the elements of an array.

valuefoldr :: (Contiguous arr, Element arr a) => (a -> b -> b) -> b -> arr a -> b
#

Right fold over the element of an array.

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

Strict right fold over the elements of an array.

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.

valuefoldlM'
  1. :: (Contiguous arr, Element arr a, Monad m)
  2. => b -> a -> m b
  3. -> b
  4. -> arr a
  5. -> m b
#

Strict left monadic fold over the elements of an array.

valuefoldrM'
  1. :: (Contiguous arr, Element arr a, Monad m)
  2. => a -> b -> m b
  3. -> b
  4. -> arr a
  5. -> m b
#

Strict right monadic fold over the elements of an array.

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

14 declarations
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_.

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

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

mapM_ = traverse_

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

for_ is traverse_ with its arguments flipped. For a version that doesn't ignore the results see for.

Example1 expression
for_ (C.fromList [1..4] :: PrimArray Int) print1234
valuesequence_
  1. :: (Contiguous arr, Element arr (f a), Applicative f)
  2. => arr (f a)
  3. -> f ()
#

Evaluate each action in the structure from left to right and ignore the results. For a version that doesn't ignore the results see sequence.

Typeclass method defaults

2 declarations
value(<$)
  1. :: (Contiguous arr1, Contiguous arr2, Element arr1 b, Element arr2 a)
  2. => a
  3. -> arr1 b
  4. -> arr2 a
#

Replace all locations in the input with the same value.

Equivalent to Data.Functor.Data.Functor.<$.

Prefix sums (scans)

10 declarations
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

0 declarations

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

methodliftMut :: UnliftedMut arr s b -> Mutable arr s b
#

Lift a mutable 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).

methodunliftMut :: Mutable arr s b -> UnliftedMut arr s b
#

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

Between mutable and immutable variants

Hashing

1 declaration

Forcing an array and its contents

1 declaration
methodrnf :: (NFData a, Element arr a) => arr a -> ()
#

Reduce the array and all of its elements to WHNF.

Classes

3 declarations
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.

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

Instances4ContiguousU
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.4.2 · Data.Primitive.Contiguous.Class

Re-Exports

8 declarations
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.4.2 · Data.Primitive.Contiguous.Class
  • ContiguousU ArrayDefined in contiguous-0.6.4.2 · 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.1.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.1.0.0 · Data.Primitive.Unlifted.Class
  • type Element Array = AlwaysDefined in contiguous-0.6.4.2 · Data.Primitive.Contiguous.Class
  • type Mutable Array = MutableArrayDefined in contiguous-0.6.4.2 · Data.Primitive.Contiguous.Class
  • type MutableSliced Array = MutableSlice ArrayDefined in contiguous-0.6.4.2 · Data.Primitive.Contiguous.Class
  • type Sliced Array = Slice ArrayDefined in contiguous-0.6.4.2 · Data.Primitive.Contiguous.Class
  • type Unlifted Array = Array#Defined in contiguous-0.6.4.2 · Data.Primitive.Contiguous.Class
  • type UnliftedMut Array = MutableArray#Defined in contiguous-0.6.4.2 · 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
typetype UnliftedArray a = UnliftedArray_ (Unlifted a) a
#

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.