HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Modulecontainers-0.7Haskell2010

Data.Sequence.Internal

WARNING

This module is considered internal.

The Package Versioning Policy does not apply.

The contents of this module may change in any way whatsoever and without any warning between minor versions of this package.

Authors importing this module are expected to track development closely.

Description

General purpose finite sequences. Apart from being finite and having strict operations, sequences also differ from lists in supporting a wider variety of operations efficiently.

An amortized running time is given for each operation, with n referring to the length of the sequence and i being the integral index used by some operations. These bounds hold even in a persistent (shared) setting.

The implementation uses 2-3 finger trees annotated with sizes, as described in section 4.2 of

Note: Many of these operations have the same names as similar operations on lists in the Prelude. The ambiguity may be resolved using either qualification or the hiding clause.

Warning: The size of a Seq must not exceed maxBound::Int. Violation of this condition is not detected and if the size limit is exceeded, the behaviour of the sequence is undefined. This is unlikely to occur in most applications, but some care may be required when using ><, <*>, *>, or >>, particularly repeatedly and particularly in combination with replicate or fromFunction.

  • 8 types
  • 2 classes
  • 76 values
  • Packagecontainers-0.7
  • Exports89
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceInternal.hs
newtypenewtype Elem a
#

Constructors

Instances11Functor, Foldable, Traversable, UnzipWith, Generic1, Generic, …
datadata FingerTree a
#

Constructors

Instances11Functor, Foldable, Traversable, UnzipWith, Generic1, Lift, …
datadata Node a
#

Constructors

Instances12Functor, Foldable, Traversable, UnzipWith, Generic1, Lift, …
datadata Digit a
#

Constructors

Instances11Functor, Foldable, Traversable, UnzipWith, Generic1, Lift, …
classclass Sized a where
#

Methods

Instances5Sized
  • Sized (Elem a)Defined in containers-0.7 · Data.Sequence.Internal
  • Sized (ForceBox a)Defined in containers-0.7 · Data.Sequence.Internal
  • Sized (Node a)Defined in containers-0.7 · Data.Sequence.Internal
  • Sized a => Sized (Digit a)Defined in containers-0.7 · Data.Sequence.Internal
  • Sized a => Sized (FingerTree a)Defined in containers-0.7 · Data.Sequence.Internal
classclass MaybeForce a where
#
Instances3MaybeForce
  • MaybeForce (Elem a)Defined in containers-0.7 · Data.Sequence.Internal
  • MaybeForce (ForceBox a)Defined in containers-0.7 · Data.Sequence.Internal
  • MaybeForce (Node a)Defined in containers-0.7 · Data.Sequence.Internal
newtypenewtype Seq a
#

General-purpose finite sequences.

Constructors

Instances26Monad, Functor, MonadFix, Applicative, Foldable, Traversable, …
  • Monad SeqDefined in containers-0.7 · Data.Sequence.Internal
  • Functor SeqDefined in containers-0.7 · Data.Sequence.Internal
  • MonadFix SeqDefined in containers-0.7 · Data.Sequence.Internal
  • Applicative SeqDefined in containers-0.7 · Data.Sequence.Internal
  • Foldable SeqDefined in containers-0.7 · Data.Sequence.Internal
  • Traversable SeqDefined in containers-0.7 · Data.Sequence.Internal
  • Alternative SeqDefined in containers-0.7 · Data.Sequence.Internal
  • MonadPlus SeqDefined in containers-0.7 · Data.Sequence.Internal
  • MonadZip SeqDefined in containers-0.7 · Data.Sequence.Internal
  • Eq1 SeqDefined in containers-0.7 · Data.Sequence.Internal
  • Ord1 SeqDefined in containers-0.7 · Data.Sequence.Internal
  • Read1 SeqDefined in containers-0.7 · Data.Sequence.Internal
  • Show1 SeqDefined in containers-0.7 · Data.Sequence.Internal
  • UnzipWith SeqDefined in containers-0.7 · Data.Sequence.Internal
  • Lift a => Lift (Seq a)Defined in containers-0.7 · Data.Sequence.Internal
  • IsList (Seq a)Defined in containers-0.7 · Data.Sequence.Internal
  • Eq a => Eq (Seq a)Defined in containers-0.7 · Data.Sequence.Internal
  • Data a => Data (Seq a)Defined in containers-0.7 · Data.Sequence.Internal
  • Ord a => Ord (Seq a)Defined in containers-0.7 · Data.Sequence.Internal
  • Read a => Read (Seq a)Defined in containers-0.7 · Data.Sequence.Internal
  • Show a => Show (Seq a)Defined in containers-0.7 · Data.Sequence.Internal
  • a ~ Char => IsString (Seq a)Defined in containers-0.7 · Data.Sequence.Internal
  • Semigroup (Seq a)Defined in containers-0.7 · Data.Sequence.Internal
  • Monoid (Seq a)Defined in containers-0.7 · Data.Sequence.Internal
  • NFData a => NFData (Seq a)Defined in containers-0.7 · Data.Sequence.Internal
  • type Item (Seq a) = aDefined in containers-0.7 · Data.Sequence.Internal
patternpattern Empty :: Seq a
#

A bidirectional pattern synonym matching an empty sequence.

patternpattern (:<|) :: a -> Seq a -> Seq a
#

A bidirectional pattern synonym viewing the front of a non-empty sequence.

patternpattern (:|>) :: Seq a -> a -> Seq a
#

A bidirectional pattern synonym viewing the rear of a non-empty sequence.

newtypenewtype State s a
#

Constructors

Instances3Monad, Functor, Applicative
  • Monad (State s)Defined in containers-0.7 · Utils.Containers.Internal.State
  • Functor (State s)Defined in containers-0.7 · Utils.Containers.Internal.State
  • Applicative (State s)Defined in containers-0.7 · Utils.Containers.Internal.State

Construction

8 declarations
valueempty :: Seq a
#

O(1) . The empty sequence.

value(<|) :: a -> Seq a -> Seq a
#

O(1) . Add an element to the left end of a sequence. Mnemonic: a triangle with the single element at the pointy end.

value(|>) :: Seq a -> a -> Seq a
#

O(1) . Add an element to the right end of a sequence. Mnemonic: a triangle with the single element at the pointy end.

value(><) :: Seq a -> Seq a -> Seq a
#

O(\log(\min(n_1,n_2))) . Concatenate two sequences.

valuefromList :: [a] -> Seq a
#

O(n) . Create a sequence from a finite list of elements. There is a function toList in the opposite direction for all instances of the Foldable class, including Seq.

valuefromFunction :: Int -> (Int -> a) -> Seq a
#

O(n) . Convert a given sequence length and a function representing that sequence into a sequence.

valuefromArray :: Ix i => Array i a -> Seq a
#

O(n) . Create a sequence consisting of the elements of an Array. Note that the resulting sequence elements may be evaluated lazily (as on GHC), so you must force the entire structure to be sure that the original array can be garbage-collected.

Repetition

valuereplicate :: Int -> a -> Seq a
#

O(\log n) . replicate n x is a sequence consisting of n copies of x.

valuecycleTaking :: Int -> Seq a -> Seq a
#

O(\log k). cycleTaking k xs forms a sequence of length k by repeatedly concatenating xs with itself. xs may only be empty if k is 0.

Property
cycleTaking k = fromList . take k . cycle . toList

Iterative construction

valueiterateN :: Int -> (a -> a) -> a -> Seq a
#

O(n) . Constructs a sequence by repeated application of a function to a seed value.

iterateN n f x = fromList (Prelude.take n (Prelude.iterate f x))
valueunfoldr :: (b -> Maybe (a, b)) -> b -> Seq a
#

Builds a sequence from a seed value. Takes time linear in the number of generated elements. WARNING: If the number of generated elements is infinite, this method will not terminate.

Deconstruction

0 declarations

Additional functions for deconstructing sequences are available via the Foldable instance of Seq.

Queries

valuenull :: Seq a -> Bool
#

O(1) . Is this the empty sequence?

valuelength :: Seq a -> Int
#

O(1) . The number of elements in the sequence.

Views

datadata ViewL a
#

View of the left end of a sequence.

Constructors

  • EmptyL

    empty sequence

  • a :< Seq ainfixr 5

    leftmost element and the rest of the sequence

Instances13Functor, Foldable, Traversable, Generic1, Lift, Eq, …
valueviewl :: Seq a -> ViewL a
#

O(1) . Analyse the left end of a sequence.

datadata ViewR a
#

View of the right end of a sequence.

Constructors

  • EmptyR

    empty sequence

  • Seq a :> ainfixl 5

    the sequence minus the rightmost element, and the rightmost element

Instances13Functor, Foldable, Traversable, Generic1, Lift, Eq, …
valueviewr :: Seq a -> ViewR a
#

O(1) . Analyse the right end of a sequence.

Scans

4 declarations
valuescanl :: (a -> b -> a) -> a -> Seq b -> Seq a
#

scanl is similar to foldl, but returns a sequence of reduced values from the left:

scanl f z (fromList [x1, x2, ...]) = fromList [z, z `f` x1, (z `f` x1) `f` x2, ...]
valuescanl1 :: (a -> a -> a) -> Seq a -> Seq a
#

scanl1 is a variant of scanl that has no starting value argument:

scanl1 f (fromList [x1, x2, ...]) = fromList [x1, x1 `f` x2, ...]

Sublists

3 declarations
valuetails :: Seq a -> Seq (Seq a)
#

O(n) . Returns a sequence of all suffixes of this sequence, longest first. For example,

tails (fromList "abc") = fromList [fromList "abc", fromList "bc", fromList "c", fromList ""]

Evaluating the i th suffix takes O(\log(\min(i, n-i))) , but evaluating every suffix in the sequence takes O(n) due to sharing.

valueinits :: Seq a -> Seq (Seq a)
#

O(n) . Returns a sequence of all prefixes of this sequence, shortest first. For example,

inits (fromList "abc") = fromList [fromList "", fromList "a", fromList "ab", fromList "abc"]

Evaluating the i th prefix takes O(\log(\min(i, n-i))) , but evaluating every prefix in the sequence takes O(n) due to sharing.

valuechunksOf :: Int -> Seq a -> Seq (Seq a)
#

O \Bigl(\bigl(\frac{n}{c}\bigr) \log c\Bigr). chunksOf c xs splits xs into chunks of size c>0. If c does not divide the length of xs evenly, then the last element of the result will be short.

Side note: the given performance bound is missing some messy terms that only really affect edge cases. Performance degrades smoothly from O(1) (for c = n ) to O(n) (for c = 1 ). The true bound is more like O \Bigl( \bigl(\frac{n}{c} - 1\bigr) (\log (c + 1)) + 1 \Bigr)

Sequential searches

valuetakeWhileL :: (a -> Bool) -> Seq a -> Seq a
#

O(i) where i is the prefix length. takeWhileL, applied to a predicate p and a sequence xs, returns the longest prefix (possibly empty) of xs of elements that satisfy p.

valuespanl :: (a -> Bool) -> Seq a -> (Seq a, Seq a)
#

O(i) where i is the prefix length. spanl, applied to a predicate p and a sequence xs, returns a pair whose first element is the longest prefix (possibly empty) of xs of elements that satisfy p and the second element is the remainder of the sequence.

valuespanr :: (a -> Bool) -> Seq a -> (Seq a, Seq a)
#

O(i) where i is the suffix length. spanr, applied to a predicate p and a sequence xs, returns a pair whose first element is the longest suffix (possibly empty) of xs of elements that satisfy p and the second element is the remainder of the sequence.

valuebreakl :: (a -> Bool) -> Seq a -> (Seq a, Seq a)
#

O(i) where i is the breakpoint index. breakl, applied to a predicate p and a sequence xs, returns a pair whose first element is the longest prefix (possibly empty) of xs of elements that do not satisfy p and the second element is the remainder of the sequence.

breakl p is equivalent to spanl (not . p).

valuepartition :: (a -> Bool) -> Seq a -> (Seq a, Seq a)
#

O(n) . The partition function takes a predicate p and a sequence xs and returns sequences of those elements which do and do not satisfy the predicate.

valuefilter :: (a -> Bool) -> Seq a -> Seq a
#

O(n) . The filter function takes a predicate p and a sequence xs and returns a sequence of those elements which satisfy the predicate.

Indexing

11 declarations
valuelookup :: Int -> Seq a -> Maybe a
#

O(\log(\min(i,n-i))) . The element at the specified position, counting from 0. If the specified position is negative or at least the length of the sequence, lookup returns Nothing.

Property
0 <= i < length xs ==> lookup i xs == Just (toList xs !! i)
Property
i < 0 || i >= length xs ==> lookup i xs = Nothing

Unlike index, this can be used to retrieve an element without forcing it. For example, to insert the fifth element of a sequence xs into a Data.Map.Lazy.Map m at key k, you could use

case lookup 5 xs of
  Nothing -> m
  Just x -> insert k x m
value(!?) :: Seq a -> Int -> Maybe a
#

O(\log(\min(i,n-i))) . A flipped, infix version of lookup.

valueindex :: Seq a -> Int -> a
#

O(\log(\min(i,n-i))) . The element at the specified position, counting from 0. The argument should thus be a non-negative integer less than the size of the sequence. If the position is out of range, index fails with an error.

Property
xs `index` i = toList xs !! i

Caution: index necessarily delays retrieving the requested element until the result is forced. It can therefore lead to a space leak if the result is stored, unforced, in another structure. To retrieve an element immediately without forcing it, use lookup or (!?).

valueadjust :: (a -> a) -> Int -> Seq a -> Seq a
#

O(\log(\min(i,n-i))) . Update the element at the specified position. If the position is out of range, the original sequence is returned. adjust can lead to poor performance and even memory leaks, because it does not force the new value before installing it in the sequence. adjust' should usually be preferred.

valueadjust' :: (a -> a) -> Int -> Seq a -> Seq a
#

O(\log(\min(i,n-i))) . Update the element at the specified position. If the position is out of range, the original sequence is returned. The new value is forced before it is installed in the sequence.

adjust' f i xs =
 case xs !? i of
   Nothing -> xs
   Just x -> let !x' = f x
             in update i x' xs
valueupdate :: Int -> a -> Seq a -> Seq a
#

O(\log(\min(i,n-i))) . Replace the element at the specified position. If the position is out of range, the original sequence is returned.

valuetake :: Int -> Seq a -> Seq a
#

O(\log(\min(i,n-i))) . The first i elements of a sequence. If i is negative, take i s yields the empty sequence. If the sequence contains fewer than i elements, the whole sequence is returned.

valuedrop :: Int -> Seq a -> Seq a
#

O(\log(\min(i,n-i))) . Elements of a sequence after the first i. If i is negative, drop i s yields the whole sequence. If the sequence contains fewer than i elements, the empty sequence is returned.

valueinsertAt :: Int -> a -> Seq a -> Seq a
#

O(\log(\min(i,n-i))) . insertAt i x xs inserts x into xs at the index i, shifting the rest of the sequence over.

insertAt 2 x (fromList [a,b,c,d]) = fromList [a,b,x,c,d]
insertAt 4 x (fromList [a,b,c,d]) = insertAt 10 x (fromList [a,b,c,d])
                                  = fromList [a,b,c,d,x]
Property
insertAt i x xs = take i xs >< singleton x >< drop i xs
valuedeleteAt :: Int -> Seq a -> Seq a
#

O(\log(\min(i,n-i))) . Delete the element of a sequence at a given index. Return the original sequence if the index is out of range.

deleteAt 2 [a,b,c,d] = [a,b,d]
deleteAt 4 [a,b,c,d] = deleteAt (-1) [a,b,c,d] = [a,b,c,d]

Indexing with predicates

These functions perform sequential searches from the left or right ends of the sequence, returning indices of matching elements.

Folds

3 declarations

General folds are available via the Foldable instance of Seq.

Transformations

5 declarations
valuemapWithIndex :: (Int -> a -> b) -> Seq a -> Seq b
#

A generalization of fmap, mapWithIndex takes a mapping function that also depends on the element's index, and applies it to every element in the sequence.

valuereverse :: Seq a -> Seq a
#

O(n) . The reverse of a sequence.

valueintersperse :: a -> Seq a -> Seq a
#

O(n) . Intersperse an element between the elements of a sequence.

intersperse a empty = empty
intersperse a (singleton x) = singleton x
intersperse a (fromList [x,y]) = fromList [x,a,y]
intersperse a (fromList [x,y,z]) = fromList [x,a,y,a,z]

Zips and unzips

valuezip :: Seq a -> Seq b -> Seq (a, b)
#

O(\min(n_1,n_2)) . zip takes two sequences and returns a sequence of corresponding pairs. If one input is short, excess elements are discarded from the right end of the longer sequence.

valuezipWith :: (a -> b -> c) -> Seq a -> Seq b -> Seq c
#

O(\min(n_1,n_2)) . zipWith generalizes zip by zipping with the function given as the first argument, instead of a tupling function. For example, zipWith (+) is applied to two sequences to take the sequence of corresponding sums.

valuezip3 :: Seq a -> Seq b -> Seq c -> Seq (a, b, c)
#

O(\min(n_1,n_2,n_3)) . zip3 takes three sequences and returns a sequence of triples, analogous to zip.

valuezipWith3 :: (a -> b -> c -> d) -> Seq a -> Seq b -> Seq c -> Seq d
#

O(\min(n_1,n_2,n_3)) . zipWith3 takes a function which combines three elements, as well as three sequences and returns a sequence of their point-wise combinations, analogous to zipWith.

valuezip4 :: Seq a -> Seq b -> Seq c -> Seq d -> Seq (a, b, c, d)
#

O(\min(n_1,n_2,n_3,n_4)) . zip4 takes four sequences and returns a sequence of quadruples, analogous to zip.

valuezipWith4
  1. :: a -> b -> c -> d -> e
  2. -> Seq a
  3. -> Seq b
  4. -> Seq c
  5. -> Seq d
  6. -> Seq e
#

O(\min(n_1,n_2,n_3,n_4)) . zipWith4 takes a function which combines four elements, as well as four sequences and returns a sequence of their point-wise combinations, analogous to zipWith.

valueunzip :: Seq (a, b) -> (Seq a, Seq b)
#

Unzip a sequence of pairs.

unzip ps = ps `seq` (fmap fst ps) (fmap snd ps)

Example:

unzip $ fromList [(1,"a"), (2,"b"), (3,"c")] =
  (fromList [1,2,3], fromList ["a", "b", "c"])

See the note about efficiency at unzipWith.

valueunzipWith :: (a -> (b, c)) -> Seq a -> (Seq b, Seq c)
#

O(n) . Unzip a sequence using a function to divide elements.

 unzipWith f xs == unzip (fmap f xs)

Efficiency note:

unzipWith produces its two results in lockstep. If you calculate unzipWith f xs and fully force either of the results, then the entire structure of the other one will be built as well. This behavior allows the garbage collector to collect each calculated pair component as soon as it dies, without having to wait for its mate to die. If you do not need this behavior, you may be better off simply calculating the sequence of pairs and using fmap to extract each component sequence.