Formal verification with Lean reveals 17 hidden bugs in Clojure 1.12
A developer used Lean 4 and differential testing to uncover unreported bugs in Clojure 1.12.7, including infinite loops in vector-of and hash map corruption.
A recent technical audit of Clojure 1.12.7 has uncovered 17 previously unreported bugs in the language’s core libraries. The investigation, conducted by a developer using the Lean 4 proof assistant and differential testing techniques, targeted fundamental data structures and arithmetic operations. These defects range from infinite loops in primitive vectors to silent data corruption in hash maps, challenging the assumption that mature persistent data structures are free of logical errors.
What happened
The researcher applied formal verification methods to Clojure’s Java-based core, focusing on version 1.12.7 released in November 2026. Inspired by similar work verifying Rust projects, the author modeled small Clojure functions in Lean 4 to check for counterexamples in integer and bit arithmetic. For larger data structures, they employed differential testing, comparing Clojure’s persistent collections against standard Java collections like java.util.HashMap and ArrayList under millions of random operations.
The process yielded 17 distinct bugs that had not been documented in Clojure’s JIRA or community forums. While the core Hash Array Mapped Trie (HAMT) and vector tree structures proved robust under differential testing, bugs were found in the surrounding logic. These included issues in vector-of reduction, hash collision node handling, and type hinting behavior during Java interop. Each bug was reproduced in the original code, and in several cases, the author verified that proposed fixes satisfied the formal properties in Lean.
How it works
The verification strategy combined two complementary approaches. For low-level arithmetic and bit manipulation, the author used bv_decide in Lean 4. This tool searches for counterexamples by modeling Java long and int types as 64-bit and 32-bit bit vectors, respectively. This allows precise modeling of wraparound behavior, rounding, and sign rules. If Lean finds a counterexample, the specific input values are used to reproduce the bug in Clojure.
For complex stateful structures like PersistentHashMap, formal modeling is often impractical. Instead, the author used differential testing. This involves applying identical sequences of operations—such as insertions, deletions, and lookups—to both the Clojure implementation and a trusted reference implementation like java.util.HashMap. By running 1.5 million operations across thousands of seeds, the test ensures that the visible behavior of the persistent structure matches the mutable reference, catching inconsistencies in edge cases like hash collisions or tree rebalancing.
Key details
- Infinite loop in
vector-of: Reducing over a dropped primitive vector with 65,505 or more elements causes an infinite loop due to a bitmask error in chunk calculation. - Hash map corruption: Removing a key from a HAMT collision node using a different but equal key can corrupt the node’s hash, causing subsequent lookups to fail silently.
- Interop inconsistency: Calling Java functional interfaces like
.removeIfyields different results depending on whether the argument has a type hint, due to inconsistent boolean coercion in reflection. - Integer overflow: The
dropfunction on string sequences can produce negative indices when adding large integers, leading toStringIndexOutOfBoundsException. - Subvector mutation: Using
assocwith a negative index on a subvector can modify the underlying parent vector, violating immutability guarantees. - Numeric quirks: Several minor bugs exist in numeric conversions, such as
(long ##NaN)returning 0 instead of throwing, and inconsistent clamping for large floating-point numbers.
Why it matters
For developers building systems on Clojure, these findings highlight that even well-established libraries can harbor subtle defects in rarely exercised code paths. The infinite loop in vector-of is particularly dangerous because it does not throw an exception but simply hangs the process, potentially causing outages in production services processing large datasets. Similarly, the hash map corruption bug can lead to data loss that is difficult to diagnose, as the map appears valid when printed but fails to retrieve existing keys.
The study also demonstrates the growing practicality of formal verification tools like Lean 4 for everyday software engineering. While differential testing has long been a staple for finding bugs, combining it with formal proofs for critical arithmetic logic provides a higher degree of confidence. This hybrid approach allows engineers to verify the correctness of low-level primitives mathematically while still stress-testing high-level state machines empirically.
What you can do
- Audit any usage of
vector-ofwithdropandreducein your codebase, especially if dealing with large collections, and consider replacing them with standard vectors if possible. - Be cautious when using
dissocon hash maps with keys that may be equal but have different hash codes, such as mixing Clojure vectors and Java ArrayLists. - Always provide explicit type hints when calling Java functional interfaces from Clojure to ensure consistent behavior between compiled and reflective calls.
- Test edge cases involving large integers in sequence operations like
dropandsubvecto avoid overflow-related exceptions or silent data corruption. - Consider adopting differential testing for your own custom data structures by comparing them against standard library implementations under randomized workloads.
- Stay updated with Clojure release notes, as some of these issues may be addressed in future patches following this report.



