rocq_on_beam - Rocq extraction to Erlang

I’d like to announce rocq_on_beam, an ongoing project that allows running Rocq programs on BEAM.

What is Rocq

Rocq (formerly known as Coq) is a venerable proof assistant and a dependently-typed functional programming language. (You’ve probably heard about the much-hyped Lean4 prover by now — roughly speaking, it’s a clone of Rocq with a larger advertisement budget.) Rocq was used to prove the Four Color theorem, Feit-Thompson theorem, to implement a formally-verified CompCert C compiler, and much more.

Please note that rocq_on_beam is not a re-implementation of Rocq on top of Erlang — it’s a Rocq extraction plugin, i.e. a simple transpiler. As such, its code base is relatively small (<1kLOC), and it doesn’t have to reinvent a trusted type-checker kernel or any of the language features. Type inference, typeclasses, advanced module system, termination checking, etc., etc. come for free.

Example

As a motivational example, here’s a simple LIFO gen_server implemented in Rocq. It implements a server storing a list of terms, together with the cached buffer size.

  1. Thanks to the dependent typing, the callbacks and wrapper calls are end-to-end type-checked and type-infered. This is achieved by declaring a gen_server “signature” type, which is passed around as an implicit argument, and erased during extraction.
  2. Rocq has “module types” — they nicely translate to Erlang behaviors.
  3. One can prove invariants (or anything, really) of the gen_server. Here I prove an invariant that cached length is always equal to the actual buffer size.

It runs:

1> {ok, S} = gen_server:start(gen_server_test, [1], []).
{ok,<0.91.0>}
2> gen_server_test:pop(S).
{value,1}
3> gen_server_test:pop(S).
undefined
4> gen_server_test:push(S, 1).
ok
5> gen_server_test:pop(S).
{value,1}

Is it ready yet?

Not quite. I still need to

  1. Lower Peano (nat) and binary (Z) integers into the native integer type.
  2. Lower boolean/bitwise operators to orelse, andalso and friends (optimization).
  3. Lower programs doing I/O (modeled by SLOT), into regular code.

The good news is that these translations are mostly mechanical.

1 Like