Ropes: structure, list model and basic API #
A rope is a persistent sequence stored as a binary tree whose leaves hold non-empty chunks of at
most B elements and whose internal nodes cache their length and height. This file defines the
raw tree, its three independent well-formedness predicates and the basic API, and proves that
every operation agrees with the List model given by toList. Bulk construction is
well-formed. Rotations and the height bound are in Rope.Balance, and join is in Rope.Join.
Split is an open objective of cslib-community/cslib-frontier#3670.
Rope α is a plain inductive type with no proof fields, so malformed trees can be written down.
Well-formedness IsWF B t is a hypothesis of theorems, never part of the type, and splits into
CacheCorrect (cached fields are correct), Chunked B (leaves have between 1 and B
elements, and internal nodes have non-empty children) and Balanced (sibling structural heights
differ by at most one). Lemmas assume only the parts they use. Both empty and a leaf have height
0, so height counts internal nodes on the longest root-to-leaf path. Different trees can
represent the same list, so there is no structural extensionality.
The operations and predicates are @[expose]d so that decide and rfl evaluate them from
other modules.
Main definitions #
Cslib.DataStructure.Rope: the raw rope tree.Rope.length,Rope.height,Rope.structuralHeight,Rope.leaves: cached and structural size measures.Rope.chunks: the leaf chunks in order.Rope.CacheCorrect,Rope.Chunked,Rope.Balanced,Rope.IsWF: the well-formedness predicates, each decidable.Rope.mkNode: a node with recomputed caches, eliminating empty children.Rope.toList,Rope.get?,Rope.map,Rope.foldl,Rope.foldr,Rope.singleton: the list model and the basic operations.Rope.ofList: bulk construction from the chunksxs.toChunks (max 1 B), combined byList.balancedFold.
Main results #
Rope.length_eq_length_toList,Rope.height_eq_structuralHeight: cached fields agree with the model and the structure.Rope.flatten_chunks: the model is the concatenation of the leaf chunks.Rope.get?_eq_getElem?,Rope.toList_map,Rope.foldl_eq,Rope.foldr_eq: the operations agree with theListmodel. Membership is membership intoList(Rope.mem_toList).Rope.toList_ofList,Rope.isWF_ofList: bulk construction represents its input and, for1 ≤ B, is well-formed.Rope.isWF_map: mapping preserves well-formedness.Rope.IsWF.left,Rope.IsWF.right: the children of a well-formed node are well-formed.Rope.isWF_node_iff: inversion of well-formedness at a node, on cached heights.Rope.isWF_mkNode:mkNodeover well-formed children whose heights differ by at most one is well-formed.
References #
A raw rope over α: the empty rope, a leaf holding a chunk, or an internal node caching its
length and height. Well-formedness is the separate predicate Rope.IsWF.
- empty {α : Type u} : Rope α
The empty rope.
- leaf {α : Type u} (xs : List α) : Rope α
A leaf holding a chunk of elements.
- node {α : Type u} (cachedLength cachedHeight : ℕ) (left right : Rope α) : Rope α
An internal node with cached length and height.
Instances For
Structure and well-formedness #
The number of elements, read from the cache at an internal node.
The height computed from the structure, ignoring the caches.
The cached length and height of every internal node are correct.
Instances For
At every internal node the structural heights of the children differ by at most one.
Instances For
Well-formedness at chunk bound B: correct caches, bounded non-empty chunks and balance.
- cacheCorrect : t.CacheCorrect
The cached fields are correct.
- chunked : Chunked B t
Leaves hold between
1andBelements and internal nodes have non-empty children. - balanced : t.Balanced
Sibling structural heights differ by at most one.
Instances For
Cached height equals structural height when the caches are correct.
Inversion of well-formedness at a node, with the balance condition on cached heights.
The list model and basic operations #
Left fold over the elements.
Right fold over the elements.
The elements of t in order: the list model of the rope. It is one right-to-left traversal
that conses each element once.
Bulk construction: one leaf per chunk of xs.toChunks (max 1 B), combined by
List.balancedFold. At every node the two sides receive ⌊n / 2⌋ and ⌈n / 2⌉ of the n
chunks. Every chunk is non-empty, so mkNode never drops a leaf, and the shape depends only on
the number of chunks.
The cached length is the length of the model when the caches are correct.
The left fold agrees with the left fold of the model.
The right fold agrees with the right fold of the model.