Reverse-Engineering the Lost OCaml Code in Apache DataSketches

The community is reverse-engineering DataSketches' lost OCaml prototype code from the Java implementation.
Apache DataSketches' core algorithms, contributed by Kevin J. Lang, were originally prototyped in OCaml and later ported to Java, with the original OCaml code lost due to the lack of version control. The community recently attempted to reconstruct an OCaml implementation from the Java source and papers; the main challenges are the differences in type systems, module systems, and functional versus imperative paradigms, plus reproducing optimizations like in-place updates. The work uses incremental verification to ensure behavioral consistency. The recovered OCaml code aids teaching and formal verification and reminds developers to value code archiving.
Reverse-Engineering the Lost OCaml Code in Apache DataSketches
Apache DataSketches is a library of sketch algorithms for large-scale streaming analytics, with core algorithms contributed by Kevin J. Lang. What is less widely known is that these algorithms were originally written in OCaml, and the original OCaml code has long been lost. Recently, the community launched a reverse-engineering effort to recover an OCaml version from the existing Java implementation.
Background: Why the OCaml Code Was Lost
According to discussion on Hacker News, Kevin J. Lang prototyped the algorithms in OCaml while developing DataSketches, and only later ported them to Java. Because there was no habit of version control or archiving at the time, the OCaml code was not preserved. Now, with growing demand for algorithm verifiability and pedagogical value, the community has begun attempting to reverse-engineer and reconstruct an OCaml implementation from the Java code and paper descriptions.
This process involves not only syntax conversion but also understanding the original author's programming style and algorithm details, which makes it quite challenging.
Apache DataSketches is an open-source library of randomized streaming algorithms, mainly used to estimate statistics such as cardinality, quantiles, and frequencies over massive data with sublinear memory. Common algorithms include HyperLogLog, KLL, and Theta Sketch. The word "sketch" in the name refers to a "data sketch," i.e., a summary structure far smaller than the original data that can approximately answer queries. The library was originally developed by Yahoo and donated to the Apache Foundation, and the Java version is the most commonly used implementation in production. Kevin J. Lang is the core designer of these algorithms, and in papers and internal implementations he repeatedly emphasized properties such as streaming, single-pass, and mergeability. These properties remain consistent between Java and OCaml and are also key criteria for judging whether behavior is correct during reverse reconstruction.
Methods and Challenges of Reverse Engineering
The reverse-engineering team relies mainly on two sources: the Java source code of DataSketches and the papers and presentations published by Kevin J. Lang. By comparing the logical structure of the Java code, they infer the data structures and functional patterns likely used in OCaml. For example, loops in Java may correspond to recursion or higher-order functions in OCaml.
The challenge is that OCaml's strong type system and module system differ considerably from Java, so direct translation may lose the original intent. In addition, some optimization techniques (such as in-place updates) are difficult to reproduce in a purely functional style. The team adopts an incremental verification approach to ensure that the reconstructed OCaml code behaves consistently with the Java version.
The paradigm gap between OCaml and Java is the main difficulty in the reconstruction work. OCaml is a strongly typed, multi-paradigm language that by default encourages immutable data and recursion, but it also supports mutable records, arrays, and ref cells. Therefore, the claim that "purely functional code cannot reproduce in-place updates" is only true within a certain scope—the original author could very well have used mutable structures in OCaml for performance. The Java side, by contrast, relies heavily on explicit loops, boxed collections, and object state. During reconstruction, the team needs to distinguish which structures are essential to the algorithm (such as update rules and merge order) and which are merely Java implementation habits (such as iterators and exception-based control flow), then map them to OCaml module signatures, variant types, and tail recursion. Behavioral consistency is usually verified through randomized input comparison tests rather than line-by-line equivalent translation.
Why It Matters
This reverse-engineering effort is not only a restoration of history but also has practical value. OCaml code is usually more concise and easier to teach and formally verify. For scholars studying sketch algorithms, a readable OCaml implementation can lower the barrier to understanding. At the same time, it reminds developers to value code archiving and avoid similar losses.
According to community feedback, the reconstruction work has made initial progress, but full recovery will still take time. The relevant code has been shared on open-source platforms for peer review.
Formal verification and pedagogical readability are the two main expected benefits of the OCaml version. OCaml's algebraic data types and pattern matching can express the state transitions of sketch algorithms in a way very close to their mathematical definitions, making it easier to extract or verify them with tools such as Coq and Why3. At the same time, the module system can clearly separate the three types of operations—"update," "merge," and "estimate"—making it easier to turn pseudocode from papers into executable specifications. It should be noted, however, that even if reverse-reconstructed code passes behavioral tests, it is not equivalent to the original implementation; it is closer to a "readable reference implementation." For researchers, such an implementation is suitable for reproducing paper experiments and comparing error bounds across different sketches, but one should not directly assume that its performance is comparable to the production-grade Java version.
Lessons for Developers
Developers can draw several lessons from this incident: first, use version control systems to manage all experimental code; second, document the algorithm design process; third, consider the maintainability of multi-language implementations. Reverse engineering can remedy the situation, but the cost is high.
For readers interested in OCaml or sketch algorithms, follow the latest developments in the DataSketches community and contribute to or verify the work.
(This article is based on Hacker News discussion and public materials; no screenshots are included.)
One-line Summary
The community is reverse-engineering the lost OCaml prototype code of DataSketches from the Java implementation.
Paragraph Summary
Apache DataSketches' core algorithms, contributed by Kevin J. Lang, were originally prototyped in OCaml and later ported to Java; the original OCaml code was lost due to the lack of version control. The community has recently attempted to reverse-reconstruct an OCaml implementation from the Java source and paper descriptions. The main challenges lie in the differences between the two languages in type systems, module systems, and functional versus imperative paradigms, as well as reproducing optimizations such as in-place updates. The reconstruction work uses incremental verification to ensure behavioral consistency. The recovered OCaml code can aid algorithm teaching and formal verification, while also reminding developers to value code archiving and the maintainability of multi-language implementations.
Related articles

ChatGPT Plus Subscription Pitfall Guide: Go/Plus/Pro Tier Comparison and Quick Activation
A complete ChatGPT Plus subscription guide based on a Bilibili tutorial: from registering an account, comparing Go/Plus/Pro tiers, to the practical steps for quickly completing your subscription.

GPT-6.2 Sol/Astra Released! A Step-by-Step ChatGPT Plus Subscription Guide and Tier Comparison
GPT-6.2 Sol/Astra is out—here's a ChatGPT Plus subscription guide: register an account, compare Go/Plus/Pro tiers, and subscribe in a minute.

Comparing Three Channels for Subscribing to GPT Plus in China: Is Website Top-Up the Easiest?
How to subscribe to GPT Plus in China? This article compares website top-up, credit card, and mobile top-up channels—process, cost, and risk—with steps and tips to help you choose wisely.