I checked Clojure's core with Lean and found unreported bugs
A researcher systematically applied formal verification with Lean 4 and differential testing to Clojure's core libraries, unearthing 17 previously unreported bugs, including critical logic errors and infinite loops, in widely used components like HAMT and vector-of. The revelation that even a mature, battle-tested codebase harbors such subtle flaws sparked extensive debate on the practical utility of formal methods and the definition of 'bug' in dynamically typed languages. This deep dive into compiler-level correctness, combined with the surprising findings, made it a hot topic for HN's technical audience, highlighting the ongoing quest for software reliability.
The Lowdown
Inspired by a similar project in Rust, a researcher undertook a meticulous formal verification of Clojure's core libraries using Lean 4 and differential testing. Despite Clojure's long history and widespread use, the project uncovered 17 previously unreported bugs, demonstrating the power of these advanced techniques in revealing subtle flaws even in highly mature codebases.
- The methodology involved modeling small functions in Lean to find counterexamples for integer and bit arithmetic, and employing differential testing against
java.utilcollections for larger persistent data structures. - One critical bug found was an infinite loop in
reduceonvector-offor large collections (65505+ elements), caused by an incorrect bitmask that reset the internal index to zero. - Another significant issue was in
dissoconHashCollisionNodewithin the HAMT, where passing the incorrect hash during node reconstruction could effectively hide a key, rendering it unretrievable. - Type hint inconsistencies led to a bug in
removeIf, where reflective calls without type hints would applyRT.booleanCast, leading to different and unexpected results compared to type-hinted calls. - Additional bugs included
intoverflows inIDropimplementations (e.g.,StringSeq.drop) andassocallowing negative indices inSubVector, leading to out-of-bounds writes on underlying vectors. - Several other minor bugs were identified, affecting
reduce-kvonvector-of, functional interface behavior, metadata retention, numeric conversions (e.g.,##NaNtolong/int),long/intclamping,charwrapping,floathandling of##Inf,byteconversion range checks, fractional indices,byte-array/short-arrayinitialization, and inconsistencies inindexOf/lastIndexOfbehavior. - Notably, extensive testing of
PersistentHashMap,PersistentHashSet,PersistentTreeMap, and various numeric overflow checks (for addition/subtraction) using these methods did not reveal any bugs, validating their robustness.
This investigation underscores that even systems built with care over many years can harbor obscure bugs, and that sophisticated formal verification techniques offer an invaluable, albeit demanding, path to higher levels of software correctness.
The Gossip
Clojure's Core: Bugs or Features?
Initial comments sparked a debate on whether findings in a dynamically typed language like Clojure should truly be considered 'bugs,' suggesting that its untyped nature might imply lower correctness guarantees. This sentiment was swiftly countered by others, who emphasized that fundamental correctness issues—such as infinite loops, data corruption, or inconsistent behavior—are unequivocally bugs, regardless of typing, and that formal methods provide a rigorous way to confirm or deny expected properties in any system.
Formal Verification's Fervor: Beyond Haskell's Horizon
Commenters delved into the spectrum of type systems and formal verification, contrasting Haskell's strong typing with the advanced capabilities of proof assistants like Lean, Idris, and Agda. The consensus emerged that while Haskell offers significant advantages over dynamically typed languages, 'true' formal verification (the kind applied in the article) demands a much higher level of specification and rigor, moving beyond mere type safety to provable correctness properties.
Hickey's Hammock & AI's Horizon
A notable tangent explored Rich Hickey's (Clojure's creator) philosophical stance, particularly his 'hammock-driven development' and, more extensively, his outspoken critical views on AI/LLMs. The discussion featured both disappointment with Hickey's 'anti-AI' position and a nuanced debate on whether LLMs function as genuine 'tools' or as a form of 'intrinsic automation.' Some users also shared their positive experiences using LLMs with Lisp, contradicting common concerns about parenthesis balancing.