mathlib documentation

data.ordmap.ordset

Verification of the ordnode α datatype #

This file proves the correctness of the operations in data.ordmap.ordnode. The public facing version is the type ordset α, which is a wrapper around ordnode α which includes the correctness invariant of the type, and it exposes parallel operations like insert as functions on ordset that do the same thing but bundle the correctness proofs. The advantage is that it is possible to, for example, prove that the result of find on insert will actually find the element, while ordnode cannot guarantee this if the input tree did not satisfy the type invariants.

Main definitions #

Implementation notes #

The majority of this file is actually in the ordnode namespace, because we first have to prove the correctness of all the operations (and defining what correctness means here is actually somewhat subtle). So all the actual ordset operations are at the very end, once we have all the theorems.

An ordnode α is an inductive type which describes a tree which stores the size at internal nodes. The correctness invariant of an ordnode α is:

Because the ordnode file was ported from Haskell, the correctness invariants of some of the functions have not been spelled out, and some theorems like ordnode.valid'.balance_l_aux show very intricate assumptions on the sizes, which may need to be revised if it turns out some operations violate these assumptions, because there is a decent amount of slop in the actual data structure invariants, so the theorem will go through with multiple choices of assumption.

Note: This file is incomplete, in the sense that the intent is to have verified versions and lemmas about all the definitions in ordnode.lean, but at the moment only a few operations are verified (the hard part should be out of the way, but still). Contributors are encouraged to pick this up and finish the job, if it appeals to you.

Tags #

ordered map, ordered set, data structure, verified programming

delta and ratio #

theorem ordnode.not_le_delta {s : } (H : 1 s) :
theorem ordnode.delta_lt_false {a b : } (h₁ : ordnode.delta * a < b) (h₂ : ordnode.delta * b < a) :

singleton #

size and empty #

def ordnode.real_size {α : Type u_1} :
ordnode α

O(n). Computes the actual number of elements in the set, ignoring the cached size field.

Equations

sized #

def ordnode.sized {α : Type u_1} :
ordnode α → Prop

The sized property asserts that all the size fields in nodes match the actual size of the respective subtrees.

Equations
theorem ordnode.sized.node' {α : Type u_1} {l : ordnode α} {x : α} {r : ordnode α} (hl : l.sized) (hr : r.sized) :
(l.node' x r).sized
theorem ordnode.sized.eq_node' {α : Type u_1} {s : } {l : ordnode α} {x : α} {r : ordnode α} (h : (ordnode.node s l x r).sized) :
ordnode.node s l x r = l.node' x r
theorem ordnode.sized.size_eq {α : Type u_1} {s : } {l : ordnode α} {x : α} {r : ordnode α} (H : (ordnode.node s l x r).sized) :
(ordnode.node s l x r).size = l.size + r.size + 1
theorem ordnode.sized.induction {α : Type u_1} {t : ordnode α} (hl : t.sized) {C : ordnode α → Prop} (H0 : C ordnode.nil) (H1 : ∀ (l : ordnode α) (x : α) (r : ordnode α), C lC rC (l.node' x r)) :
C t
theorem ordnode.size_eq_real_size {α : Type u_1} {t : ordnode α} :
t.sizedt.size = t.real_size
@[simp]
theorem ordnode.sized.size_eq_zero {α : Type u_1} {t : ordnode α} (ht : t.sized) :
theorem ordnode.sized.pos {α : Type u_1} {s : } {l : ordnode α} {x : α} {r : ordnode α} (h : (ordnode.node s l x r).sized) :
0 < s

dual

theorem ordnode.dual_dual {α : Type u_1} (t : ordnode α) :
t.dual.dual = t
@[simp]
theorem ordnode.size_dual {α : Type u_1} (t : ordnode α) :

balanced

def ordnode.balanced_sz (l r : ) :
Prop

The balanced_sz l r asserts that a hypothetical tree with children of sizes l and r is balanced: either l ≤ δ * r and r ≤ δ * r, or the tree is trivial with a singleton on one side and nothing on the other.

Equations
def ordnode.balanced {α : Type u_1} :
ordnode α → Prop

The balanced t asserts that the tree t satisfies the balance invariants (at every level).

Equations
@[instance]
Equations
theorem ordnode.balanced_sz_up {l r₁ r₂ : } (h₁ : r₁ r₂) (h₂ : l + r₂ 1 r₂ ordnode.delta * l) (H : ordnode.balanced_sz l r₁) :
theorem ordnode.balanced_sz_down {l r₁ r₂ : } (h₁ : r₁ r₂) (h₂ : l + r₂ 1 l ordnode.delta * r₁) (H : ordnode.balanced_sz l r₂) :
theorem ordnode.balanced.dual {α : Type u_1} {t : ordnode α} :

rotate and balance #

def ordnode.node3_l {α : Type u_1} (l : ordnode α) (x : α) (m : ordnode α) (y : α) (r : ordnode α) :

Build a tree from three nodes, left associated (ignores the invariants).

Equations
def ordnode.node3_r {α : Type u_1} (l : ordnode α) (x : α) (m : ordnode α) (y : α) (r : ordnode α) :

Build a tree from three nodes, right associated (ignores the invariants).

Equations
def ordnode.node4_l {α : Type u_1} :
ordnode αα → ordnode αα → ordnode αordnode α

Build a tree from three nodes, with a () b -> (a ()) b and a (b c) d -> ((a b) (c d)).

Equations
def ordnode.node4_r {α : Type u_1} :
ordnode αα → ordnode αα → ordnode αordnode α

Build a tree from three nodes, with a () b -> a (() b) and a (b c) d -> ((a b) (c d)).

Equations
def ordnode.rotate_l {α : Type u_1} :
ordnode αα → ordnode αordnode α

Concatenate two nodes, performing a left rotation x (y z) -> ((x y) z) if balance is upset.

Equations
def ordnode.rotate_r {α : Type u_1} :
ordnode αα → ordnode αordnode α

Concatenate two nodes, performing a right rotation (x y) z -> (x (y z)) if balance is upset.

Equations
def ordnode.balance_l' {α : Type u_1} (l : ordnode α) (x : α) (r : ordnode α) :

A left balance operation. This will rebalance a concatenation, assuming the original nodes are not too far from balanced.

Equations
def ordnode.balance_r' {α : Type u_1} (l : ordnode α) (x : α) (r : ordnode α) :

A right balance operation. This will rebalance a concatenation, assuming the original nodes are not too far from balanced.

Equations
def ordnode.balance' {α : Type u_1} (l : ordnode α) (x : α) (r : ordnode α) :

The full balance operation. This is the same as balance, but with less manual inlining. It is somewhat easier to work with this version in proofs.

Equations
theorem ordnode.dual_node' {α : Type u_1} (l : ordnode α) (x : α) (r : ordnode α) :
(l.node' x r).dual = r.dual.node' x l.dual
theorem ordnode.dual_node3_l {α : Type u_1} (l : ordnode α) (x : α) (m : ordnode α) (y : α) (r : ordnode α) :
(l.node3_l x m y r).dual = r.dual.node3_r y m.dual x l.dual
theorem ordnode.dual_node3_r {α : Type u_1} (l : ordnode α) (x : α) (m : ordnode α) (y : α) (r : ordnode α) :
(l.node3_r x m y r).dual = r.dual.node3_l y m.dual x l.dual
theorem ordnode.dual_node4_l {α : Type u_1} (l : ordnode α) (x : α) (m : ordnode α) (y : α) (r : ordnode α) :
(l.node4_l x m y r).dual = r.dual.node4_r y m.dual x l.dual
theorem ordnode.dual_node4_r {α : Type u_1} (l : ordnode α) (x : α) (m : ordnode α) (y : α) (r : ordnode α) :
(l.node4_r x m y r).dual = r.dual.node4_l y m.dual x l.dual
theorem ordnode.dual_rotate_l {α : Type u_1} (l : ordnode α) (x : α) (r : ordnode α) :
theorem ordnode.dual_rotate_r {α : Type u_1} (l : ordnode α) (x : α) (r : ordnode α) :
theorem ordnode.dual_balance' {α : Type u_1} (l : ordnode α) (x : α) (r : ordnode α) :
theorem ordnode.dual_balance_l {α : Type u_1} (l : ordnode α) (x : α) (r : ordnode α) :
theorem ordnode.dual_balance_r {α : Type u_1} (l : ordnode α) (x : α) (r : ordnode α) :
theorem ordnode.sized.node3_l {α : Type u_1} {l : ordnode α} {x : α} {m : ordnode α} {y : α} {r : ordnode α} (hl : l.sized) (hm : m.sized) (hr : r.sized) :
(l.node3_l x m y r).sized
theorem ordnode.sized.node3_r {α : Type u_1} {l : ordnode α} {x : α} {m : ordnode α} {y : α} {r : ordnode α} (hl : l.sized) (hm : m.sized) (hr : r.sized) :
(l.node3_r x m y r).sized
theorem ordnode.sized.node4_l {α : Type u_1} {l : ordnode α} {x : α} {m : ordnode α} {y : α} {r : ordnode α} (hl : l.sized) (hm : m.sized) (hr : r.sized) :
(l.node4_l x m y r).sized
theorem ordnode.node3_l_size {α : Type u_1} {l : ordnode α} {x : α} {m : ordnode α} {y : α} {r : ordnode α} :
(l.node3_l x m y r).size = l.size + m.size + r.size + 2
theorem ordnode.node3_r_size {α : Type u_1} {l : ordnode α} {x : α} {m : ordnode α} {y : α} {r : ordnode α} :
(l.node3_r x m y r).size = l.size + m.size + r.size + 2
theorem ordnode.node4_l_size {α : Type u_1} {l : ordnode α} {x : α} {m : ordnode α} {y : α} {r : ordnode α} (hm : m.sized) :
(l.node4_l x m y r).size = l.size + m.size + r.size + 2
theorem ordnode.sized.dual {α : Type u_1} {t : ordnode α} (h : t.sized) :
theorem ordnode.sized.dual_iff {α : Type u_1} {t : ordnode α} :
theorem ordnode.sized.rotate_l {α : Type u_1} {l : ordnode α} {x : α} {r : ordnode α} (hl : l.sized) (hr : r.sized) :
(l.rotate_l x r).sized
theorem ordnode.sized.rotate_r {α : Type u_1} {l : ordnode α} {x : α} {r : ordnode α} (hl : l.sized) (hr : r.sized) :
(l.rotate_r x r).sized
theorem ordnode.sized.rotate_l_size {α : Type u_1} {l : ordnode α} {x : α} {r : ordnode α} (hm : r.sized) :
(l.rotate_l x r).size = l.size + r.size + 1
theorem ordnode.sized.rotate_r_size {α : Type u_1} {l : ordnode α} {x : α} {r : ordnode α} (hl : l.sized) :
(l.rotate_r x r).size = l.size + r.size + 1
theorem ordnode.sized.balance' {α : Type u_1} {l : ordnode α} {x : α} {r : ordnode α} (hl : l.sized) (hr : r.sized) :
(l.balance' x r).sized
theorem ordnode.size_balance' {α : Type u_1} {l : ordnode α} {x : α} {r : ordnode α} (hl : l.sized) (hr : r.sized) :
(l.balance' x r).size = l.size + r.size + 1

all, any, emem, amem #

theorem ordnode.all.imp {α : Type u_1} {P Q : α → Prop} (H : ∀ (a : α), P aQ a) {t : ordnode α} :
theorem ordnode.any.imp {α : Type u_1} {P Q : α → Prop} (H : ∀ (a : α), P aQ a) {t : ordnode α} :
theorem ordnode.all_singleton {α : Type u_1} {P : α → Prop} {x : α} :
ordnode.all P {x} P x
theorem ordnode.any_singleton {α : Type u_1} {P : α → Prop} {x : α} :
ordnode.any P {x} P x
theorem ordnode.all_dual {α : Type u_1} {P : α → Prop} {t : ordnode α} :
theorem ordnode.all_iff_forall {α : Type u_1} {P : α → Prop} {t : ordnode α} :
ordnode.all P t ∀ (x : α), ordnode.emem x tP x
theorem ordnode.any_iff_exists {α : Type u_1} {P : α → Prop} {t : ordnode α} :
ordnode.any P t ∃ (x : α), ordnode.emem x t P x
theorem ordnode.emem_iff_all {α : Type u_1} {x : α} {t : ordnode α} :
ordnode.emem x t ∀ (P : α → Prop), ordnode.all P tP x
theorem ordnode.all_node' {α : Type u_1} {P : α → Prop} {l : ordnode α} {x : α} {r : ordnode α} :
theorem ordnode.all_node3_l {α : Type u_1} {P : α → Prop} {l : ordnode α} {x : α} {m : ordnode α} {y : α} {r : ordnode α} :
theorem ordnode.all_node3_r {α : Type u_1} {P : α → Prop} {l : ordnode α} {x : α} {m : ordnode α} {y : α} {r : ordnode α} :
theorem ordnode.all_node4_l {α : Type u_1} {P : α → Prop} {l : ordnode α} {x : α} {m : ordnode α} {y : α} {r : ordnode α} :
theorem ordnode.all_node4_r {α : Type u_1} {P : α → Prop} {l : ordnode α} {x : α} {m : ordnode α} {y : α} {r : ordnode α} :
theorem ordnode.all_rotate_l {α : Type u_1} {P : α → Prop} {l : ordnode α} {x : α} {r : ordnode α} :
theorem ordnode.all_rotate_r {α : Type u_1} {P : α → Prop} {l : ordnode α} {x : α} {r : ordnode α} :
theorem ordnode.all_balance' {α : Type u_1} {P : α → Prop} {l : ordnode α} {x : α} {r : ordnode α} :

to_list #

theorem ordnode.foldr_cons_eq_to_list {α : Type u_1} (t : ordnode α) (r : list α) :
@[simp]
theorem ordnode.to_list_nil {α : Type u_1} :
@[simp]
theorem ordnode.to_list_node {α : Type u_1} (s : ) (l : ordnode α) (x : α) (r : ordnode α) :
theorem ordnode.emem_iff_mem_to_list {α : Type u_1} {x : α} {t : ordnode α} :
theorem ordnode.length_to_list' {α : Type u_1} (t : ordnode α) :
theorem ordnode.length_to_list {α : Type u_1} {t : ordnode α} (h : t.sized) :
theorem ordnode.equiv_iff {α : Type u_1} {t₁ t₂ : ordnode α} (h₁ : t₁.sized) (h₂ : t₂.sized) :
t₁.equiv t₂ t₁.to_list = t₂.to_list

(find/erase/split)_(min/max) #

theorem ordnode.find_min'_dual {α : Type u_1} (t : ordnode α) (x : α) :
theorem ordnode.find_max'_dual {α : Type u_1} (t : ordnode α) (x : α) :
theorem ordnode.find_min_dual {α : Type u_1} (t : ordnode α) :
theorem ordnode.find_max_dual {α : Type u_1} (t : ordnode α) :
theorem ordnode.dual_erase_min {α : Type u_1} (t : ordnode α) :
theorem ordnode.dual_erase_max {α : Type u_1} (t : ordnode α) :
theorem ordnode.split_min_eq {α : Type u_1} (s : ) (l : ordnode α) (x : α) (r : ordnode α) :
theorem ordnode.split_max_eq {α : Type u_1} (s : ) (l : ordnode α) (x : α) (r : ordnode α) :
theorem ordnode.find_min'_all {α : Type u_1} {P : α → Prop} (t : ordnode α) (x : α) :
ordnode.all P tP xP (t.find_min' x)
theorem ordnode.find_max'_all {α : Type u_1} {P : α → Prop} (x : α) (t : ordnode α) :
P xordnode.all P tP (ordnode.find_max' x t)

glue #

merge #

@[simp]
theorem ordnode.merge_nil_left {α : Type u_1} (t : ordnode α) :
@[simp]
theorem ordnode.merge_nil_right {α : Type u_1} (t : ordnode α) :
@[simp]
theorem ordnode.merge_node {α : Type u_1} {ls : } {ll : ordnode α} {lx : α} {lr : ordnode α} {rs : } {rl : ordnode α} {rx : α} {rr : ordnode α} :
(ordnode.node ls ll lx lr).merge (ordnode.node rs rl rx rr) = ite (ordnode.delta * ls < rs) (((ordnode.node ls ll lx lr).merge rl).balance_l rx rr) (ite (ordnode.delta * rs < ls) (ll.balance_r lx (lr.merge (ordnode.node rs rl rx rr))) ((ordnode.node ls ll lx lr).glue (ordnode.node rs rl rx rr)))

insert #

theorem ordnode.dual_insert {α : Type u_1} [preorder α] [is_total α has_le.le] [decidable_rel has_le.le] (x : α) (t : ordnode α) :

balance properties #

theorem ordnode.balance_eq_balance' {α : Type u_1} {l : ordnode α} {x : α} {r : ordnode α} (hl : l.balanced) (hr : r.balanced) (sl : l.sized) (sr : r.sized) :
l.balance x r = l.balance' x r
theorem ordnode.balance_l_eq_balance {α : Type u_1} {l : ordnode α} {x : α} {r : ordnode α} (sl : l.sized) (sr : r.sized) (H1 : l.size = 0r.size 1) (H2 : 1 l.size1 r.sizer.size ordnode.delta * l.size) :
l.balance_l x r = l.balance x r
def ordnode.raised (n m : ) :
Prop

raised n m means m is either equal or one up from n.

Equations
theorem ordnode.raised_iff {n m : } :
ordnode.raised n m n m m n + 1
theorem ordnode.raised.dist_le {n m : } (H : ordnode.raised n m) :
n.dist m 1
theorem ordnode.raised.dist_le' {n m : } (H : ordnode.raised n m) :
m.dist n 1
theorem ordnode.raised.add_left (k : ) {n m : } (H : ordnode.raised n m) :
ordnode.raised (k + n) (k + m)
theorem ordnode.raised.add_right (k : ) {n m : } (H : ordnode.raised n m) :
ordnode.raised (n + k) (m + k)
theorem ordnode.raised.right {α : Type u_1} {l : ordnode α} {x₁ x₂ : α} {r₁ r₂ : ordnode α} (H : ordnode.raised r₁.size r₂.size) :
ordnode.raised (l.node' x₁ r₁).size (l.node' x₂ r₂).size
theorem ordnode.balance_l_eq_balance' {α : Type u_1} {l : ordnode α} {x : α} {r : ordnode α} (hl : l.balanced) (hr : r.balanced) (sl : l.sized) (sr : r.sized) (H : (∃ (l' : ), ordnode.raised l' l.size ordnode.balanced_sz l' r.size) ∃ (r' : ), ordnode.raised r.size r' ordnode.balanced_sz l.size r') :
l.balance_l x r = l.balance' x r
theorem ordnode.balance_sz_dual {α : Type u_1} {l r : ordnode α} (H : (∃ (l' : ), ordnode.raised l.size l' ordnode.balanced_sz l' r.size) ∃ (r' : ), ordnode.raised r' r.size ordnode.balanced_sz l.size r') :
theorem ordnode.size_balance_l {α : Type u_1} {l : ordnode α} {x : α} {r : ordnode α} (hl : l.balanced) (hr : r.balanced) (sl : l.sized) (sr : r.sized) (H : (∃ (l' : ), ordnode.raised l' l.size ordnode.balanced_sz l' r.size) ∃ (r' : ), ordnode.raised r.size r' ordnode.balanced_sz l.size r') :
(l.balance_l x r).size = l.size + r.size + 1
theorem ordnode.all_balance_l {α : Type u_1} {P : α → Prop} {l : ordnode α} {x : α} {r : ordnode α} (hl : l.balanced) (hr : r.balanced) (sl : l.sized) (sr : r.sized) (H : (∃ (l' : ), ordnode.raised l' l.size ordnode.balanced_sz l' r.size) ∃ (r' : ), ordnode.raised r.size r' ordnode.balanced_sz l.size r') :
theorem ordnode.balance_r_eq_balance' {α : Type u_1} {l : ordnode α} {x : α} {r : ordnode α} (hl : l.balanced) (hr : r.balanced) (sl : l.sized) (sr : r.sized) (H : (∃ (l' : ), ordnode.raised l.size l' ordnode.balanced_sz l' r.size) ∃ (r' : ), ordnode.raised r' r.size ordnode.balanced_sz l.size r') :
l.balance_r x r = l.balance' x r
theorem ordnode.size_balance_r {α : Type u_1} {l : ordnode α} {x : α} {r : ordnode α} (hl : l.balanced) (hr : r.balanced) (sl : l.sized) (sr : r.sized) (H : (∃ (l' : ), ordnode.raised l.size l' ordnode.balanced_sz l' r.size) ∃ (r' : ), ordnode.raised r' r.size ordnode.balanced_sz l.size r') :
(l.balance_r x r).size = l.size + r.size + 1
theorem ordnode.all_balance_r {α : Type u_1} {P : α → Prop} {l : ordnode α} {x : α} {r : ordnode α} (hl : l.balanced) (hr : r.balanced) (sl : l.sized) (sr : r.sized) (H : (∃ (l' : ), ordnode.raised l.size l' ordnode.balanced_sz l' r.size) ∃ (r' : ), ordnode.raised r' r.size ordnode.balanced_sz l.size r') :

bounded #

def ordnode.bounded {α : Type u_1} [preorder α] :
ordnode αwith_bot αwith_top α → Prop

bounded t lo hi says that every element x ∈ t is in the range lo < x < hi, and also this property holds recursively in subtrees, making the full tree a BST. The bounds can be set to lo = ⊥ and hi = ⊤ if we care only about the internal ordering constraints.

Equations
theorem ordnode.bounded.dual {α : Type u_1} [preorder α] {t : ordnode α} {o₁ : with_bot α} {o₂ : with_top α} (h : t.bounded o₁ o₂) :
t.dual.bounded o₂ o₁
theorem ordnode.bounded.dual_iff {α : Type u_1} [preorder α] {t : ordnode α} {o₁ : with_bot α} {o₂ : with_top α} :
t.bounded o₁ o₂ t.dual.bounded o₂ o₁
theorem ordnode.bounded.weak_left {α : Type u_1} [preorder α] {t : ordnode α} {o₁ : with_bot α} {o₂ : with_top α} :
t.bounded o₁ o₂t.bounded o₂
theorem ordnode.bounded.weak_right {α : Type u_1} [preorder α] {t : ordnode α} {o₁ : with_bot α} {o₂ : with_top α} :
t.bounded o₁ o₂t.bounded o₁
theorem ordnode.bounded.weak {α : Type u_1} [preorder α] {t : ordnode α} {o₁ : with_bot α} {o₂ : with_top α} (h : t.bounded o₁ o₂) :
theorem ordnode.bounded.mono_left {α : Type u_1} [preorder α] {x y : α} (xy : x y) {t : ordnode α} {o : with_top α} :
t.bounded y ot.bounded x o
theorem ordnode.bounded.mono_right {α : Type u_1} [preorder α] {x y : α} (xy : x y) {t : ordnode α} {o : with_bot α} :
t.bounded o xt.bounded o y
theorem ordnode.bounded.to_lt {α : Type u_1} [preorder α] {t : ordnode α} {x y : α} :
t.bounded x yx < y
theorem ordnode.bounded.to_nil {α : Type u_1} [preorder α] {t : ordnode α} {o₁ : with_bot α} {o₂ : with_top α} :
t.bounded o₁ o₂ordnode.nil.bounded o₁ o₂
theorem ordnode.bounded.trans_left {α : Type u_1} [preorder α] {t₁ t₂ : ordnode α} {x : α} {o₁ : with_bot α} {o₂ : with_top α} :
t₁.bounded o₁ xt₂.bounded x o₂t₂.bounded o₁ o₂
theorem ordnode.bounded.trans_right {α : Type u_1} [preorder α] {t₁ t₂ : ordnode α} {x : α} {o₁ : with_bot α} {o₂ : with_top α} :
t₁.bounded o₁ xt₂.bounded x o₂t₁.bounded o₁ o₂
theorem ordnode.bounded.mem_lt {α : Type u_1} [preorder α] {t : ordnode α} {o : with_bot α} {x : α} :
t.bounded o xordnode.all (λ (_x : α), _x < x) t
theorem ordnode.bounded.mem_gt {α : Type u_1} [preorder α] {t : ordnode α} {o : with_top α} {x : α} :
t.bounded x oordnode.all (λ (_x : α), _x > x) t
theorem ordnode.bounded.of_lt {α : Type u_1} [preorder α] {t : ordnode α} {o₁ : with_bot α} {o₂ : with_top α} {x : α} :
t.bounded o₁ o₂ordnode.nil.bounded o₁ xordnode.all (λ (_x : α), _x < x) tt.bounded o₁ x
theorem ordnode.bounded.of_gt {α : Type u_1} [preorder α] {t : ordnode α} {o₁ : with_bot α} {o₂ : with_top α} {x : α} :
t.bounded o₁ o₂ordnode.nil.bounded x o₂ordnode.all (λ (_x : α), _x > x) tt.bounded x o₂
theorem ordnode.bounded.to_sep {α : Type u_1} [preorder α] {t₁ t₂ : ordnode α} {o₁ : with_bot α} {o₂ : with_top α} {x : α} (h₁ : t₁.bounded o₁ x) (h₂ : t₂.bounded x o₂) :
ordnode.all (λ (y : α), ordnode.all (λ (z : α), y < z) t₂) t₁

valid #

structure ordnode.valid' {α : Type u_1} [preorder α] (lo : with_bot α) (t : ordnode α) (hi : with_top α) :
Prop

The validity predicate for an ordnode subtree. This asserts that the size fields are correct, the tree is balanced, and the elements of the tree are organized according to the ordering. This version of valid also puts all elements in the tree in the interval (lo, hi).

def ordnode.valid {α : Type u_1} [preorder α] (t : ordnode α) :
Prop

The validity predicate for an ordnode subtree. This asserts that the size fields are correct, the tree is balanced, and the elements of the tree are organized according to the ordering.

Equations
theorem ordnode.valid'.mono_left {α : Type u_1} [preorder α] {x y : α} (xy : x y) {t : ordnode α} {o : with_top α} (h : ordnode.valid' y t o) :
theorem ordnode.valid'.mono_right {α : Type u_1} [preorder α] {x y : α} (xy : x y) {t : ordnode α} {o : with_bot α} (h : ordnode.valid' o t x) :
theorem ordnode.valid'.trans_left {α : Type u_1} [preorder α] {t₁ t₂ : ordnode α} {x : α} {o₁ : with_bot α} {o₂ : with_top α} (h : t₁.bounded o₁ x) (H : ordnode.valid' x t₂ o₂) :
ordnode.valid' o₁ t₂ o₂
theorem ordnode.valid'.trans_right {α : Type u_1} [preorder α] {t₁ t₂ : ordnode α} {x : α} {o₁ : with_bot α} {o₂ : with_top α} (H : ordnode.valid' o₁ t₁ x) (h : t₂.bounded x o₂) :
ordnode.valid' o₁ t₁ o₂
theorem ordnode.valid'.of_lt {α : Type u_1} [preorder α] {t : ordnode α} {x : α} {o₁ : with_bot α} {o₂ : with_top α} (H : ordnode.valid' o₁ t o₂) (h₁ : ordnode.nil.bounded o₁ x) (h₂ : ordnode.all (λ (_x : α), _x < x) t) :
theorem ordnode.valid'.of_gt {α : Type u_1} [preorder α] {t : ordnode α} {x : α} {o₁ : with_bot α} {o₂ : with_top α} (H : ordnode.valid' o₁ t o₂) (h₁ : ordnode.nil.bounded x o₂) (h₂ : ordnode.all (λ (_x : α), _x > x) t) :
theorem ordnode.valid'.valid {α : Type u_1} [preorder α] {t : ordnode α} {o₁ : with_bot α} {o₂ : with_top α} (h : ordnode.valid' o₁ t o₂) :
theorem ordnode.valid'_nil {α : Type u_1} [preorder α] {o₁ : with_bot α} {o₂ : with_top α} (h : ordnode.nil.bounded o₁ o₂) :
theorem ordnode.valid_nil {α : Type u_1} [preorder α] :
theorem ordnode.valid'.node {α : Type u_1} [preorder α] {s : } {l : ordnode α} {x : α} {r : ordnode α} {o₁ : with_bot α} {o₂ : with_top α} (hl : ordnode.valid' o₁ l x) (hr : ordnode.valid' x r o₂) (H : ordnode.balanced_sz l.size r.size) (hs : s = l.size + r.size + 1) :
ordnode.valid' o₁ (ordnode.node s l x r) o₂
theorem ordnode.valid'.dual {α : Type u_1} [preorder α] {t : ordnode α} {o₁ : with_bot α} {o₂ : with_top α} (h : ordnode.valid' o₁ t o₂) :
ordnode.valid' o₂ t.dual o₁
theorem ordnode.valid'.dual_iff {α : Type u_1} [preorder α] {t : ordnode α} {o₁ : with_bot α} {o₂ : with_top α} :
ordnode.valid' o₁ t o₂ ordnode.valid' o₂ t.dual o₁
theorem ordnode.valid.dual {α : Type u_1} [preorder α] {t : ordnode α} :
t.valid → t.dual.valid
theorem ordnode.valid.dual_iff {α : Type u_1} [preorder α] {t : ordnode α} :
theorem ordnode.valid'.left {α : Type u_1} [preorder α] {s : } {l : ordnode α} {x : α} {r : ordnode α} {o₁ : with_bot α} {o₂ : with_top α} (H : ordnode.valid' o₁ (ordnode.node s l x r) o₂) :
theorem ordnode.valid'.right {α : Type u_1} [preorder α] {s : } {l : ordnode α} {x : α} {r : ordnode α} {o₁ : with_bot α} {o₂ : with_top α} (H : ordnode.valid' o₁ (ordnode.node s l x r) o₂) :
theorem ordnode.valid.left {α : Type u_1} [preorder α] {s : } {l : ordnode α} {x : α} {r : ordnode α} (H : (ordnode.node s l x r).valid) :
theorem ordnode.valid.right {α : Type u_1} [preorder α] {s : } {l : ordnode α} {x : α} {r : ordnode α} (H : (ordnode.node s l x r).valid) :
theorem ordnode.valid.size_eq {α : Type u_1} [preorder α] {s : } {l : ordnode α} {x : α} {r : ordnode α} (H : (ordnode.node s l x r).valid) :
(ordnode.node s l x r).size = l.size + r.size + 1
theorem ordnode.valid'.node' {α : Type u_1} [preorder α] {l : ordnode α} {x : α} {r : ordnode α} {o₁ : with_bot α} {o₂ : with_top α} (hl : ordnode.valid' o₁ l x) (hr : ordnode.valid' x r o₂) (H : ordnode.balanced_sz l.size r.size) :
ordnode.valid' o₁ (l.node' x r) o₂
theorem ordnode.valid'_singleton {α : Type u_1} [preorder α] {x : α} {o₁ : with_bot α} {o₂ : with_top α} (h₁ : ordnode.nil.bounded o₁ x) (h₂ : ordnode.nil.bounded x o₂) :
ordnode.valid' o₁ {x} o₂
theorem ordnode.valid_singleton {α : Type u_1} [preorder α] {x : α} :
{x}.valid
theorem ordnode.valid'.node3_l {α : Type u_1} [preorder α] {l : ordnode α} {x : α} {m : ordnode α} {y : α} {r : ordnode α} {o₁ : with_bot α} {o₂ : with_top α} (hl : ordnode.valid' o₁ l x) (hm : ordnode.valid' x m y) (hr : ordnode.valid' y r o₂) (H1 : ordnode.balanced_sz l.size m.size) (H2 : ordnode.balanced_sz (l.size + m.size + 1) r.size) :
ordnode.valid' o₁ (l.node3_l x m y r) o₂
theorem ordnode.valid'.node3_r {α : Type u_1} [preorder α] {l : ordnode α} {x : α} {m : ordnode α} {y : α} {r : ordnode α} {o₁ : with_bot α} {o₂ : with_top α} (hl : ordnode.valid' o₁ l x) (hm : ordnode.valid' x m y) (hr : ordnode.valid' y r o₂) (H1 : ordnode.balanced_sz l.size (m.size + r.size + 1)) (H2 : ordnode.balanced_sz m.size r.size) :
ordnode.valid' o₁ (l.node3_r x m y r) o₂
theorem ordnode.valid'.node4_l_lemma₁ {a b c d : } (lr₂ : 3 * (b + c + 1 + d) 16 * a + 9) (mr₂ : b + c + 1 3 * d) (mm₁ : b 3 * c) :
b < 3 * a + 1
theorem ordnode.valid'.node4_l_lemma₂ {b c d : } (mr₂ : b + c + 1 3 * d) :
c 3 * d
theorem ordnode.valid'.node4_l_lemma₃ {b c d : } (mr₁ : 2 * d b + c + 1) (mm₁ : b 3 * c) :
d 3 * c
theorem ordnode.valid'.node4_l_lemma₄ {a b c d : } (lr₁ : 3 * a b + c + 1 + d) (mr₂ : b + c + 1 3 * d) (mm₁ : b 3 * c) :
a + b + 1 3 * (c + d + 1)
theorem ordnode.valid'.node4_l_lemma₅ {a b c d : } (lr₂ : 3 * (b + c + 1 + d) 16 * a + 9) (mr₁ : 2 * d b + c + 1) (mm₂ : c 3 * b) :
c + d + 1 3 * (a + b + 1)
theorem ordnode.valid'.node4_l {α : Type u_1} [preorder α] {l : ordnode α} {x : α} {m : ordnode α} {y : α} {r : ordnode α} {o₁ : with_bot α} {o₂ : with_top α} (hl : ordnode.valid' o₁ l x) (hm : ordnode.valid' x m y) (hr : ordnode.valid' y r o₂) (Hm : 0 < m.size) (H : l.size = 0 m.size = 1 r.size 1 0 < l.size ordnode.ratio * r.size m.size ordnode.delta * l.size m.size + r.size 3 * (m.size + r.size) 16 * l.size + 9 m.size ordnode.delta * r.size) :
ordnode.valid' o₁ (l.node4_l x m y r) o₂
theorem ordnode.valid'.rotate_l_lemma₁ {a b c : } (H2 : 3 * a b + c) (hb₂ : c 3 * b) :
a 3 * b
theorem ordnode.valid'.rotate_l_lemma₂ {a b c : } (H3 : 2 * (b + c) 9 * a + 3) (h : b < 2 * c) :
b < 3 * a + 1
theorem ordnode.valid'.rotate_l_lemma₃ {a b c : } (H2 : 3 * a b + c) (h : b < 2 * c) :
a + b < 3 * c
theorem ordnode.valid'.rotate_l_lemma₄ {a b : } (H3 : 2 * b 9 * a + 3) :
3 * b 16 * a + 9
theorem ordnode.valid'.rotate_l {α : Type u_1} [preorder α] {l : ordnode α} {x : α} {r : ordnode α} {o₁ : with_bot α} {o₂ : with_top α} (hl : ordnode.valid' o₁ l x) (hr : ordnode.valid' x r o₂) (H1 : ¬l.size + r.size 1) (H2 : ordnode.delta * l.size < r.size) (H3 : 2 * r.size 9 * l.size + 5 r.size 3) :
ordnode.valid' o₁ (l.rotate_l x r) o₂
theorem ordnode.valid'.rotate_r {α : Type u_1} [preorder α] {l : ordnode α} {x : α} {r : ordnode α} {o₁ : with_bot α} {o₂ : with_top α} (hl : ordnode.valid' o₁ l x) (hr : ordnode.valid' x r o₂) (H1 : ¬l.size + r.size 1) (H2 : ordnode.delta * r.size < l.size) (H3 : 2 * l.size 9 * r.size + 5 l.size 3) :
ordnode.valid' o₁ (l.rotate_r x r) o₂
theorem ordnode.valid'.balance'_aux {α : Type u_1} [preorder α] {l : ordnode α} {x : α} {r : ordnode α} {o₁ : with_bot α} {o₂ : with_top α} (hl : ordnode.valid' o₁ l x) (hr : ordnode.valid' x r o₂) (H₁ : 2 * r.size 9 * l.size + 5 r.size 3) (H₂ : 2 * l.size 9 * r.size + 5 l.size 3) :
ordnode.valid' o₁ (l.balance' x r) o₂
theorem ordnode.valid'.balance'_lemma {α : Type u_1} {l : ordnode α} {l' : } {r : ordnode α} {r' : } (H1 : ordnode.balanced_sz l' r') (H2 : l.size.dist l' 1 r.size = r' r.size.dist r' 1 l.size = l') :
2 * r.size 9 * l.size + 5 r.size 3
theorem ordnode.valid'.balance' {α : Type u_1} [preorder α] {l : ordnode α} {x : α} {r : ordnode α} {o₁ : with_bot α} {o₂ : with_top α} (hl : ordnode.valid' o₁ l x) (hr : ordnode.valid' x r o₂) (H : ∃ (l' r' : ), ordnode.balanced_sz l' r' (l.size.dist l' 1 r.size = r' r.size.dist r' 1 l.size = l')) :
ordnode.valid' o₁ (l.balance' x r) o₂
theorem ordnode.valid'.balance {α : Type u_1} [preorder α] {l : ordnode α} {x : α} {r : ordnode α} {o₁ : with_bot α} {o₂ : with_top α} (hl : ordnode.valid' o₁ l x) (hr : ordnode.valid' x r o₂) (H : ∃ (l' r' : ), ordnode.balanced_sz l' r' (l.size.dist l' 1 r.size = r' r.size.dist r' 1 l.size = l')) :
ordnode.valid' o₁ (l.balance x r) o₂
theorem ordnode.valid'.balance_l_aux {α : Type u_1} [preorder α] {l : ordnode α} {x : α} {r : ordnode α} {o₁ : with_bot α} {o₂ : with_top α} (hl : ordnode.valid' o₁ l x) (hr : ordnode.valid' x r o₂) (H₁ : l.size = 0r.size 1) (H₂ : 1 l.size1 r.sizer.size ordnode.delta * l.size) (H₃ : 2 * l.size 9 * r.size + 5 l.size 3) :
ordnode.valid' o₁ (l.balance_l x r) o₂
theorem ordnode.valid'.balance_l {α : Type u_1} [preorder α] {l : ordnode α} {x : α} {r : ordnode α} {o₁ : with_bot α} {o₂ : with_top α} (hl : ordnode.valid' o₁ l x) (hr : ordnode.valid' x r o₂) (H : (∃ (l' : ), ordnode.raised l' l.size ordnode.balanced_sz l' r.size) ∃ (r' : ), ordnode.raised r.size r' ordnode.balanced_sz l.size r') :
ordnode.valid' o₁ (l.balance_l x r) o₂
theorem ordnode.valid'.balance_r_aux {α : Type u_1} [preorder α] {l : ordnode α} {x : α} {r : ordnode α} {o₁ : with_bot α} {o₂ : with_top α} (hl : ordnode.valid' o₁ l x) (hr : ordnode.valid' x r o₂) (H₁ : r.size = 0l.size 1) (H₂ : 1 r.size1 l.sizel.size ordnode.delta * r.size) (H₃ : 2 * r.size 9 * l.size + 5 r.size 3) :
ordnode.valid' o₁ (l.balance_r x r) o₂
theorem ordnode.valid'.balance_r {α : Type u_1} [preorder α] {l : ordnode α} {x : α} {r : ordnode α} {o₁ : with_bot α} {o₂ : with_top α} (hl : ordnode.valid' o₁ l x) (hr : ordnode.valid' x r o₂) (H : (∃ (l' : ), ordnode.raised l.size l' ordnode.balanced_sz l' r.size) ∃ (r' : ), ordnode.raised r' r.size ordnode.balanced_sz l.size r') :
ordnode.valid' o₁ (l.balance_r x r) o₂
theorem ordnode.valid'.erase_max_aux {α : Type u_1} [preorder α] {s : } {l : ordnode α} {x : α} {r : ordnode α} {o₁ : with_bot α} {o₂ : with_top α} (H : ordnode.valid' o₁ (ordnode.node s l x r) o₂) :
theorem ordnode.valid'.erase_min_aux {α : Type u_1} [preorder α] {s : } {l : ordnode α} {x : α} {r : ordnode α} {o₁ : with_bot α} {o₂ : with_top α} (H : ordnode.valid' o₁ (ordnode.node s l x r) o₂) :
ordnode.valid' (l.find_min' x) (l.node' x r).erase_min o₂ (l.node' x r).size = (l.node' x r).erase_min.size + 1
theorem ordnode.erase_min.valid {α : Type u_1} [preorder α] {t : ordnode α} (h : t.valid) :
theorem ordnode.erase_max.valid {α : Type u_1} [preorder α] {t : ordnode α} (h : t.valid) :
theorem ordnode.valid'.glue_aux {α : Type u_1} [preorder α] {l r : ordnode α} {o₁ : with_bot α} {o₂ : with_top α} (hl : ordnode.valid' o₁ l o₂) (hr : ordnode.valid' o₁ r o₂) (sep : ordnode.all (λ (x : α), ordnode.all (λ (y : α), x < y) r) l) (bal : ordnode.balanced_sz l.size r.size) :
ordnode.valid' o₁ (l.glue r) o₂ (l.glue r).size = l.size + r.size
theorem ordnode.valid'.glue {α : Type u_1} [preorder α] {l : ordnode α} {x : α} {r : ordnode α} {o₁ : with_bot α} {o₂ : with_top α} (hl : ordnode.valid' o₁ l x) (hr : ordnode.valid' x r o₂) :
ordnode.balanced_sz l.size r.sizeordnode.valid' o₁ (l.glue r) o₂ (l.glue r).size = l.size + r.size
theorem ordnode.valid'.merge_lemma {a b c : } (h₁ : 3 * a < b + c + 1) (h₂ : b 3 * c) :
2 * (a + b) 9 * c + 5
theorem ordnode.valid'.merge_aux₁ {α : Type u_1} [preorder α] {o₁ : with_bot α} {o₂ : with_top α} {ls : } {ll : ordnode α} {lx : α} {lr : ordnode α} {rs : } {rl : ordnode α} {rx : α} {rr t : ordnode α} (hl : ordnode.valid' o₁ (ordnode.node ls ll lx lr) o₂) (hr : ordnode.valid' o₁ (ordnode.node rs rl rx rr) o₂) (h : ordnode.delta * ls < rs) (v : ordnode.valid' o₁ t rx) (e : t.size = ls + rl.size) :
ordnode.valid' o₁ (t.balance_l rx rr) o₂ (t.balance_l rx rr).size = ls + rs
theorem ordnode.valid'.merge_aux {α : Type u_1} [preorder α] {l r : ordnode α} {o₁ : with_bot α} {o₂ : with_top α} (hl : ordnode.valid' o₁ l o₂) (hr : ordnode.valid' o₁ r o₂) (sep : ordnode.all (λ (x : α), ordnode.all (λ (y : α), x < y) r) l) :
ordnode.valid' o₁ (l.merge r) o₂ (l.merge r).size = l.size + r.size
theorem ordnode.valid.merge {α : Type u_1} [preorder α] {l r : ordnode α} (hl : l.valid) (hr : r.valid) (sep : ordnode.all (λ (x : α), ordnode.all (λ (y : α), x < y) r) l) :
(l.merge r).valid
theorem ordnode.insert_with.valid_aux {α : Type u_1} [preorder α] [is_total α has_le.le] [decidable_rel has_le.le] (f : α → α) (x : α) (hf : ∀ (y : α), x y y xx f y f y x) {t : ordnode α} {o₁ : with_bot α} {o₂ : with_top α} :
theorem ordnode.insert_with.valid {α : Type u_1} [preorder α] [is_total α has_le.le] [decidable_rel has_le.le] (f : α → α) (x : α) (hf : ∀ (y : α), x y y xx f y f y x) {t : ordnode α} (h : t.valid) :
theorem ordnode.insert_eq_insert_with {α : Type u_1} [preorder α] [decidable_rel has_le.le] (x : α) (t : ordnode α) :
ordnode.insert x t = ordnode.insert_with (λ (_x : α), x) x t
theorem ordnode.insert.valid {α : Type u_1} [preorder α] [is_total α has_le.le] [decidable_rel has_le.le] (x : α) {t : ordnode α} (h : t.valid) :
theorem ordnode.insert'.valid {α : Type u_1} [preorder α] [is_total α has_le.le] [decidable_rel has_le.le] (x : α) {t : ordnode α} (h : t.valid) :
def ordset (α : Type u_1) [preorder α] :
Type u_1

An ordset α 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. The correctness property of the tree is baked into the type, so all operations on this type are correct by construction.

Equations
def ordset.nil {α : Type u_1} [preorder α] :

O(1). The empty set.

Equations
def ordset.size {α : Type u_1} [preorder α] (s : ordset α) :

O(1). Get the size of the set.

Equations
def ordset.singleton {α : Type u_1} [preorder α] (a : α) :

O(1). Construct a singleton set containing value a.

Equations
@[instance]
def ordset.has_emptyc {α : Type u_1} [preorder α] :
Equations
@[instance]
def ordset.inhabited {α : Type u_1} [preorder α] :
Equations
@[instance]
def ordset.has_singleton {α : Type u_1} [preorder α] :
Equations
def ordset.empty {α : Type u_1} [preorder α] (s : ordset α) :
Prop

O(1). Is the set empty?

Equations
theorem ordset.empty_iff {α : Type u_1} [preorder α] {s : ordset α} :
def ordset.insert {α : Type u_1} [preorder α] [is_total α has_le.le] [decidable_rel has_le.le] (x : α) (s : ordset α) :

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.

Equations
@[instance]
Equations
def ordset.insert' {α : Type u_1} [preorder α] [is_total α has_le.le] [decidable_rel has_le.le] (x : α) (s : ordset α) :

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.

Equations