Invariants for the verification of Ordnode
#
An Ordnode
, defined in Mathlib/Data/Ordmap/Ordnode.lean
, is an inductive type which describes a
tree which stores the size
at internal nodes.
In this file we define the correctness invariant of an Ordnode
, comprising:
Ordnode.Sized t
: All internalsize
fields must match the actual measured size of the tree. (This is not hard to satisfy.)Ordnode.Balanced t
: Unless the tree has the form()
or((a) b)
or(a (b))
(that is, nil or a single singleton subtree), the two subtrees must satisfysize l ≤ δ * size r
andsize r ≤ δ * size l
, whereδ := 3
is a global parameter of the data structure (and this property must hold recursively at subtrees). This is why we say this is a "size balanced tree" data structure.Ordnode.Bounded lo hi t
: The members of the tree must be in strictly increasing order, meaning that ifa
is in the left subtree andb
is the root, thena ≤ b
and¬(b ≤ a)
. We enforce this usingOrdnode.Bounded
which includes also a global upper and lower bound.
This whole file is in the Ordnode
namespace, because we first have to prove the correctness of
all the operations (and defining what correctness means here is somewhat subtle).
The actual Ordset
operations are in Mathlib/Data/Ordmap/Ordset.lean
.
TODO #
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 #
singleton
#
size
and empty
#
O(n). Computes the actual number of elements in the set, ignoring the cached size
field.
Equations
Instances For
The BalancedSz 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
Instances For
rotate
and balance
#
All
, Any
, Emem
, Amem
#
toList
#
mem
#
(find/erase/split)(Min/Max)
#
glue
#
merge
#
insert
#
balance
properties #
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.