The areas / V33. Data structures and computation
Mathlib.Data.Ordmap.Ordnode
121 declarations. Mathlib docs · Source
Ordered sets
This file defines a data structure for ordered sets, supporting a variety of useful operations including insertion and deletion, logarithmic time lookup, set operations, folds, and conversion from lists.
The Ordnode α operations all assume that α has the structure of
a total preorder, meaning a ≤ operation that is
- Transitive:
x ≤ y → y ≤ z → x ≤ z - Reflexive:
x ≤ x - Total:
x ≤ y ∨ y ≤ x
For example, in order to use this data structure as a map type, one
can store pairs (k, v) where (k, v) ≤ (k', v') is defined to mean
k ≤ k' (assuming that the key values are linearly ordered).
Two values x,y are equivalent if x ≤ y and y ≤ x. An Ordnode α
maintains the invariant that it never stores two equivalent nodes;
the insertion operation comes with two variants depending on whether
you want to keep the old value or the new value in case you insert a value
that is equivalent to one in the set.
The operations in this file are not verified, in the sense that they provide
"raw operations" that work for programming purposes but the invariants
are not explicitly in the structure. See Ordset for a verified version
of this data structure.
Main definitions
Ordnode α: A set of values of typeα
Implementation notes
Based on weight balanced trees:
- Stephen Adams, "Efficient sets: a balancing act", Journal of Functional Programming 3(4):553-562, October 1993, http://www.swiss.ai.mit.edu/~adams/BB/.
- J. Nievergelt and E.M. Reingold, "Binary search trees of bounded balance", SIAM journal of computing 2(1), March 1973.
Ported from Haskell's Data.Set.
Tags
ordered map, ordered set, data structure
abbrev Ordnode.ctorElimType E1 #
{α : Type u} → {motive : Ordnode α → Sort u_1} → ℕ → Sort (max 1 u_1 (imax (u + 1) (u + 1) (u + 1) u_1))
theorem BinaryTree.toOrdNode.match_1.congr_eq_2 E1 #
∀ {α : Type u_1} (motive : BinaryTree α → Sort u_2) (x : BinaryTree α) (h_1 : Unit → motive BinaryTree.nil)
(h_2 : (x : α) → (l r : BinaryTree α) → motive (BinaryTree.node x l r)) (x_1 : α) (l r : BinaryTree α),
x = BinaryTree.node x_1 l r →
(match x with
| BinaryTree.nil => h_1 ()
| BinaryTree.node x l r => h_2 x l r) ≍
h_2 x_1 l r
abbrev Ordnode.brecOn.go E1 #
{α : Type u} →
{motive : Ordnode α → Sort u_1} →
(t : Ordnode α) → ((t : Ordnode α) → Ordnode.below t → motive t) → motive t ×' Ordnode.below t
theorem Ordnode.size.match_1.congr_eq_2 E1 #
∀ {α : Type u_1} (motive : Ordnode α → Sort u_2) (x : Ordnode α) (h_1 : Unit → motive Ordnode.nil)
(h_2 : (sz : ℕ) → (l : Ordnode α) → (x : α) → (r : Ordnode α) → motive (Ordnode.node sz l x r)) (sz : ℕ)
(l : Ordnode α) (x_1 : α) (r : Ordnode α),
x = Ordnode.node sz l x_1 r →
(match x with
| Ordnode.nil => h_1 ()
| Ordnode.node sz l x r => h_2 sz l x r) ≍
h_2 sz l x_1 r
theorem BinaryTree.toOrdNode.match_1.congr_eq_1 E1 #
∀ {α : Type u_1} (motive : BinaryTree α → Sort u_2) (x : BinaryTree α) (h_1 : Unit → motive BinaryTree.nil)
(h_2 : (x : α) → (l r : BinaryTree α) → motive (BinaryTree.node x l r)),
x = BinaryTree.nil →
(match x with
| BinaryTree.nil => h_1 ()
| BinaryTree.node x l r => h_2 x l r) ≍
h_1 ()
theorem Ordnode.size.match_1.congr_eq_1 E1 #
∀ {α : Type u_1} (motive : Ordnode α → Sort u_2) (x : Ordnode α) (h_1 : Unit → motive Ordnode.nil)
(h_2 : (sz : ℕ) → (l : Ordnode α) → (x : α) → (r : Ordnode α) → motive (Ordnode.node sz l x r)),
x = Ordnode.nil →
(match x with
| Ordnode.nil => h_1 ()
| Ordnode.node sz l x r => h_2 sz l x r) ≍
h_1 ()
theorem Ordnode.node.inj E1 #
∀ {α : Type u} {size : ℕ} {l : Ordnode α} {x : α} {r : Ordnode α} {size_1 : ℕ} {l_1 : Ordnode α} {x_1 : α}
{r_1 : Ordnode α},
Ordnode.node size l x r = Ordnode.node size_1 l_1 x_1 r_1 → size = size_1 ∧ l = l_1 ∧ x = x_1 ∧ r = r_1
theorem Ordnode.brecOn.eq E1 #
∀ {α : Type u} {motive : Ordnode α → Sort u_1} (t : Ordnode α) (F_1 : (t : Ordnode α) → Ordnode.below t → motive t),
Ordnode.brecOn t F_1 = F_1 t (Ordnode.brecOn.go t F_1).2
theorem Ordnode.node.injEq E0 #
∀ {α : Type u} (size : ℕ) (l : Ordnode α) (x : α) (r : Ordnode α) (size_1 : ℕ) (l_1 : Ordnode α) (x_1 : α)
(r_1 : Ordnode α),
(Ordnode.node size l x r = Ordnode.node size_1 l_1 x_1 r_1) = (size = size_1 ∧ l = l_1 ∧ x = x_1 ∧ r = r_1)
def Ordnode.ctorIdx E1 #
{α : Type u} → Ordnode α → ℕ
inductive Ordnode E3 #
Type u → Type u
An Ordnode α is a finite set of values, represented as a tree.
The operations on this type maintain that the tree is balanced
and correctly stores subtree sizes at each level.
This is a copy of BinaryTree with a cached size field for BinaryTree.numNodes.
ctor Ordnode.nil E0 #
{α : Type u} → Ordnode α
ctor Ordnode.node E0 #
{α : Type u} → ℕ → Ordnode α → α → Ordnode α → Ordnode α
instance Ordnode.instEmptyCollection E0 #
{α : Type u_1} → EmptyCollection (Ordnode α)
instance Ordnode.instInhabited E0 #
{α : Type u_1} → Inhabited (Ordnode α)
def Ordnode.delta E1 #
ℕ
Internal use only
The maximal relative difference between the sizes of
two trees, it corresponds with the w in Adams' paper.
According to the Haskell comment, only (delta, ratio) settings
of (3, 2) and (4, 2) will work, and the proofs in
Ordset.lean assume delta := 3 and ratio := 2.
def Ordnode.ratio E1 #
ℕ
Internal use only
The ratio between an outer and inner sibling of the
heavier subtree in an unbalanced setting. It determines
whether a double or single rotation should be performed
to restore balance. It is corresponds with the inverse
of α in Adam's article.
def Ordnode.singleton E1 #
{α : Type u_1} → α → Ordnode α
O(1). Construct a singleton set containing value a.
singleton 3 = {3}
instance Ordnode.instSingleton E0 #
{α : Type u_1} → Singleton α (Ordnode α)
def Ordnode.size E1 #
{α : Type u_1} → Ordnode α → ℕ
O(1). Get the size of the set.
size {2, 1, 1, 4} = 3
theorem Ordnode.size_nil E0 #
∀ {α : Type u_1}, Ordnode.nil.size = 0
theorem Ordnode.size_node E0 #
∀ {α : Type u_1} (sz : ℕ) (l : Ordnode α) (x : α) (r : Ordnode α), (Ordnode.node sz l x r).size = sz
def Ordnode.empty E1 #
{α : Type u_1} → Ordnode α → Bool
O(1). Is the set empty?
empty ∅ = tt
empty {1, 2, 3} = ff
def Ordnode.dual E1 #
{α : Type u_1} → Ordnode α → Ordnode α
Internal use only, because it violates the BST property on the original order.
O(n). The dual of a tree is a tree with its left and right sides reversed throughout. The dual of a valid BST is valid under the dual order. This is convenient for exploiting symmetries in the algorithms.
def Ordnode.node' E1 #
{α : Type u_1} → Ordnode α → α → Ordnode α → Ordnode α
Internal use only
O(1). Construct a node with the correct size information, without rebalancing.
def BinaryTree.toOrdNode E1 #
{α : Type u_1} → BinaryTree α → Ordnode α
Convert to an OrdNode by pre-computing the sizes.
theorem Ordnode.size_toOrdNode E0 #
∀ {α : Type u_1} (b : BinaryTree α), b.toOrdNode.size = b.numNodes
def Ordnode.toBinaryTree E1 #
{α : Type u_1} → Ordnode α → BinaryTree α
Convert to an BinaryTree, discarding the cached size information.
theorem Ordnode.toBinaryTree_toOrdNode E0 #
∀ {α : Type u_1} (b : BinaryTree α), b.toOrdNode.toBinaryTree = b
def Ordnode.repr E1 #
{α : Type u_2} → [Repr α] → Ordnode α → ℕ → Std.Format
Basic pretty printing for Ordnode α that shows the structure of the tree.
repr {3, 1, 2, 4} = ((∅ 1 ∅) 2 ((∅ 3 ∅) 4 ∅))
instance Ordnode.instRepr E0 #
{α : Type u_2} → [Repr α] → Repr (Ordnode α)
def Ordnode.balanceL E1 #
{α : Type u_1} → Ordnode α → α → Ordnode α → Ordnode α
Internal use only
O(1). Rebalance a tree which was previously balanced but has had its left side grow by 1, or its right side shrink by 1.
def Ordnode.balanceR E1 #
{α : Type u_1} → Ordnode α → α → Ordnode α → Ordnode α
Internal use only
O(1). Rebalance a tree which was previously balanced but has had its right side grow by 1, or its left side shrink by 1.
def Ordnode.balance E1 #
{α : Type u_1} → Ordnode α → α → Ordnode α → Ordnode α
Internal use only
O(1). Rebalance a tree which was previously balanced but has had one side change by at most 1.
def Ordnode.All E1 #
{α : Type u_1} → (α → Prop) → Ordnode α → Prop
O(n). Does every element of the map satisfy property P?
All (fun x ↦ x < 5) {1, 2, 3} = True
All (fun x ↦ x < 5) {1, 2, 3, 5} = False
instance Ordnode.All.decidable E0 #
{α : Type u_1} → {P : α → Prop} → (t : Ordnode α) → [DecidablePred P] → Decidable (Ordnode.All P t)
def Ordnode.Any E1 #
{α : Type u_1} → (α → Prop) → Ordnode α → Prop
O(n). Does any element of the map satisfy property P?
Any (fun x ↦ x < 2) {1, 2, 3} = True
Any (fun x ↦ x < 2) {2, 3, 5} = False
instance Ordnode.Any.decidable E0 #
{α : Type u_1} → {P : α → Prop} → (t : Ordnode α) → [DecidablePred P] → Decidable (Ordnode.Any P t)
def Ordnode.Emem E1 #
{α : Type u_1} → α → Ordnode α → Prop
O(n). Exact membership in the set. This is useful primarily for stating
correctness properties; use ∈ for a version that actually uses the BST property
of the tree.
Emem 2 {1, 2, 3} = true
Emem 4 {1, 2, 3} = false
instance Ordnode.Emem.decidable E0 #
{α : Type u_1} → (x : α) → [DecidableEq α] → (t : Ordnode α) → Decidable (Ordnode.Emem x t)
def Ordnode.Amem E1 #
{α : Type u_1} → [LE α] → α → Ordnode α → Prop
O(n). Approximate membership in the set, that is, whether some element in the
set is equivalent to this one in the preorder. This is useful primarily for stating
correctness properties; use ∈ for a version that actually uses the BST property
of the tree.
Amem 2 {1, 2, 3} = true
Amem 4 {1, 2, 3} = false
To see the difference with Emem, we need a preorder that is not a partial order.
For example, suppose we compare pairs of numbers using only their first coordinate. Then:
-- TODO: Verify below example
Emem (0, 1) {(0, 0), (1, 2)} = false
Amem (0, 1) {(0, 0), (1, 2)} = true
(0, 1) ∈ {(0, 0), (1, 2)} = true
The ∈ relation is equivalent to Amem as long as the Ordnode is well formed,
and should always be used instead of Amem.
instance Ordnode.Amem.decidable E0 #
{α : Type u_1} → [inst : LE α] → [DecidableLE α] → (x : α) → (t : Ordnode α) → Decidable (Ordnode.Amem x t)
def Ordnode.findMin' E1 #
{α : Type u_1} → Ordnode α → α → α
O(log n). Return the minimum element of the tree, or the provided default value.
findMin' 37 {1, 2, 3} = 1
findMin' 37 ∅ = 37
def Ordnode.findMin E1 #
{α : Type u_1} → Ordnode α → Option α
O(log n). Return the minimum element of the tree, if it exists.
findMin {1, 2, 3} = some 1
findMin ∅ = none
def Ordnode.findMax' E1 #
{α : Type u_1} → α → Ordnode α → α
O(log n). Return the maximum element of the tree, or the provided default value.
findMax' 37 {1, 2, 3} = 3
findMax' 37 ∅ = 37
def Ordnode.findMax E1 #
{α : Type u_1} → Ordnode α → Option α
O(log n). Return the maximum element of the tree, if it exists.
findMax {1, 2, 3} = some 3
findMax ∅ = none
def Ordnode.eraseMin E1 #
{α : Type u_1} → Ordnode α → Ordnode α
O(log n). Remove the minimum element from the tree, or do nothing if it is already empty.
eraseMin {1, 2, 3} = {2, 3}
eraseMin ∅ = ∅
def Ordnode.eraseMax E1 #
{α : Type u_1} → Ordnode α → Ordnode α
O(log n). Remove the maximum element from the tree, or do nothing if it is already empty.
eraseMax {1, 2, 3} = {1, 2}
eraseMax ∅ = ∅
def Ordnode.splitMin' E1 #
{α : Type u_1} → Ordnode α → α → Ordnode α → α × Ordnode α
Internal use only, because it requires a balancing constraint on the inputs.
O(log n). Extract and remove the minimum element from a nonempty tree.
def Ordnode.splitMin E1 #
{α : Type u_1} → Ordnode α → Option (α × Ordnode α)
O(log n). Extract and remove the minimum element from the tree, if it exists.
split_min {1, 2, 3} = some (1, {2, 3})
split_min ∅ = none
def Ordnode.splitMax' E1 #
{α : Type u_1} → Ordnode α → α → Ordnode α → Ordnode α × α
Internal use only, because it requires a balancing constraint on the inputs.
O(log n). Extract and remove the maximum element from a nonempty tree.
def Ordnode.splitMax E1 #
{α : Type u_1} → Ordnode α → Option (Ordnode α × α)
O(log n). Extract and remove the maximum element from the tree, if it exists.
split_max {1, 2, 3} = some ({1, 2}, 3)
split_max ∅ = none
def Ordnode.glue E1 #
{α : Type u_1} → Ordnode α → Ordnode α → Ordnode α
Internal use only
O(log(m + n)). Concatenate two trees that are balanced and ordered with respect to each other.
def Ordnode.merge E1 #
{α : Type u_1} → Ordnode α → Ordnode α → Ordnode α
O(log(m + n)). Concatenate two trees that are ordered with respect to each other.
merge {1, 2} {3, 4} = {1, 2, 3, 4}
merge {3, 4} {1, 2} = precondition violation
def Ordnode.insertMax E1 #
{α : Type u_1} → Ordnode α → α → Ordnode α
O(log n). Insert an element above all the others, without any comparisons. (Assumes that the element is in fact above all the others).
insertMax {1, 2} 4 = {1, 2, 4}
insertMax {1, 2} 0 = precondition violation
def Ordnode.insertMin E1 #
{α : Type u_1} → α → Ordnode α → Ordnode α
O(log n). Insert an element below all the others, without any comparisons. (Assumes that the element is in fact below all the others).
insertMin {1, 2} 0 = {0, 1, 2}
insertMin {1, 2} 4 = precondition violation
def Ordnode.link E1 #
{α : Type u_1} → Ordnode α → α → Ordnode α → Ordnode α
O(log(m+n)). Build a tree from an element between two trees, without any assumption on the relative sizes.
link {1, 2} 4 {5, 6} = {1, 2, 4, 5, 6}
link {1, 3} 2 {5} = precondition violation
def Ordnode.filter E1 #
{α : Type u_1} → (p : α → Prop) → [DecidablePred p] → Ordnode α → Ordnode α
O(n). Filter the elements of a tree satisfying a predicate.
filter (fun x ↦ x < 3) {1, 2, 4} = {1, 2}
filter (fun x ↦ x > 5) {1, 2, 4} = ∅
def Ordnode.partition E1 #
{α : Type u_1} → (p : α → Prop) → [DecidablePred p] → Ordnode α → Ordnode α × Ordnode α
O(n). Split the elements of a tree into those satisfying, and not satisfying, a predicate.
partition (fun x ↦ x < 3) {1, 2, 4} = ({1, 2}, {3})
def Ordnode.map E1 #
{α : Type u_1} → {β : Type u_2} → (α → β) → Ordnode α → Ordnode β
O(n). Map a function across a tree, without changing the structure. Only valid when
the function is strictly monotone, i.e. x < y → f x < f y.
partition (fun x ↦ x + 2) {1, 2, 4} = {2, 3, 6}
partition (fun x : ℕ ↦ x - 2) {1, 2, 4} = precondition violation
def Ordnode.fold E1 #
{α : Type u_1} → {β : Sort u_2} → β → (β → α → β → β) → Ordnode α → β
O(n). Fold a function across the structure of a tree.
fold z f {1, 2, 4} = f (f z 1 z) 2 (f z 4 z)
The exact structure of function applications depends on the tree and so is unspecified.
def Ordnode.foldl E1 #
{α : Type u_1} → {β : Sort u_2} → (β → α → β) → β → Ordnode α → β
O(n). Fold a function from left to right (in increasing order) across the tree.
foldl f z {1, 2, 4} = f (f (f z 1) 2) 4
def Ordnode.foldr E1 #
{α : Type u_1} → {β : Sort u_2} → (α → β → β) → Ordnode α → β → β
O(n). Fold a function from right to left (in decreasing order) across the tree.
foldr f {1, 2, 4} z = f 1 (f 2 (f 4 z))
def Ordnode.toList E1 #
{α : Type u_1} → Ordnode α → List α
O(n). Build a list of elements in ascending order from the tree.
toList {1, 2, 4} = [1, 2, 4]
toList {2, 1, 1, 4} = [1, 2, 4]
def Ordnode.toRevList E1 #
{α : Type u_1} → Ordnode α → List α
O(n). Build a list of elements in descending order from the tree.
toRevList {1, 2, 4} = [4, 2, 1]
toRevList {2, 1, 1, 4} = [4, 2, 1]
instance Ordnode.instToString E0 #
{α : Type u_1} → [ToString α] → ToString (Ordnode α)
instance Ordnode.instToFormat E0 #
{α : Type u_1} → [Std.ToFormat α] → Std.ToFormat (Ordnode α)
def Ordnode.Equiv E1 #
{α : Type u_1} → Ordnode α → Ordnode α → Prop
O(n). True if the trees have the same elements, ignoring structural differences.
Equiv {1, 2, 4} {2, 1, 1, 4} = true
Equiv {1, 2, 4} {1, 2, 3} = false
instance Ordnode.instDecidableRelEquivOfDecidableEq E0 #
{α : Type u_1} → [DecidableEq α] → DecidableRel Ordnode.Equiv
def Ordnode.powerset E1 #
{α : Type u_1} → Ordnode α → Ordnode (Ordnode α)
O(2^n). Constructs the powerset of a given set, that is, the set of all subsets.
powerset {1, 2, 3} = {∅, {1}, {2}, {3}, {1,2}, {1,3}, {2,3}, {1,2,3}}
def Ordnode.prod E1 #
{α : Type u_1} → {β : Type u_2} → Ordnode α → Ordnode β → Ordnode (α × β)
O(m * n). The Cartesian product of two sets: (a, b) ∈ s.prod t iff a ∈ s and b ∈ t.
prod {1, 2} {2, 3} = {(1, 2), (1, 3), (2, 2), (2, 3)}
def Ordnode.copair E1 #
{α : Type u_1} → {β : Type u_2} → Ordnode α → Ordnode β → Ordnode (α ⊕ β)
O(m + n). Build a set on the disjoint union by combining sets on the factors.
Or.inl a ∈ s.copair t iff a ∈ s, and Or.inr b ∈ s.copair t iff b ∈ t.
copair {1, 2} {2, 3} = {inl 1, inl 2, inr 2, inr 3}
def Ordnode.pmap E1 #
{α : Type u_1} → {P : α → Prop} → {β : Type u_2} → ((a : α) → P a → β) → (t : Ordnode α) → Ordnode.All P t → Ordnode β
O(n). Map a partial function across a set. The result depends on a proof that the function is defined on all members of the set.
pmap (fin.mk : ∀ n, n < 4 → fin 4) {1, 2} H = {(1 : fin 4), (2 : fin 4)}
def Ordnode.attach' E1 #
{α : Type u_1} → {P : α → Prop} → (t : Ordnode α) → Ordnode.All P t → Ordnode { a // P a }
O(n). "Attach" the information that every element of t satisfies property
P to these elements inside the set, producing a set in the subtype.
attach' (fun x ↦ x < 4) {1, 2} H = ({1, 2} : Ordnode {x // x<4})
def Ordnode.nth E1 #
{α : Type u_1} → Ordnode α → ℕ → Option α
O(log n). Get the ith element of the set, by its index from left to right.
nth {a, b, c, d} 2 = some c
nth {a, b, c, d} 5 = none
def Ordnode.removeNth E1 #
{α : Type u_1} → Ordnode α → ℕ → Ordnode α
O(log n). Remove the ith element of the set, by its index from left to right.
remove_nth {a, b, c, d} 2 = {a, b, d}
remove_nth {a, b, c, d} 5 = {a, b, c, d}
def Ordnode.takeAux E0 #
{α : Type u_1} → Ordnode α → ℕ → Ordnode α
Auxiliary definition for take. (Can also be used in lieu of take if you know the
index is within the range of the data structure.)
takeAux {a, b, c, d} 2 = {a, b}
takeAux {a, b, c, d} 5 = {a, b, c, d}
def Ordnode.take E1 #
{α : Type u_1} → ℕ → Ordnode α → Ordnode α
O(log n). Get the first i elements of the set, counted from the left.
take 2 {a, b, c, d} = {a, b}
take 5 {a, b, c, d} = {a, b, c, d}
def Ordnode.dropAux E0 #
{α : Type u_1} → Ordnode α → ℕ → Ordnode α
Auxiliary definition for drop. (Can also be used in lieu of drop if you know the
index is within the range of the data structure.)
drop_aux {a, b, c, d} 2 = {c, d}
drop_aux {a, b, c, d} 5 = ∅
def Ordnode.drop E1 #
{α : Type u_1} → ℕ → Ordnode α → Ordnode α
O(log n). Remove the first i elements of the set, counted from the left.
drop 2 {a, b, c, d} = {c, d}
drop 5 {a, b, c, d} = ∅
def Ordnode.splitAtAux E0 #
{α : Type u_1} → Ordnode α → ℕ → Ordnode α × Ordnode α
Auxiliary definition for splitAt. (Can also be used in lieu of splitAt if you know the
index is within the range of the data structure.)
splitAtAux {a, b, c, d} 2 = ({a, b}, {c, d})
splitAtAux {a, b, c, d} 5 = ({a, b, c, d}, ∅)
def Ordnode.splitAt E1 #
{α : Type u_1} → ℕ → Ordnode α → Ordnode α × Ordnode α
O(log n). Split a set at the ith element, getting the first i and everything else.
splitAt 2 {a, b, c, d} = ({a, b}, {c, d})
splitAt 5 {a, b, c, d} = ({a, b, c, d}, ∅)
def Ordnode.takeWhile E1 #
{α : Type u_1} → (p : α → Prop) → [DecidablePred p] → Ordnode α → Ordnode α
O(log n). Get an initial segment of the set that satisfies the predicate p.
p is required to be antitone, that is, x < y → p y → p x.
takeWhile (fun x ↦ x < 4) {1, 2, 3, 4, 5} = {1, 2, 3}
takeWhile (fun x ↦ x > 4) {1, 2, 3, 4, 5} = precondition violation
def Ordnode.dropWhile E1 #
{α : Type u_1} → (p : α → Prop) → [DecidablePred p] → Ordnode α → Ordnode α
O(log n). Remove an initial segment of the set that satisfies the predicate p.
p is required to be antitone, that is, x < y → p y → p x.
dropWhile (fun x ↦ x < 4) {1, 2, 3, 4, 5} = {4, 5}
dropWhile (fun x ↦ x > 4) {1, 2, 3, 4, 5} = precondition violation
def Ordnode.span E1 #
{α : Type u_1} → (p : α → Prop) → [DecidablePred p] → Ordnode α → Ordnode α × Ordnode α
O(log n). Split the set into those satisfying and not satisfying the predicate p.
p is required to be antitone, that is, x < y → p y → p x.
span (fun x ↦ x < 4) {1, 2, 3, 4, 5} = ({1, 2, 3}, {4, 5})
span (fun x ↦ x > 4) {1, 2, 3, 4, 5} = precondition violation
def Ordnode.ofAscListAux₁ E1 #
{α : Type u_1} → (l : List α) → ℕ → Ordnode α × { l' // l'.length ≤ l.length }
Auxiliary definition for ofAscList.
Note: This function is defined by well-founded recursion, so it will probably not compute
in the kernel, meaning that you probably can't prove things like
ofAscList [1, 2, 3] = {1, 2, 3} by rfl.
This implementation is optimized for VM evaluation.
def Ordnode.ofAscListAux₂ E1 #
{α : Type u_1} → List α → Ordnode α → ℕ → Ordnode α
Auxiliary definition for ofAscList.
def Ordnode.ofAscList E1 #
{α : Type u_1} → List α → Ordnode α
O(n). Build a set from a list which is already sorted. Performs no comparisons.
ofAscList [1, 2, 3] = {1, 2, 3}
ofAscList [3, 2, 1] = precondition violation
def Ordnode.mem E1 #
{α : Type u_1} → [inst : LE α] → [DecidableLE α] → α → Ordnode α → Bool
O(log n). Does the set (approximately) contain the element x? That is,
is there an element that is equivalent to x in the order?
1 ∈ {1, 2, 3} = true
4 ∈ {1, 2, 3} = false
Using a preorder on ℕ × ℕ that only compares the first coordinate:
(1, 1) ∈ {(0, 1), (1, 2)} = true
(3, 1) ∈ {(0, 1), (1, 2)} = false
def Ordnode.find E1 #
{α : Type u_1} → [inst : LE α] → [DecidableLE α] → α → Ordnode α → Option α
O(log n). Retrieve an element in the set that is equivalent to x in the order,
if it exists.
find 1 {1, 2, 3} = some 1
find 4 {1, 2, 3} = none
Using a preorder on ℕ × ℕ that only compares the first coordinate:
find (1, 1) {(0, 1), (1, 2)} = some (1, 2)
find (3, 1) {(0, 1), (1, 2)} = none
instance Ordnode.instMembership E0 #
{α : Type u_1} → [inst : LE α] → [DecidableLE α] → Membership α (Ordnode α)
instance Ordnode.mem.decidable E0 #
{α : Type u_1} → [inst : LE α] → [inst_1 : DecidableLE α] → (x : α) → (t : Ordnode α) → Decidable (x ∈ t)
def Ordnode.insertWith E1 #
{α : Type u_1} → [inst : LE α] → [DecidableLE α] → (α → α) → α → Ordnode α → Ordnode α
O(log n). Insert an element into the set, preserving balance and the BST property.
If an equivalent element is already in the set, the function f is used to generate
the element to insert (being passed the current value in the set).
insertWith f 0 {1, 2, 3} = {0, 1, 2, 3}
insertWith f 1 {1, 2, 3} = {f 1, 2, 3}
Using a preorder on ℕ × ℕ that only compares the first coordinate:
insertWith f (1, 1) {(0, 1), (1, 2)} = {(0, 1), f (1, 2)}
insertWith f (3, 1) {(0, 1), (1, 2)} = {(0, 1), (1, 2), (3, 1)}
def Ordnode.adjustWith E1 #
{α : Type u_1} → [inst : LE α] → [DecidableLE α] → (α → α) → α → Ordnode α → Ordnode α
O(log n). Modify an element in the set with the given function,
doing nothing if the key is not found.
Note that the element returned by f must be equivalent to x.
adjustWith f 0 {1, 2, 3} = {1, 2, 3}
adjustWith f 1 {1, 2, 3} = {f 1, 2, 3}
Using a preorder on ℕ × ℕ that only compares the first coordinate:
adjustWith f (1, 1) {(0, 1), (1, 2)} = {(0, 1), f (1, 2)}
adjustWith f (3, 1) {(0, 1), (1, 2)} = {(0, 1), (1, 2)}
def Ordnode.updateWith E1 #
{α : Type u_1} → [inst : LE α] → [DecidableLE α] → (α → Option α) → α → Ordnode α → Ordnode α
O(log n). Modify an element in the set with the given function,
doing nothing if the key is not found.
Note that the element returned by f must be equivalent to x.
updateWith f 0 {1, 2, 3} = {1, 2, 3}
updateWith f 1 {1, 2, 3} = {2, 3} if f 1 = none
= {a, 2, 3} if f 1 = some a
def Ordnode.alter E1 #
{α : Type u_1} → [inst : LE α] → [DecidableLE α] → (Option α → Option α) → α → Ordnode α → Ordnode α
O(log n). Modify an element in the set with the given function,
doing nothing if the key is not found.
Note that the element returned by f must be equivalent to x.
alter f 0 {1, 2, 3} = {1, 2, 3} if f none = none
= {a, 1, 2, 3} if f none = some a
alter f 1 {1, 2, 3} = {2, 3} if f 1 = none
= {a, 2, 3} if f 1 = some a
def Ordnode.insert E1 #
{α : Type u_1} → [inst : LE α] → [DecidableLE α] → α → Ordnode α → Ordnode α
O(log n). Insert an element into the set, preserving balance and the BST property. If an equivalent element is already in the set, this replaces it.
insert 1 {1, 2, 3} = {1, 2, 3}
insert 4 {1, 2, 3} = {1, 2, 3, 4}
Using a preorder on ℕ × ℕ that only compares the first coordinate:
insert (1, 1) {(0, 1), (1, 2)} = {(0, 1), (1, 1)}
insert (3, 1) {(0, 1), (1, 2)} = {(0, 1), (1, 2), (3, 1)}
instance Ordnode.instInsert E0 #
{α : Type u_1} → [inst : LE α] → [DecidableLE α] → Insert α (Ordnode α)
def Ordnode.insert' E1 #
{α : Type u_1} → [inst : LE α] → [DecidableLE α] → α → Ordnode α → Ordnode α
O(log n). Insert an element into the set, preserving balance and the BST property. If an equivalent element is already in the set, the set is returned as is.
insert' 1 {1, 2, 3} = {1, 2, 3}
insert' 4 {1, 2, 3} = {1, 2, 3, 4}
Using a preorder on ℕ × ℕ that only compares the first coordinate:
insert' (1, 1) {(0, 1), (1, 2)} = {(0, 1), (1, 2)}
insert' (3, 1) {(0, 1), (1, 2)} = {(0, 1), (1, 2), (3, 1)}
def Ordnode.split E1 #
{α : Type u_1} → [inst : LE α] → [DecidableLE α] → α → Ordnode α → Ordnode α × Ordnode α
O(log n). Split the tree into those smaller than x and those greater than it.
If an element equivalent to x is in the set, it is discarded.
split 2 {1, 2, 4} = ({1}, {4})
split 3 {1, 2, 4} = ({1, 2}, {4})
split 4 {1, 2, 4} = ({1, 2}, ∅)
Using a preorder on ℕ × ℕ that only compares the first coordinate:
split (1, 1) {(0, 1), (1, 2)} = ({(0, 1)}, ∅)
split (3, 1) {(0, 1), (1, 2)} = ({(0, 1), (1, 2)}, ∅)
def Ordnode.split3 E1 #
{α : Type u_1} → [inst : LE α] → [DecidableLE α] → α → Ordnode α → Ordnode α × Option α × Ordnode α
O(log n). Split the tree into those smaller than x and those greater than it,
plus an element equivalent to x, if it exists.
split3 2 {1, 2, 4} = ({1}, some 2, {4})
split3 3 {1, 2, 4} = ({1, 2}, none, {4})
split3 4 {1, 2, 4} = ({1, 2}, some 4, ∅)
Using a preorder on ℕ × ℕ that only compares the first coordinate:
split3 (1, 1) {(0, 1), (1, 2)} = ({(0, 1)}, some (1, 2), ∅)
split3 (3, 1) {(0, 1), (1, 2)} = ({(0, 1), (1, 2)}, none, ∅)
def Ordnode.erase E1 #
{α : Type u_1} → [inst : LE α] → [DecidableLE α] → α → Ordnode α → Ordnode α
O(log n). Remove an element from the set equivalent to x. Does nothing if there
is no such element.
erase 1 {1, 2, 3} = {2, 3}
erase 4 {1, 2, 3} = {1, 2, 3}
Using a preorder on ℕ × ℕ that only compares the first coordinate:
erase (1, 1) {(0, 1), (1, 2)} = {(0, 1)}
erase (3, 1) {(0, 1), (1, 2)} = {(0, 1), (1, 2)}
def Ordnode.findLtAux E0 #
{α : Type u_1} → [inst : LE α] → [DecidableLE α] → α → Ordnode α → α → α
Auxiliary definition for findLt.
def Ordnode.findLt E1 #
{α : Type u_1} → [inst : LE α] → [DecidableLE α] → α → Ordnode α → Option α
O(log n). Get the largest element in the tree that is < x.
findLt 2 {1, 2, 4} = some 1
findLt 3 {1, 2, 4} = some 2
findLt 0 {1, 2, 4} = none
def Ordnode.findGtAux E0 #
{α : Type u_1} → [inst : LE α] → [DecidableLE α] → α → Ordnode α → α → α
Auxiliary definition for findGt.
def Ordnode.findGt E1 #
{α : Type u_1} → [inst : LE α] → [DecidableLE α] → α → Ordnode α → Option α
O(log n). Get the smallest element in the tree that is > x.
findGt 2 {1, 2, 4} = some 4
findGt 3 {1, 2, 4} = some 4
findGt 4 {1, 2, 4} = none
def Ordnode.findLeAux E0 #
{α : Type u_1} → [inst : LE α] → [DecidableLE α] → α → Ordnode α → α → α
Auxiliary definition for findLe.
def Ordnode.findLe E1 #
{α : Type u_1} → [inst : LE α] → [DecidableLE α] → α → Ordnode α → Option α
O(log n). Get the largest element in the tree that is ≤ x.
findLe 2 {1, 2, 4} = some 2
findLe 3 {1, 2, 4} = some 2
findLe 0 {1, 2, 4} = none
def Ordnode.findGeAux E0 #
{α : Type u_1} → [inst : LE α] → [DecidableLE α] → α → Ordnode α → α → α
Auxiliary definition for findGe.
def Ordnode.findGe E1 #
{α : Type u_1} → [inst : LE α] → [DecidableLE α] → α → Ordnode α → Option α
O(log n). Get the smallest element in the tree that is ≥ x.
findGe 2 {1, 2, 4} = some 2
findGe 3 {1, 2, 4} = some 4
findGe 5 {1, 2, 4} = none
def Ordnode.findIndexAux E0 #
{α : Type u_1} → [inst : LE α] → [DecidableLE α] → α → Ordnode α → ℕ → Option ℕ
Auxiliary definition for findIndex.
def Ordnode.findIndex E1 #
{α : Type u_1} → [inst : LE α] → [DecidableLE α] → α → Ordnode α → Option ℕ
O(log n). Get the index, counting from the left,
of an element equivalent to x if it exists.
findIndex 2 {1, 2, 4} = some 1
findIndex 4 {1, 2, 4} = some 2
findIndex 5 {1, 2, 4} = none
def Ordnode.isSubsetAux E0 #
{α : Type u_1} → [inst : LE α] → [DecidableLE α] → Ordnode α → Ordnode α → Bool
Auxiliary definition for isSubset.
def Ordnode.isSubset E1 #
{α : Type u_1} → [inst : LE α] → [DecidableLE α] → Ordnode α → Ordnode α → Bool
O(m + n). Is every element of t₁ equivalent to some element of t₂?
is_subset {1, 4} {1, 2, 4} = tt
is_subset {1, 3} {1, 2, 4} = ff
def Ordnode.disjoint E1 #
{α : Type u_1} → [inst : LE α] → [DecidableLE α] → Ordnode α → Ordnode α → Bool
O(m + n). Is every element of t₁ not equivalent to any element of t₂?
disjoint {1, 3} {2, 4} = tt
disjoint {1, 2} {2, 4} = ff
def Ordnode.union E1 #
{α : Type u_1} → [inst : LE α] → [DecidableLE α] → Ordnode α → Ordnode α → Ordnode α
O(m * log(|m ∪ n| + 1)), m ≤ n. The union of two sets, preferring members of
t₁ over those of t₂ when equivalent elements are encountered.
union {1, 2} {2, 3} = {1, 2, 3}
union {1, 3} {2} = {1, 2, 3}
Using a preorder on ℕ × ℕ that only compares the first coordinate:
union {(1, 1)} {(0, 1), (1, 2)} = {(0, 1), (1, 1)}
def Ordnode.diff E1 #
{α : Type u_1} → [inst : LE α] → [DecidableLE α] → Ordnode α → Ordnode α → Ordnode α
O(m * log(|m ∪ n| + 1)), m ≤ n. Difference of two sets.
diff {1, 2} {2, 3} = {1}
diff {1, 2, 3} {2} = {1, 3}
def Ordnode.inter E1 #
{α : Type u_1} → [inst : LE α] → [DecidableLE α] → Ordnode α → Ordnode α → Ordnode α
O(m * log(|m ∪ n| + 1)), m ≤ n. Intersection of two sets, preferring members of
t₁ over those of t₂ when equivalent elements are encountered.
inter {1, 2} {2, 3} = {2}
inter {1, 3} {2} = ∅
def Ordnode.ofList E1 #
{α : Type u_1} → [inst : LE α] → [DecidableLE α] → List α → Ordnode α
O(n * log n). Build a set from a list, preferring elements that appear earlier in the list in the case of equivalent elements.
ofList [1, 2, 3] = {1, 2, 3}
ofList [2, 1, 1, 3] = {1, 2, 3}
Using a preorder on ℕ × ℕ that only compares the first coordinate:
ofList [(1, 1), (0, 1), (1, 2)] = {(0, 1), (1, 1)}
def Ordnode.ofList' E1 #
{α : Type u_1} → [inst : LE α] → [DecidableLE α] → List α → Ordnode α
O(n * log n). Adaptively chooses between the linear and log-linear algorithm depending on whether the input list is already sorted.
ofList' [1, 2, 3] = {1, 2, 3}
ofList' [2, 1, 1, 3] = {1, 2, 3}
def Ordnode.image E1 #
{α : Type u_2} → {β : Type u_3} → [inst : LE β] → [DecidableLE β] → (α → β) → Ordnode α → Ordnode β
O(n * log n). Map a function on a set. Unlike map this has no requirements on
f, and the resulting set may be smaller than the input if f is noninjective.
Equivalent elements are selected with a preference for smaller source elements.
image (fun x ↦ x + 2) {1, 2, 4} = {3, 4, 6}
image (fun x : ℕ ↦ x - 2) {1, 2, 4} = {0, 2}