What is the relationship between the ReScript compiler and OCaml?

My current understanding is that both ReScript source code and OCaml source code are compiled to OCaml byte code. I’m guessing that type safety is determined from the byte code, which is reported back to the ReScript and OCaml compilers.

The reason I’m asking is to gain a better understanding of the robustness of ReScript’s type safety.

Thanks

both ReScript source code and OCaml source code are compiled to OCaml byte code.

No. ReScript is a standalone compiler.

The compiler of ReScript is implemented using OCaml, similar to other languages like Grain, Haxe.

The rest is the implementation detail which should not be relevant to daily use.

The type system is largely inherited from OCaml (with lots of simplifications and extensions since we don’t need the OO stuff and we add stuff for JS specifics).

We appreciate the legacy we got from OCaml; we inherit a time tested type system as the starting point, ReScript will keep evolving as its own language

3 Likes

Thanks for clarifying @Hongbo

To clarify a bit:

The OCaml compiler first compiles to an internal representation IR, and then from the IR to a target. In fact, there are a couple of IRs before you start emitting for your target. The target is either OCaml bytecode, or machine code. Historically, most of the IRs are not typed, and thus types are erased fairly early on in the compiler. Roughly, once you have elaboration/inference done, you know the program type checks, so you can remove the types from the later compiler stages. More modern compiler construction tend to type IRs as well and make the passes type-preserving, because it can remove a large set of errors from optimizations, simply by running a type check on the IR parts.

The js_of_ocaml project uses the compiled bytecode and produces a javascript target from it. This is done because the bytecode is relatively stable, even under changes to the language. The flip side is that the produced output doesn’t really look like Javascript.

To my knowledge, ReScript’s compiler uses some of the OCaml stages, mixing those in with stages of it’s own. In particular, it produces a direct Javascript target, which is also quite readable by programmers.

The major insight is that once your IR is a lambda calculus variant, almost any language, imperative or functional, can be compiled to that IR. And symbolic manipulation of the lambda calculus is very strong, hence it becomes a natural IR for a programming language.

6 Likes

Hi, I am the primary author, please don’t make the story too complicated based on your assumption…

1 Like