OCaml is *awesome*

I have a full article and repository at https://gitlab.com/diskuv/samples/merrychristmas2023#dksdk-merry-christmas-2023.

Short version … huge thanks to Oleg for his preprint paper Generating C: Heterogeneous metaprogramming system description. I’ve adapted it so it mostly works with PPX and without BER MetaOCaml (although I intend to switch to BER MetaOCaml at some point).

Typing real OCaml on your keyboard:

$ git clone https://gitlab.com/diskuv/samples/merrychristmas2023.git
$ cd merrychristmas2023
$ opam switch create . 4.14.1 # or on Windows: dkml init
$ opam install . utop --with-test --yes
$ opam exec -- dune utop
#require "metaquot.ppx";;
open DkSDKMetatype_Offshore;;
open Xmas2023;;
let sum_ar_staged =
  ( Lexing.dummy_pos,
      fun arr n ->
        let sum = ref 0 in
        for i = 0 to 3 do
          for j = i to min (i + 3) (n - 1) do
            sum := !sum + arr.(j)
    Lexing.dummy_pos ) ;;

print_c "sum_ar" (module SumArConv) sum_ar_staged ;;

prints real C code:

int DkSDKMetatype_Offshore_sum_ar(int * const arr,int const n){
  int sum = 0;
  for (int i = 0; i < (1 + 3); i += 1)
    for (int j = i; j < (1 + min(i + 3,n - 1)); j += 1)
    sum = sum + (arr[j]);
  return sum;

Other languages are easy to add.

Merry Christmas! Jonah


FYI: Post on Hacker News: https://news.ycombinator.com/item?id=38748249

You wrote an OCaml to C translator?

I can’t take credit for that! Oleg wrote it; the main bit of code is on the page https://okmij.org/ftp/meta-programming/tutorial/genc.html and other bits are spread out over that site. And it would be much more accurate to say it is a translator of a subset of OCaml to C. And the target language is not limited to C.

I ported it so it runs using ordinary OCaml infrastructure (PPX and a type inference pass from compiler-libs). And I’m extending it for my users beyond its original optimization/vectorization focus (the original “subset” is mostly imperative operations on arrays) to allow data reshaping and transformations (ie. the extended “subset” will now include operations on product types). That description is abstract so a concrete example would be generating C code for ECS “systems” like Move if you are familiar with ECS and other data-oriented frameworks.


I’m not invested in MetaOCaml anymore, so this bears no weight – I still think it would be worthwhile to convince BER MetaOCaml users to abandon the original syntax in favor of a PPX syntax, and perhaps even standardize the various metacomputation systems which have overlapping functionality on a single syntax.

Could you describe which verctorization related oriented programs people usually desire to use in OCaml? It could be an interesting/representative set of benchmarks to experiment with intrinsics/vectorization.

The use case here is about OCaml as a “meta” language (the “M” in ML) rather than OCaml as a primary language. On the site you’ll find approaches to generating optimal BLAS kernels and low-memory stream transformations. (nit: The stream transformations (https://strymonas.github.io/) can be used if you are using OCaml as the primary language, but I’m more interested in how strymonas auto-generates C and Java code.)

Which other metacomputation systems? Do you mean MacoCaml … if so, I believe the groups talk to each other (but I’m not going to speak for them) even if the goals are sufficiently different.

Basically, I meant MacoCaml and ppx_stage. But was thinking maybe there’s more.