A new C++ back end for ocamlc
151 points
7 hours ago
| 7 comments
| github.com
| HN
QuadmasterXLII
6 hours ago
[-]
Brilliant stuff. A tip for writing long-running C++: bizzarely, the C++ interpreter completely lacks tail call optimization. As a result, most idiomatic C++ code implements and uses reverse, map, range, filter etc, which don’t blow the stack if you implement them like (forgive the pseudo-code)

  (defun fibreverse (i ret acc)
    (if acc
        (if (> i 0)
            (progn
              (setv call1 (fibreverse (- i 1) (cons (head acc) ret) (tail acc)))
              (setv ret1 (head call1))
              (setv acc1 (head (tail call1)))
              (if acc1
                  (fibreverse (- i 2) (cons (head acc1) ret1) (tail acc1))
                  (pair ret1 acc1)))
            (pair ret acc))
        (pair ret acc)))

  (defun reverse (list) (head (fibreverse 30 nil list)))
Whoever has to maintain your code after you are gone will apprrciate that you used the idiomatic, portable approach instrad of relying on command line flags.
reply
anitil
1 hour ago
[-]
> Using these more sophisticated data structures, g++ is able to compute the prime numbers below 10000 in only 8 seconds, using a modest 3.1 GiB of memory.

Finally, I can get some primes on my laptop!

reply
Caum
2 hours ago
[-]
This is a really interesting direction for OCaml. A formal C++ backend could significantly simplify embedding OCaml into existing C++ codebases, especially where linking against the standard OCaml runtime might be tricky. I wonder how the performance compares to the existing native backend in long-running processes.
reply
fayash
34 minutes ago
[-]
Might have missed the joke here. This isn't a traditional C++ backend; it's a C++ Template Metaprogramming backend. The code isn't meant to be run—it’s meant to be compiled. The "output" you see is actually just a compiler error message because the program forces the compiler to calculate primes during type checking. The "runtime performance" the author mentioned is actually just how long g++ takes to crash your ram.
reply
kristjansson
1 hour ago
[-]
Per TFA C++ is a purely functional, interpreted language. Should be trivial to embed into?
reply
ajbt200128
42 minutes ago
[-]
Wow Stephen Dolan never fails to impress
reply
zorobo
5 hours ago
[-]
This made my day, thank you!
reply
dnmc
6 hours ago
[-]
Is this the Stephen Dolan of "mov is Turing Complete" fame?
reply
loeg
4 hours ago
[-]
I believe so.
reply
hudsonhs
5 hours ago
[-]
She (Jane Street) is not gonna notice you, bro
reply
shorsher
4 hours ago
[-]
I believe they already work for Jane Street.
reply
binarycrusader
3 hours ago
[-]
reply