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

Modulefingertree-0.1.6.2Haskell2010

Data.FingerTree

A general sequence representation with arbitrary annotations, for use as a base for implementations of various collection types, as described in section 4 of

For a directly usable sequence type, see Data.Sequence, which is a specialization of this structure.

An amortized running time is given for each operation, with n referring to the length of the sequence. These bounds hold even in a persistent (shared) setting.

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.

  • 4 types
  • 1 class
  • 26 values
  • Packagefingertree-0.1.6.2
  • Exports31
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceFingerTree.hs
datadata FingerTree v a
#

A representation of a sequence of values of type a, allowing access to the ends in constant time, and append and split in time logarithmic in the size of the smaller piece.

The collection is also parameterized by a measure type v, which is used to specify a position in the sequence for the split operation. The types of the operations enforce the constraint Measured v a, which also implies that the type v is determined by a.

A variety of abstract data types can be implemented by using different element types and measurements.

Instances10Measured, Foldable, Eq, Ord, Show, Generic, …
classclass Monoid v => Measured v a | a -> v where
#

Things that can be measured.

Methods

Instances5Measured
  • Measured v a => Measured v (Digit a)Defined in fingertree-0.1.6.2 · Data.FingerTree
  • Measured v a => Measured v (FingerTree v a)Defined in fingertree-0.1.6.2 · Data.FingerTree

    O(1). The cached measure of a tree.

  • Monoid v => Measured v (Node v a)Defined in fingertree-0.1.6.2 · Data.FingerTree
  • Ord v => Measured (IntInterval v) (Node v a)Defined in fingertree-0.1.6.2 · Data.IntervalMap.FingerTree
  • Ord k => Measured (Prio k v) (Entry k v)Defined in fingertree-0.1.6.2 · Data.PriorityQueue.FingerTree

Construction

6 declarations
value(<|) :: Measured v a => a -> FingerTree v a -> FingerTree v 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(|>) :: Measured v a => FingerTree v a -> a -> FingerTree v a
#

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

Deconstruction

1 declaration

Examining the ends

datadata ViewL (s :: Type -> Type) a
#

View of the left end of a sequence.

Constructors

  • EmptyL

    empty sequence

  • a :< s ainfixr 5

    leftmost element and the rest of the sequence

Instances8Functor, Eq, Ord, Read, Show, Generic, …
datadata ViewR (s :: Type -> Type) a
#

View of the right end of a sequence.

Constructors

  • EmptyR

    empty sequence

  • s a :> ainfixl 5

    the sequence minus the rightmost element, and the rightmost element

Instances8Functor, Eq, Ord, Read, Show, Generic, …

Search

datadata SearchResult v a
#

A result of search, attempting to find a point where a predicate on splits of the sequence changes from False to True.

Constructors

  • Position !(FingerTree v a) a !(FingerTree v a)

    A tree opened at a particular element: the prefix to the left, the element, and the suffix to the right.

  • OnLeft

    A position to the left of the sequence, indicating that the predicate is True at both ends.

  • OnRight

    A position to the right of the sequence, indicating that the predicate is False at both ends.

  • Nowhere

    No position in the tree, returned if the predicate is True at the left end and False at the right end. This will not occur if the predicate in monotonic on the tree.

Instances6Eq, Ord, Show, Generic, NFData, Rep
valuesearch
  1. :: Measured v a
  2. => v -> v -> Bool
  3. -> FingerTree v a
  4. -> SearchResult v a
#

O(log(min(i,n-i))). Search a sequence for a point where a predicate on splits of the sequence changes from False to True.

The argument p is a relation between the measures of the two sequences that could be appended together to form the sequence t. If the relation is False at the leftmost split and True at the rightmost split, i.e.

not (p mempty (measure t)) && p (measure t) mempty

then there must exist an element x in the sequence such that p is False for the split immediately before x and True for the split just after it:

image: images/search.svg

In this situation, search p t returns such an element x and the pieces l and r of the sequence to its left and right respectively. That is, it returns Position l x r such that

  • l >< (x <| r) = t
  • not (p (measure l) (measure (x <| r))
  • p (measure (l |> x)) (measure r)

For predictable results, one should ensure that there is only one such point, i.e. that the predicate is monotonic on t.

Splitting

These functions are special cases of search.

valuesplit
  1. :: Measured v a
  2. => v -> Bool
  3. -> FingerTree v a
  4. -> (FingerTree v a, FingerTree v a)
#

O(log(min(i,n-i))). Split a sequence at a point where the predicate on the accumulated measure of the prefix changes from False to True.

For predictable results, one should ensure that there is only one such point, i.e. that the predicate is monotonic.

Transformation

1 declaration

Maps

valuefmapWithContext
  1. :: (Measured v1 a1, Measured v2 a2)
  2. => v1 -> a1 -> v1 -> a2
  3. -> FingerTree v1 a1
  4. -> FingerTree v2 a2
#

Map all elements of the tree with a function that also takes the measure of the prefix to the left and of the suffix to the right of the element.

Folds

valuefoldlWithPos
  1. :: Measured v a
  2. => b -> v -> a -> b
  3. -> b
  4. -> FingerTree v a
  5. -> b
#

Fold the tree from the left with a function that also takes the measure of the prefix to the left of the element.

valuefoldrWithPos
  1. :: Measured v a
  2. => v -> a -> b -> b
  3. -> b
  4. -> FingerTree v a
  5. -> b
#

Fold the tree from the right with a function that also takes the measure of the prefix to the left of the element.

valuefoldlWithContext
  1. :: Measured v a
  2. => b -> v -> a -> v -> b
  3. -> b
  4. -> FingerTree v a
  5. -> b
#

Fold the tree from the left with a function that also takes the measure of the prefix to the left of the element and the measure of the suffix to the right of the element.

valuefoldrWithContext
  1. :: Measured v a
  2. => v -> a -> v -> b -> b
  3. -> b
  4. -> FingerTree v a
  5. -> b
#

Fold the tree from the right with a function that also takes the measure of the prefix to the left of the element and the measure of the suffix to the right of the element.

Traversals

Example

0 declarations

Particular abstract data types may be implemented by defining element types with suitable Measured instances.

(from section 4.5 of the paper) Simple sequences can be implemented using a Sum monoid as a measure:

newtype Elem a = Elem { getElem :: a }

instance Measured (Sum Int) (Elem a) where
    measure (Elem _) = Sum 1

newtype Seq a = Seq (FingerTree (Sum Int) (Elem a))

Then the measure of a subsequence is simply its length. This representation supports log-time extraction of subsequences:

take :: Int -> Seq a -> Seq a
take k (Seq xs) = Seq (takeUntil (> Sum k) xs)

drop :: Int -> Seq a -> Seq a
drop k (Seq xs) = Seq (dropUntil (> Sum k) xs)

The module Data.Sequence is an optimized instantiation of this type.

For further examples, see Data.IntervalMap.FingerTree and Data.PriorityQueue.FingerTree.