Documentation

CslibFrontier.DataStructure.Rope.Basic

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 #

Main results #

References #

inductive Cslib.DataStructure.Rope (α : Type u) :

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, read from the cache at an internal node; empty and leaves have height 0.

    The height computed from the structure, ignoring the caches.

    The number of leaves.

    The leaf chunks, from left to right.

    Whether the rope is the empty constructor.

    The cached length and height of every internal node are correct.

    Instances For

      Every leaf holds between 1 and B elements and every internal node has non-empty children, so the empty rope is only ever the empty constructor.

      Instances For

        At every internal node the structural heights of the children differ by at most one.

        Instances For
          structure Cslib.DataStructure.Rope.IsWF {α : Type u} (B : ℕ) (t : Rope α) :

          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 1 and B elements and internal nodes have non-empty children.

          • balanced : t.Balanced

            Sibling structural heights differ by at most one.

          Instances For
            @[simp]
            theorem Cslib.DataStructure.Rope.length_leaf {α : Type u} (xs : List α) :
            @[simp]
            theorem Cslib.DataStructure.Rope.length_node {α : Type u} (n h : ℕ) (l r : Rope α) :
            (node n h l r).length = n
            @[simp]
            theorem Cslib.DataStructure.Rope.height_leaf {α : Type u} (xs : List α) :
            (leaf xs).height = 0
            @[simp]
            theorem Cslib.DataStructure.Rope.height_node {α : Type u} (n h : ℕ) (l r : Rope α) :
            (node n h l r).height = h
            @[simp]
            theorem Cslib.DataStructure.Rope.chunks_leaf {α : Type u} (xs : List α) :
            (leaf xs).chunks = [xs]
            @[simp]
            theorem Cslib.DataStructure.Rope.chunks_node {α : Type u} (n h : ℕ) (l r : Rope α) :
            (node n h l r).chunks = l.chunks ++ r.chunks
            @[simp]
            @[simp]
            theorem Cslib.DataStructure.Rope.isEmpty_node {α : Type u} (n h : ℕ) (l r : Rope α) :
            (node n h l r).isEmpty = false
            theorem Cslib.DataStructure.Rope.isWF_leaf_iff {α : Type u} {B : ℕ} {xs : List α} :
            IsWF B (leaf xs) ↔ 0 < xs.length ∧ xs.length ≤ B
            theorem Cslib.DataStructure.Rope.IsWF.left {α : Type u} {B n h : ℕ} {l r : Rope α} (hw : IsWF B (node n h l r)) :
            IsWF B l

            The left child of a well-formed node is well-formed.

            theorem Cslib.DataStructure.Rope.IsWF.right {α : Type u} {B n h : ℕ} {l r : Rope α} (hw : IsWF B (node n h l r)) :
            IsWF B r

            The right child of a well-formed node is well-formed.

            @[instance_reducible]
            @[instance_reducible]
            @[instance_reducible]
            instance Cslib.DataStructure.Rope.instDecidableIsWF {α : Type u} (B : ℕ) (t : Rope α) :

            Cached height equals structural height when the caches are correct.

            def Cslib.DataStructure.Rope.mkNode {α : Type u} :
            Rope α → Rope α → Rope α

            A node over l and r with recomputed caches; an empty child is dropped instead.

            @[simp]
            theorem Cslib.DataStructure.Rope.chunked_mkNode {α : Type u} {B : ℕ} {l r : Rope α} (hl : Chunked B l) (hr : Chunked B r) :
            Chunked B (l.mkNode r)
            @[simp]
            theorem Cslib.DataStructure.Rope.mkNode_of_isEmpty_eq_false {α : Type u} {l r : Rope α} (hl : l.isEmpty = false) (hr : r.isEmpty = false) :
            l.mkNode r = node (l.length + r.length) (max l.height r.height + 1) l r

            Over two non-empty children, mkNode is a node with recomputed caches.

            theorem Cslib.DataStructure.Rope.height_mkNode {α : Type u} {l r : Rope α} (hl : l.isEmpty = false) (hr : r.isEmpty = false) :
            theorem Cslib.DataStructure.Rope.isWF_node_iff {α : Type u} {B n h : ℕ} {l r : Rope α} :

            Inversion of well-formedness at a node, with the balance condition on cached heights.

            theorem Cslib.DataStructure.Rope.isWF_mkNode {α : Type u} {B : ℕ} {l r : Rope α} (hl : IsWF B l) (hr : IsWF B r) (h₁ : l.height ≤ r.height + 1) (h₂ : r.height ≤ l.height + 1) :
            IsWF B (l.mkNode r)

            mkNode over well-formed children whose heights differ by at most one is well-formed.

            The list model and basic operations #

            def Cslib.DataStructure.Rope.get? {α : Type u} :
            Rope α → ℕ → Option α

            The element at index i, if any.

            def Cslib.DataStructure.Rope.map {α : Type u} {β : Type v} (f : α → β) :
            Rope α → Rope β

            Apply f to every element, keeping the shape and the caches.

            def Cslib.DataStructure.Rope.foldl {α : Type u} {β : Type v} (f : β → α → β) :
            β → Rope α → β

            Left fold over the elements.

            def Cslib.DataStructure.Rope.foldr {α : Type u} {β : Type v} (f : α → β → β) :
            β → Rope α → β

            Right fold over the elements.

            def Cslib.DataStructure.Rope.toList {α : Type u} (t : Rope α) :
            List α

            The elements of t in order: the list model of the rope. It is one right-to-left traversal that conses each element once.

            def Cslib.DataStructure.Rope.singleton {α : Type u} (x : α) :
            Rope α

            The one-element rope.

            def Cslib.DataStructure.Rope.ofList {α : Type u} (B : ℕ) (xs : List α) :
            Rope α

            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.

            @[instance_reducible]
            @[instance_reducible]
            @[simp]
            theorem Cslib.DataStructure.Rope.mem_toList {α : Type u} {x : α} {t : Rope α} :
            x ∈ t.toList ↔ x ∈ t

            Membership in the model is membership in the rope.

            @[simp]
            theorem Cslib.DataStructure.Rope.toList_leaf {α : Type u} (xs : List α) :
            (leaf xs).toList = xs
            @[simp]
            theorem Cslib.DataStructure.Rope.toList_node {α : Type u} (n h : ℕ) (l r : Rope α) :
            (node n h l r).toList = l.toList ++ r.toList
            @[simp]

            The model is the concatenation of the leaf chunks.

            The cached length is the length of the model when the caches are correct.

            @[simp]
            theorem Cslib.DataStructure.Rope.get?_leaf {α : Type u} (xs : List α) (i : ℕ) :
            (leaf xs).get? i = xs[i]?
            theorem Cslib.DataStructure.Rope.get?_eq_getElem? {α : Type u} {t : Rope α} (hc : t.CacheCorrect) (i : ℕ) :

            Indexing agrees with indexing the model; out-of-range indices give none.

            @[simp]
            theorem Cslib.DataStructure.Rope.map_empty {α : Type u} {β : Type v} (f : α → β) :
            @[simp]
            theorem Cslib.DataStructure.Rope.map_leaf {α : Type u} {β : Type v} (f : α → β) (xs : List α) :
            map f (leaf xs) = leaf (List.map f xs)
            @[simp]
            theorem Cslib.DataStructure.Rope.map_node {α : Type u} {β : Type v} (f : α → β) (n h : ℕ) (l r : Rope α) :
            map f (node n h l r) = node n h (map f l) (map f r)
            @[simp]
            theorem Cslib.DataStructure.Rope.toList_map {α : Type u} {β : Type v} (f : α → β) (t : Rope α) :
            @[simp]
            theorem Cslib.DataStructure.Rope.length_map {α : Type u} {β : Type v} (f : α → β) (t : Rope α) :
            (map f t).length = t.length
            @[simp]
            theorem Cslib.DataStructure.Rope.height_map {α : Type u} {β : Type v} (f : α → β) (t : Rope α) :
            (map f t).height = t.height
            @[simp]
            theorem Cslib.DataStructure.Rope.structuralHeight_map {α : Type u} {β : Type v} (f : α → β) (t : Rope α) :
            @[simp]
            theorem Cslib.DataStructure.Rope.isEmpty_map {α : Type u} {β : Type v} (f : α → β) (t : Rope α) :
            theorem Cslib.DataStructure.Rope.cacheCorrect_map {α : Type u} {β : Type v} (f : α → β) {t : Rope α} (hc : t.CacheCorrect) :
            theorem Cslib.DataStructure.Rope.chunked_map {α : Type u} {β : Type v} (f : α → β) {B : ℕ} {t : Rope α} (hc : Chunked B t) :
            Chunked B (map f t)
            theorem Cslib.DataStructure.Rope.balanced_map {α : Type u} {β : Type v} (f : α → β) {t : Rope α} (hb : t.Balanced) :
            theorem Cslib.DataStructure.Rope.isWF_map {α : Type u} {β : Type v} (f : α → β) {B : ℕ} {t : Rope α} (hw : IsWF B t) :
            IsWF B (map f t)
            theorem Cslib.DataStructure.Rope.foldl_eq {α : Type u} {β : Type v} (f : β → α → β) (z : β) (t : Rope α) :
            foldl f z t = List.foldl f z t.toList

            The left fold agrees with the left fold of the model.

            theorem Cslib.DataStructure.Rope.foldr_eq {α : Type u} {β : Type v} (f : α → β → β) (z : β) (t : Rope α) :
            foldr f z t = List.foldr f z t.toList

            The right fold agrees with the right fold of the model.

            theorem Cslib.DataStructure.Rope.isWF_singleton {α : Type u} {B : ℕ} (hB : 1 ≤ B) (x : α) :
            @[simp]
            theorem Cslib.DataStructure.Rope.toList_ofList {α : Type u} (B : ℕ) (xs : List α) :
            (ofList B xs).toList = xs

            Bulk construction represents its input.

            theorem Cslib.DataStructure.Rope.chunked_ofList {α : Type u} {B : ℕ} (hB : 1 ≤ B) (xs : List α) :
            Chunked B (ofList B xs)

            Bulk construction produces chunks of between 1 and B elements.

            theorem Cslib.DataStructure.Rope.balanced_ofList {α : Type u} (B : ℕ) (xs : List α) :

            Bulk construction is balanced.

            theorem Cslib.DataStructure.Rope.isWF_ofList {α : Type u} {B : ℕ} (hB : 1 ≤ B) (xs : List α) :
            IsWF B (ofList B xs)

            Bulk construction is well-formed.