Balancing operations #
This file contains the implementation of internal balancing operations used by the modification operations of the tree map and proves that these operations produce balanced trees.
Implementation #
Although it is desirable to separate the implementation from the balancedness proofs as much as
possible, we want the Lean to optimize away some impossible case distinctions. Therefore, we need to
prove them impossible in the implementation itself. Most proofs are automated using a custom
tactic tree_tac
, but the proof terms tend to be large, so we should be cautious.
Implementations marked with an exclamation mark do not rely on balancing proofs and just panic when a case occurs that is impossible for balanced trees. These implementations are slower because the impossible cases need to be checked for.
Implementation #
Precondition for balanceL
: at most one element was added to left subtree.
Equations
- Std.DTreeMap.Internal.Impl.BalanceLPrecond left right = (Std.DTreeMap.Internal.Impl.BalancedAtRoot left right ∨ 1 ≤ left ∧ Std.DTreeMap.Internal.Impl.BalancedAtRoot (left - 1) right)
Instances For
Precondition for balanceLErase
. As Breitner et al. remark, "not very educational".
Equations
- One or more equations did not get rendered due to their size.
Instances For
Internal implementation detail of the tree map
Equations
- Std.DTreeMap.Internal.Impl.tacticTree_tac = Lean.ParserDescr.node `Std.DTreeMap.Internal.Impl.tacticTree_tac 1024 (Lean.ParserDescr.nonReservedSymbol "tree_tac" false)
Instances For
Internal implementation detail of the tree map
Equations
- Std.DTreeMap.Internal.Impl.«term✓» = Lean.ParserDescr.node `Std.DTreeMap.Internal.Impl.«term✓» 1024 (Lean.ParserDescr.symbol "✓")
Instances For
Rebalances a tree after at most one element was added to the left subtree. Uses balancing information to show that some cases are impossible.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Slower version of balanceL
with weaker balancing assumptions that hold after erasing from
the right side of the tree.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Slower version of balanceL
which can be used in the complete absence of balancing information
but still assumes that the preconditions of balanceL
or balanceL
are satisfied
(otherwise panics).
Equations
- One or more equations did not get rendered due to their size.
Instances For
Rebalances a tree after at most one element was added to the right subtree. Uses balancing information to show that some cases are impossible.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Slower version of balanceR
with weaker balancing assumptions that hold after erasing from
the left side of the tree.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Slower version of balanceR
which can be used in the complete absence of balancing information
but still assumes that the preconditions of balanceR
or balanceRErase
are satisfied
(otherwise panics).
Equations
- One or more equations did not get rendered due to their size.
Instances For
Rebalances a tree after at most one element was added or removed from either subtree.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Slower version of balance
which can be used in the complete absence of balancing information
but still assumes that the preconditions of balance
are satisfied
(otherwise panics).
Equations
- One or more equations did not get rendered due to their size.
Instances For
Lemmas about balancing operations #
The following definitions are not actually used by the tree map implementation. They are only used
in the model function balanceₘ
, which exists for proof purposes only.
The terminology is consistent with the comment above
the balance
implementation
in Haskell.
Constructor for an inner node with the correct size.
Equations
- Std.DTreeMap.Internal.Impl.bin k v l r = Std.DTreeMap.Internal.Impl.inner (l.size + 1 + r.size) k v l r
Instances For
A single left rotation.
Equations
- Std.DTreeMap.Internal.Impl.singleL k v l rk rv rl rr = Std.DTreeMap.Internal.Impl.bin rk rv (Std.DTreeMap.Internal.Impl.bin k v l rl) rr
Instances For
A single right rotation.
Equations
- Std.DTreeMap.Internal.Impl.singleR k v lk lv ll lr r = Std.DTreeMap.Internal.Impl.bin lk lv ll (Std.DTreeMap.Internal.Impl.bin k v lr r)