A GitHub pull request suggests adding a C++ backend to OCaml's compiler, enabling translation of functional code into C++ template metaprograms, though with significant performance and usability challenges.
The OCaml language ecosystem may be expanding in an unexpected direction, with a pull request proposing a C++ backend for the compiler. This initiative, submitted by contributor stedolan, aims to translate OCaml code into idiomatic C++ through template metaprogramming techniques.
The pull request (#14701) introduces a new C++ backend to ocamlc, positioning it as an improvement over the current unincremented C backend used by the runtime and Foreign Function Interface (FFI). The implementation allows developers to compile OCaml programs to C++ using the ocamlc -incr-c command line option.
"This patch adds a new C++ backend to ocamlc, improving on the unincremented C currently in use by the runtime and FFI," stedolan explains in the pull request.
The approach works by transforming OCaml code into C++ template metaprograms. For example, a simple prime number computation program can be compiled to generate C++ code that uses template recursion to implement functional algorithms.

However, the implementation comes with significant limitations. The C++ backend treats C++ as a purely functional language with no mutable state, which means the OCaml standard library cannot be used directly since it relies on mutation. Developers would need to reimplement standard library functions in a purely functional style.
"C++ is a purely functional language, with no support for mutable state. Unfortunately, this means that the OCaml standard library is unavailable, as it contains a number of uses of mutation," the pull request notes.
Running the generated C++ code requires a C++ interpreter like g++, with arguments passed using the -D option. The output format is unusual because C++ interpreters format results as compiler error messages, displaying nested Cons<hd, tl> cells instead of OCaml's more familiar :: syntax.
Performance presents another challenge. The implementation struggles with larger computations, with support for larger programs disabled by default. Enabling it requires specifying -ftemplate-depth=999999, and even then, performance can be problematic.
"C++ can struggle somewhat on larger or longer-running computations," the pull request states. "On my machine, this prints the prime numbers up to 10000 in about half a minute, consuming approximately 11 GiB of memory."
Performance varies significantly between C++ implementations. While g++ can complete the computation with substantial memory usage, clang++ may be more memory-efficient but prone to segmentation faults.
The pull request suggests that algorithmic improvements can help mitigate some performance issues. By implementing more sophisticated data structures like Okasaki's leftist heap, the example program's performance improved to 8 seconds with 3.1 GiB of memory usage for computing primes up to 10,000.
Looking ahead, the pull request hints at potential expansion to other languages. "The approach here could be widened to support other languages. In particular, as soon as Rust finishes shipping support for partial impl specialization, then it too should become capable of running OCaml programs," stedolan suggests.
The community reaction to the proposal has been mixed but generally positive. Contributor redianthus expressed enthusiasm while asking about support for non-uniform recursive datatypes. Other community members engaged with the technical aspects, with dra27 appreciating the attention to detail in copyright headers and avsm joking about preferring C# or C-- instead.
This proposal, while technically intriguing, raises questions about practical utility. The performance characteristics, unusual output format, and limitations imposed by treating C++ as a purely functional language present significant barriers to adoption. Nevertheless, the creativity involved in bridging functional programming with template metaprogramming showcases the innovative thinking within the OCaml community.
For developers interested in exploring this unconventional approach, the pull request provides a starting point. The GitHub pull request contains example code and further details about the implementation. However, given the performance and usability challenges, it seems unlikely that this C++ backend would replace the existing C backend for production use.
The OCaml language continues to evolve through such experimental proposals, demonstrating the community's willingness to explore unconventional approaches to language implementation and compilation techniques.

Comments
Please log in or register to join the discussion