The Functional Essence of Imperative Binary Search Trees

Publication date

2024-06-01

Authors

Lorenzen, Anton
Leijen, Daan
Swierstra, W.S.ORCID 0000-0002-0295-7944ISNI 0000000426852359
Lindley, Sam

Editors

Advisors

Supervisors

Document Type

Article
Open Access logo

License

cc_by

Abstract

Algorithms on restructuring binary search trees are typically presented in imperative pseudocode. Understandably so, as their performance relies on in-place execution, rather than the repeated allocation of fresh nodes in memory. Unfortunately, these imperative algorithms are notoriously difficult to verify as their loop invariants must relate the unfinished tree fragments being rebalanced. This paper presents several novel functional algorithms for accessing and inserting elements in a restructuring binary search tree that are as fast as their imperative counterparts; yet the correctness of these functional algorithms is established using a simple inductive argument. For each data structure, move-to-root, splay, and zip trees, this paper describes both a bottom-up algorithm using zippers and a top-down algorithm using a novel first-class constructor context primitive. The functional and imperative algorithms are equivalent: we mechanise the proofs establishing this in the Coq proof assistant using the Iris framework. This yields a first fully verified implementation of well known algorithms on binary search trees with performance on par with the fastest implementations in C.

Keywords

FBIP, FIP, Splay Trees, Tail Recursion Modulo Cons, Zip Trees, Zippers

Citation

Lorenzen, A, Leijen, D, Swierstra, W & Lindley, S 2024, 'The Functional Essence of Imperative Binary Search Trees', Proceedings of the ACM on Programming Languages, vol. 8, no. PLDI, 168, pp. 518-542. https://doi.org/10.1145/3656398