A new C++ back end for ocamlc

A new patch for ocamlc introduces a C++ backend that translates OCaml code into idiomatic C++ templates, effectively treating C++ as a purely functional language and using compilers like g++ as interpreters.
A New Step for the OCaml Compiler
The OCaml development community has just received a significant patch, adding a new C++ backend to the ocamlc compiler. This is considered a major improvement over the unincremented C currently used by the runtime and FFI.
This new backend allows for the direct translation of OCaml programs into idiomatic and readable C++ code. However, this approach comes with an interesting perspective: treating C++ as a purely functional programming language.
C++: A Purely Functional, Stateless Language
In the context of this new backend, C++ is operated as a purely functional language, with no support for mutable state. This leads to a limitation where the OCaml standard library is currently unavailable, as it contains many components based on mutation.
To illustrate, developers will need to reimplement modules (such as the List module) in a purely functional style to ensure compatibility when compiling to C++.
Using a C++ Compiler as an Interpreter
A unique aspect of this backend is how programs are executed. Instead of running an executable as usual, users utilize C++ compilers (like g++) acting as "interpreters."
By using options like -D to pass arguments into the main function, the program is executed during the compilation process itself. An interesting detail is that due to the historical development of C++, the program's output is displayed in the style of compiler error messages. This is a subtle homage to C++'s origins as an advanced preprocessor for C.
Furthermore, because C++ uses the :: operator for other purposes, OCaml's familiar infix :: syntax for list cons cells is replaced by explicitly nested Cons<hd, tl> structures in the output.
Performance and Resource Challenges
Leveraging the C++ template system for complex computations poses significant performance challenges. By default, larger programs are restricted, but users can expand this limit using the -ftemplate-depth=999999 option.
Real-world testing shows significant differences between compilers:
- g++: Can compute prime numbers up to 10,000 in about half a minute, consuming approximately 11 GiB of memory. With more optimized data structures like leftist heaps, the time drops to 8 seconds with 3.1 GiB of memory usage.
- clang++: Shows different performance, processing extremely fast but often ending with a warning or a segfault in similar scenarios.
Future Outlook
This approach opens the possibility of supporting more languages in the future. Developers expect that as soon as Rust finishes shipping support for "partial impl specialization," it too will become capable of running OCaml programs in a similar fashion to the current C++ backend.
Source: Hacker News













