HeadlinesBriefing favicon HeadlinesBriefing.com

OCaml Adopts C++ Backend, But Output Is Bizarre

Hacker News •
×

A pull request from stedolan introduces a C++ backend for ocamlc, replacing the decades-old C backend used by OCaml's runtime and foreign function interface. The change generates idiomatic C++ instead of C, aiming for better modern toolchain integration. This shifts OCaml's compilation strategy after relying on C for years.

The patch's example translates a prime sieve program. The generated C++ code outputs via compiler error messages—a historical quirk—and uses nested template structs like Cons instead of OCaml's :: operator. Since OCaml's standard library uses mutation, the example reimplements List purely functionally, highlighting the backend's constraints.

Running the code demands extreme template depths, using about 11 GiB of memory for 10,000 primes. Clang++ crashes faster. The author stresses the core issue is the algorithmic problem: the naive sieve is inefficient regardless of backend. A superior functional version exists using priority queues. Thus, while the new backend enables novel compilation, algorithmic choice remains paramount.