Tool generates verified navigation code for algebraic data structures
Datatypes In, Zippers Out!
Programming Languages
Summary
Manipulating complex data structures can be tricky, especially when you want to focus on one part without losing sight of the whole. The authors created a tool called Kamot that automatically generates precise and verified navigation code for these structures, ensuring moves like looking around or replacing parts are correct and efficient. They proved their tool’s correctness and worked with many example data types, showing it handles complicated cases and infinite inputs well. This helps developers build reliable software that navigates data safely and predictably.
What this means in practice
- •For functional programmers: Automatically generate correctness-verified navigation code for complex algebraic data types in Haskell.
- •For software verification engineers: Use operational contracts and formal bounds to certify safe traversal and modification of recursive data structures.
Authors
Smayan Agarwal, Aalok Thakkar
Abstract
A zipper represents a focused position in an algebraic data structure together with the context needed to reconstruct the whole. Datatype derivatives determine one-hole context types, but do not by themselves choose a zipper's concrete representation or specify navigation costs and demands. We present Kamot, a tool that converts regular algebraic datatype declarations in a restricted Haskell fragment into zipper implementations with operational contracts. Navigation, replacement, and reconstruction are proved correct against a reference traversal. Kamot supports mutual recursion and regular containers inside containers. A compositional analysis derives sound bounds on transitions and constructor demands for certified traversal directions. These bounds are affine in trail depth (the number of saved frames) and guarantee termination of each move on productive, potentially infinite inputs. For inhabited families the analysis is complete: when no move certificate is emitted, no bound depending only on trail depth exists, and some productive input makes a move diverge. We prove that the generated code preserves results and the reference order of constructor matches, including finite prefixes of divergent searches. Lean 4 verifies simulation, bounds, and completeness for finite values of single self-recursive datatypes, in both directions. On 19 datatype models and 81 generated families, every generated module passes Haskell type checking, and independent checkers validate certificates, traces, and bounds. In a prefetching workload, starting only searches whose bounds fit a budget answers every local query and avoids discarded source evaluations on long returns, but conservative bounds can lose answers that fuelled search delivers.