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 #
Ordset α
: A well formed set of values of typeα
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:
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.
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'.balanceL_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 #
O(n). Computes the actual number of elements in the set, ignoring the cached size
field.
Equations
- Ordnode.realSize Ordnode.nil = 0
- Ordnode.realSize (Ordnode.node size l x_1 r) = Ordnode.realSize l + Ordnode.realSize r + 1
Instances For
The Sized
property asserts that all the size
fields in nodes match the actual size of the
respective subtrees.
Equations
- Ordnode.Sized Ordnode.nil = True
- Ordnode.Sized (Ordnode.node size l x_1 r) = (size = Ordnode.size l + Ordnode.size r + 1 ∧ Ordnode.Sized l ∧ Ordnode.Sized r)
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
- Ordnode.BalancedSz l r = (l + r ≤ 1 ∨ l ≤ Ordnode.delta * r ∧ r ≤ Ordnode.delta * l)
Instances For
Equations
- Ordnode.BalancedSz.dec x✝ x = Or.decidable
The Balanced t
asserts that the tree t
satisfies the balance invariants
(at every level).
Equations
- Ordnode.Balanced Ordnode.nil = True
- Ordnode.Balanced (Ordnode.node size l x_1 r) = (Ordnode.BalancedSz (Ordnode.size l) (Ordnode.size r) ∧ Ordnode.Balanced l ∧ Ordnode.Balanced r)
Instances For
Equations
- One or more equations did not get rendered due to their size.
- Ordnode.Balanced.dec Ordnode.nil = Eq.mpr (_ : Decidable (Ordnode.Balanced Ordnode.nil) = Decidable True) inferInstance
Build a tree from three nodes, left associated (ignores the invariants).
Equations
- Ordnode.node3L l x m y r = Ordnode.node' (Ordnode.node' l x m) y r
Instances For
Build a tree from three nodes, right associated (ignores the invariants).
Equations
- Ordnode.node3R l x m y r = Ordnode.node' l x (Ordnode.node' m y r)
Instances For
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
- One or more equations did not get rendered due to their size.
Instances For
All
, Any
, Emem
, Amem
#
toList
#
(find/erase/split)(Min/Max)
#
bounded
#
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
- Ordnode.Bounded Ordnode.nil (some a) (some b) = (a < b)
- Ordnode.Bounded Ordnode.nil x✝ x = True
- Ordnode.Bounded (Ordnode.node size l x_3 r) x✝ x = (Ordnode.Bounded l x✝ ↑x_3 ∧ Ordnode.Bounded r (↑x_3) x)
Instances For
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)
.
- ord : Ordnode.Bounded t lo hi
- sz : Ordnode.Sized t
- bal : Ordnode.Balanced t
Instances For
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
- Ordnode.Valid t = Ordnode.Valid' ⊥ t ⊤
Instances For
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
- Ordset α = { t : Ordnode α // Ordnode.Valid t }
Instances For
O(1). The empty set.
Equations
- Ordset.nil = { val := Ordnode.nil, property := (_ : Ordnode.Valid' ⊥ Ordnode.nil ⊤) }
Instances For
O(1). Construct a singleton set containing value a
.
Equations
- Ordset.singleton a = { val := {a}, property := (_ : Ordnode.Valid {a}) }
Instances For
Equations
- Ordset.instEmptyCollection = { emptyCollection := Ordset.nil }
Equations
- Ordset.Empty.instDecidablePred x = decidable_of_iff' (Ordnode.empty ↑x = true) (_ : x = ∅ ↔ Ordnode.empty ↑x = true)
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
- Ordset.insert x s = { val := Ordnode.insert x ↑s, property := (_ : Ordnode.Valid (Ordnode.insert x ↑s)) }
Instances For
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
- Ordset.insert' x s = { val := Ordnode.insert' x ↑s, property := (_ : Ordnode.Valid (Ordnode.insert' x ↑s)) }
Instances For
O(log n). Does the set contain the element x
? That is,
is there an element that is equivalent to x
in the order?
Equations
- Ordset.mem x s = decide (x ∈ ↑s)
Instances For
O(log n). Retrieve an element in the set that is equivalent to x
in the order,
if it exists.
Equations
- Ordset.find x s = Ordnode.find x ↑s
Instances For
Equations
- Ordset.instMembership = { mem := fun (x : α) (s : Ordset α) => Ordset.mem x s = true }
Equations
- Ordset.mem.decidable x s = instDecidableEqBool (Ordset.mem x s) true
O(log n). Remove an element from the set equivalent to x
. Does nothing if there
is no such element.
Equations
- Ordset.erase x s = { val := Ordnode.erase x ↑s, property := (_ : Ordnode.Valid (Ordnode.erase x ↑s)) }
Instances For
O(n). Map a function across a tree, without changing the structure.
Equations
- Ordset.map f f_strict_mono s = { val := Ordnode.map f ↑s, property := (_ : Ordnode.Valid (Ordnode.map f ↑s)) }