HN
Today

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.

68
Score
23
Comments
#6
Highest Rank
15h
on Front Page
First Seen
Nov 25, 5:00 PM
Last Seen
Nov 26, 2:00 PM
Rank Over Time
67610914171819192430283030

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.util collections for larger persistent data structures.
  • One critical bug found was an infinite loop in reduce on vector-of for large collections (65505+ elements), caused by an incorrect bitmask that reset the internal index to zero.
  • Another significant issue was in dissoc on HashCollisionNode within 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 apply RT.booleanCast, leading to different and unexpected results compared to type-hinted calls.
  • Additional bugs included int overflows in IDrop implementations (e.g., StringSeq.drop) and assoc allowing negative indices in SubVector, leading to out-of-bounds writes on underlying vectors.
  • Several other minor bugs were identified, affecting reduce-kv on vector-of, functional interface behavior, metadata retention, numeric conversions (e.g., ##NaN to long/int), long/int clamping, char wrapping, float handling of ##Inf, byte conversion range checks, fractional indices, byte-array/short-array initialization, and inconsistencies in indexOf/lastIndexOf behavior.
  • 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.