persistent-vector: A persistent sequence based on array mapped tries

[ bsd3, data, library ] [ Propose Tags ]

This package provides persistent vectors based on array mapped tries. The implementation is based on the persistent vectors used in clojure, but in a Haskell-style API. The API is modeled after Data.Sequence from the containers library.

Technically, the element-wise operations are O(log(n)), but the underlying tree cannot be more than 7 or 8 levels deep so this is effectively constant time.

One change from the clojure implementation is that this version supports O(1) slicing, though it does cheat a little. Slices retain references to elements that cannot be indexed. These extra references (and the space they occupy) can be reclaimed by shrinking the slice. This seems like a reasonable tradeoff, and, I believe, mirrors the behavior of the vector library.


  • O(1) append element, indexing, updates, length, and slicing

  • Reasonably compact representation

[Skip to Readme]


Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees


Versions [RSS],,, 0.1.1, 0.2.0
Change log
Dependencies base (>=4 && <5), deepseq (>=1 && <1.5), semigroups (>=0.18 && <0.19), transformers (>=0.3 && <0.6) [details]
License BSD-3-Clause
Author Tristan Ravitch
Category Data
Home page
Bug tracker
Source repo head: git clone git://
Uploaded by TristanRavitch at 2020-10-29T06:21:47Z
Reverse Dependencies 3 direct, 46 indirect [details]
Downloads 3434 total (19 in the last 30 days)
Rating 2.0 (votes: 1) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2020-10-29 [all 1 reports]

Readme for persistent-vector-0.2.0

[back to package description]

Persistent Vector

A library providing persistent (purely functional) vectors for Haskell based on array mapped tries.


These persistent vectors are modeled on the persistent vector used by clojure, with an API modeled after Data.Sequence from the containers library. This data structure is spine strict and is not useful for incremental consumption. If you need that, stick to lists. It is still lazy in the elements.

While per-element operations are O(log(n)), the internal tree can never be more than 7 or 8 deep. Thus, they are effectively constant time.

This implementation adds O(1) slicing support for vectors that I do not believe clojure supports. The implementation cheats, though, and slices can retain references to objects that cannot be indexed.


Performance is an important consideration for a data structure like this. The package contains a criterion benchmark suite that attempts to compare the performance of persistent vectors against a variety of existing persistent data structures. As an overview of the results I have observed:

  • Traversing and building lists is faster than the same operations with persistent vectors.

  • (Strict) left folds over persistent vectors are faster than left folds over Sequences. Right folds over Sequences are faster than right folds over vectors.

  • Indexing persistent vectors is faster than indexing sequences and IntMaps (and, of course, lists).

  • Appending to vectors is slightly faster than appending to a Sequence. It is much faster than appending to an IntMap.

  • Updating an element at an index in a vector is slower than updating an index in a Sequence (but still faster than an IntMap).

Overall, it seems like persistent vectors are efficient at most tasks. If you only need a (strict) left fold, they are efficient for traversal. Indexing and construction are very fast, but Sequences are superior for element-wise updates.



  • More of the Data.Sequence API

  • More efficient Eq and Ord instances. This is tricky in the presence of slicing. There are faster implementations for unsliced inputs.

  • Implement something to make parallel reductions simple (maybe something like vector-strategies)

  • Implement cons. Cons can use the space that is hidden by the offset cheaply. It can also make a variant of pushTail (pushHead) that allocates fragments of preceeding sub-trees. Each cons call will modify the offset of its result vector.