The CertiRocq Verified Compiler

CertiRocq CertiRocq is a compiler for programs written in the Rocq proof assistant.

It compiles Rocq programs to C, and it comes with a machine-checked proof that the C code computes the same results as the original program. As of December 2026, this proof covers every pass of the compiler, from the Rocq program to the C code. A second proof connects CertiRocq’s theorem with CompCert’s, providing a verified pipeline from Rocq all the way to assembly language.

The project started in 2014. I started working on it in 2015, when I started my PhD at Princeton. Since then, the team has changed many times, and so has the pipeline.

For many years, my collaborators and I produced manual proofs for different parts of CertiRocq [1, 2, 3, 4, 5]. The first end-to-end proof of its correctness was completed this week, with the help of LLM agents.

This post explains why Rocq needs a verified compiler, what the compiler’s correctness theorem says, how the compiler works, how fast the code it generates is, and how AI helped complete the proof.

Why a verified compiler?

Rocq lets you write a program and prove that it is correct. For example, you can write a sorting function and prove that it always returns a sorted permutation of its input. The Rocq kernel checks every step of the proof, so you do not need to trust whoever wrote it, whether a human or an AI agent.

But the proof is about the Rocq program, not about the code that runs on your computer. The usual way to run a Rocq program is through extraction. Rocq translates the program to OCaml (or Haskell, or Scheme), and a compiler for that language turns it into machine code. Neither step is verified. The extraction, the compiler of the target language, and its runtime all become part of the trusted computing base, i.e., the code you have to trust without a proof.

Rocq’s extraction alone is about 9,200 lines of OCaml, and the OCaml compiler is much larger. A bug in any of them can produce an executable that does not do what the proof says. For example, extraction turns a definition of an infinite stream into OCaml code that loops forever, although Rocq computes the first element of the stream (rocq#18827, reported in 2024 and still open). The OCaml compiler compiles a match on an integer so that it takes the wrong branch (ocaml#15443, reported on December 1, 2026).1

A verified compiler closes this gap. It comes with a machine-checked proof that the code it generates behaves like the source program. Combined with a proof about the source program, this gives a guarantee about the code that actually runs. CompCert [7], written in Rocq, does this for C, and CakeML, written in HOL4, does it for ML. CertiRocq does it for Rocq.

AI now writes much of the world’s software [8, 9], including compilers [10] and runtimes [11]. There is an emerging idea that AI should use proof assistants like Rocq, Lean, or Isabelle/HOL to verify the software it writes. The proof checker removes the need to trust the AI. A verified compiler removes the need to trust the tools between the verified program and the executable. What is left to trust is the statement of the theorem, the semantics of the target code, and the proof checker.

What does the end-to-end theorem say?

In plain words, the theorem guarantees that if a well-typed Rocq program evaluates to a value, then the C code that CertiRocq generates for it returns the same value, unless the garbage collector runs out of memory and aborts.

What does “the same value” mean? For programs that return plain data (i.e., first-order data, such as numbers, booleans, lists, or trees), the answer is simple: the C code computes, laid out in memory, exactly the tree of constructors that Rocq computes.

For other results, such as functions, the C code returns a closure, but the theorem does not say what the closure computes.

Here is the statement of the theorem, from theories/Codegen/pipeline_correctness_C.v:

Theorem CertiRocq_C_correct (o : Options)
    (p : Ast.Env.program) (* The AST of the typed Rocq term *)
    (pc : Clight.program) (* The compiled C program *)
    gc_inv tb tofs h m v
    (* [p] is well-typed *)
    (hwt : ∥ @TemplateProgram.wt_template_program config.extraction_checker_flags p ∥)
    (* The inductive mapping of [o] is well-formed *)
    (hmap : wf_template_inductives_mapping p.1 (Pipeline_utils.inductives_mapping o)) :
  (* CertiRocq, with the verified passes only, compiles [p] to [pc] *)
  verified_options o ->
  C_program o p hwt hmap = Some pc ->
  (* The collector and the pointer test of the runtime meet their specifications, and the
     thread info at [tb, tofs] is set up in the memory [m] *)
  (forall cenv, collector_ok cenv (globalenv pc) gc_inv (gc_aborted pc)) ->
  is_ptr_spec (globalenv pc) ->
  (forall cenv, tinfo_ok cenv (globalenv pc) gc_inv h [] m tb tofs) ->
  (* [p] evaluates to [v] *)
  TemplateProgram.eval_template_program p v ->
  (* [body] returns the layout of [v], or the collector aborts the program *)
  (exists w m', call_evaluates pc bodyId [Vptr tb (Ptrofs.repr tofs)] m w m' /\
                layout p.1 (globalenv pc) m' v w) \/
  collector_aborts_in pc bodyId [Vptr tb (Ptrofs.repr tofs)] m.

In words, the hypotheses require that:

The conclusion says that if p evaluates to v, then the C function body of the compiled program pc returns a value w that represents v in memory (defined by the layout relation), unless the garbage collector aborts the program.

Down to assembly code

A second theorem, in theories/Compiler/compile_io_correct.v, composes CertiRocq’s proof with CompCert’s. For a function f : A -> B on first-order data, CertiRocq generates a whole C program that reads a value a of type A from its standard input and prints the value of f a. If the input is the printed form of a well-typed value a, the assembly code that CompCert generates from this program prints exactly the value of f a and exits with code 0, or runs out of memory.

Assumptions and limitations

How CertiRocq works

The CertiRocq pipeline: a Rocq program is quoted to Template, then compiled through PCUIC, λ□, λANF, and flattened λANF to Clight. A C compiler turns the Clight code into machine code.
The CertiRocq pipeline.

CertiRocq compiles a program in several passes, each from one intermediate language to the next. Each pass has its own correctness proof, and the end-to-end theorem composes them.

  1. Quoting turns the program into a syntax tree that Rocq functions can manipulate (the Template representation). It is part of MetaRocq [14], a formalization of Rocq in Rocq. A translation then moves it to PCUIC, the version of Rocq’s type theory on which MetaRocq proves its results.
  2. Erasure (PCUIC → λ□) removes types and proofs, which cannot affect the result. MetaRocq proves erasure correct [15]. A few more passes then transform the erased program.
  3. ANF conversion (λ□ → λANF) puts the program in A-normal form. In this representation, every intermediate result gets a name and the order of evaluation becomes explicit. CertiRocq can also convert to continuation-passing style instead [4], but the theorem above covers the ANF route.
  4. Optimizations and flattening (λANF → flattened λANF) improve the λANF code and make it ready for translation to C. This stage comprises 14 distinct passes. These include uncurrying, inlining, lambda lifting, shrink reduction, closure conversion, hoisting, and dead-parameter elimination, among others. Our ICFP 2021 paper [3] describes the proof technique and the compositional framework. Last week, with the help of Claude, I added eight new passes that greatly improve performance, including passes that lift constant computations outside of function bodies, specialize function arguments, and avoid building a closure between the arguments of unknown curried functions. The output of this stage is flattened λANF. Closure conversion turns each function into code plus an explicit environment, so functions never refer to variables defined outside them. Hoisting then moves every function to the top level, removing nested functions. This is the shape of a C program, where all functions are defined at the top level.
  5. C code generation (flattened λANF → Clight) generates C code in which values live in a heap managed by a garbage collector.

The C code can then be compiled with CompCert, GCC, Clang, or really any C compiler. CertiRocq also has a WebAssembly backend, which this post does not cover.

Performance

How fast is the code that CertiRocq generates? I show some preliminary measurements, comparing CertiRocq with Rocq’s extraction to OCaml on three programs:

The setup is the following:

Program C compiler CertiRocq + new passes ocamlopt flambda ‑O3 ÷ flambda
CertiRocq compiling itself clang ‑O2 82,500 69,400 62,100 61,300 1.13
  CompCert 123,300 98,500 62,100 61,300 1.61
VeriStar, hard problem clang ‑O2 9.71 4.91 6.10 5.96 0.82
  CompCert 14.1 7.35 6.10 5.96 1.23
Graph coloring clang ‑O2 4.80 3.68 3.08 2.89 1.28
  CompCert 5.98 3.98 3.08 2.89 1.38

With the new passes, CertiRocq, when its output is compiled with clang -O2, is faster than OCaml with flambda on VeriStar (0.82 times the time of flambda). It is slower on CertiRocq compiling itself (1.13 times) and on graph coloring (1.28 times). The eight new λANF passes take 16% to 49% off CertiRocq’s times. Compiling the generated C code with CompCert instead of clang makes the times with the new passes 8% to 50% slower. Differences of a few percent are within the noise of the measurements.

The use of AI

For most of its history, CertiRocq was proved by hand, and my collaborators and I spent a significant amount of time on these proofs. In 2026, I used AI coding agents for three parts of the pipeline: the ANF conversion, new λANF optimizations, and the C code generator.

ANF conversion. In February 2026, Claude Code, running Opus 4.6, wrote a 6,000-line proof of the ANF conversion in about four days. I asked it to reuse the technique of our earlier proof of the CPS conversion [4]. I guided it, but I did not write any proof code. It still required four days and close guidance. I described this experience in my previous post and in an experience report published at ICFP 2026 [17].

New optimizations. In the experience report, I wrote, hesitantly, that adding verified optimizations on demand might become possible. I did not expect it to happen this fast. Last week, I asked Claude Code, running Fable 5.2, how to make the generated code allocate less memory by adding new λANF optimizations, and asked it to keep only the passes that improve some benchmark.

In one day, it wrote eight passes. Then, I asked Claude Code, running Opus 5.5, to prove them correct using the existing composition proof framework of the λANF pipeline. It proved five of them correct in about four hours and 7,700 lines. For each of the other three, it found a small program whose behavior the pass changes. These three passes were wrong as written.4 This is a great example of how formal verification can help safeguard AI-generated code. All three are now fixed and proved, and the proofs of the eight passes total about 16,700 lines.

Overall, only nine months after my first experiment with LLMs writing proofs, the process seems much faster and more autonomous.

C code generator. Until November 2026, CertiRocq used an older C code generator. Its correctness proof, about 10,800 lines, was never completed: when we deleted it, it still had 5 admitted lemmas and 20 unproved cases in other proofs. In November, I asked Claude Code, running Fable 5.2, for a new code generator with the same memory layout and garbage collector interface as the old one, but faster and cleaner. It wrote one in under three hours, while I reviewed design decisions. It also found a performance bug in the previous code generator that was preventing proper tail recursion elimination.

For the proof, I chose a logical relation between λANF and Clight values, and, with Claude’s help, wrote a short prompt for a second agent that stated the goal and the main design decisions. That agent, also running Fable 5.2, proved the generator correct in less than three and a half hours. Over the next days, I steered the agent to extend the proof to divergence and separate compilation, and composed it with the rest of the pipeline, again reviewing design decisions. I also asked it to change the statement, which, as stated, was vacuous. The proof had assumed that every call to the garbage collector returns with enough free memory. This is too strong, and no collector can promise it. CertiRocq’s collector aborts the program when it runs out of memory. Now the theorem says that the generated code returns the right value, unless the collector runs out of memory and aborts the program. The new generator is about 900 lines of Rocq, and its proof is about 8,700 lines, with no admitted lemmas. It completed the end-to-end theorem, which was merged on December 1.

Specifications. A compiler proof has two parts: deciding what each pass must guarantee, and proving that it does. Each guarantee must combine with those of the other passes into one end-to-end theorem, and its assumptions must not exclude programs of interest or be too strong to satisfy. The proof frameworks that we built over the years settle this. They fix what it means for a pass to be correct, and they prove that correct passes compose transitively (for subsequent passes of the compiler) and horizontally (for programs compiled separately). Each new optimization came with its specification ready, and the agent only had to prove it. For the C code generator, the agent wrote the specification, but its shape was informed by the previous ones, and some design choices were mine (e.g., to use a logical relation between function values instead of a syntactic one). Still, after the proof was done, the specification turned out to be too weak in practice and needed another iteration.

Writing proofs is no longer the hard part. What remains is deciding what to prove.

What is next?

The next steps are:

CertiRocq is open source, under the MIT license. The code, installation instructions, and examples are on GitHub.

Footnotes

  1. The MetaRocq project provides a verified extraction to OCaml [6], but Rocq 9.3 still ships with the unverified one. ↩

  2. The passes without a proof are the current conversion to continuation-passing style, which CertiRocq can use instead of the ANF conversion, MetaRocq’s typed erasure, which relies on unproved assumptions about the eta-expansion it performs, and the remapping of inductive types by CertiRocq Extract Inductives. ↩

  3. A registered primitive is a Rocq constant that CertiRocq replaces with a C function, given with the command CertiRocq Register. CertiRocq’s plugin registers the operations on Rocq’s primitive integers, floats, and strings, such as PrimInt63.add, and on big integers from the GMP library. The theorem does not cover these C functions yet. ↩

  4. A pass that builds a constant once, instead of at every function call, moved constants whose computation can fail, so a program could fail where it did not before. After the fix, it leaves such constants alone. A pass that delays a recursive call until its result is needed could make a program that runs forever return. It now delays only calls that cannot run forever. The pass that avoids building a closure between the two arguments of a call was redesigned to rewrite every program, so that programs compiled separately still link correctly. Correcting and proving such transformations still needed my supervision and design decisions, but at a much higher level than the ANF conversion proof required. ↩

References

  1. Olivier Savary Bélanger and Andrew W. Appel. Shrink Fast Correctly! PPDP 2017.
  2. Zoe Paraskevopoulou and Andrew W. Appel. Closure Conversion Is Safe for Space. ICFP 2019.
  3. Zoe Paraskevopoulou, John M. Li, and Andrew W. Appel. Compositional Optimizations for CertiCoq. ICFP 2021.
  4. Zoe Paraskevopoulou and Anvay Grover. Compiling with Continuations, Correctly. OOPSLA 2021.
  5. Wolfgang Meier, Martin Jensen, Jean Pichon-Pharabod, and Bas Spitters. CertiCoq-Wasm: A Verified WebAssembly Backend for CertiCoq. CPP 2025.
  6. Yannick Forster, Matthieu Sozeau, and Nicolas Tabareau. Verified Extraction from Coq to OCaml. PLDI 2024.
  7. Xavier Leroy. Formal Verification of a Realistic Compiler. Communications of the ACM, 2009.
  8. GitHub Staff. Octoverse: Commits, pushes, reach new highs in 2026 as developers build with agents. GitHub Blog, October 2026.
  9. Sundar Pichai. Cloud Next ‘26: Momentum and innovation at Google scale. Google Blog, April 2026.
  10. Nicholas Carlini. Building a C compiler with a team of parallel Claudes. Anthropic Engineering Blog, February 2026.
  11. Keane Lucas et al. LLMs are making formal software verification feasible at a much larger scale. Anthropic, October 2026.
  12. Shengyi Wang, Qinxiang Cao, Anshuman Mohan, and Aquinas Hobor. Certifying Graph-Manipulating C Programs via Localizations within Data Structures. OOPSLA 2019.
  13. Shengyi Wang, Kathrin Stark, and Andrew W. Appel. Verification of a Generational Garbage Collector. arXiv:2609.13186, 2026.
  14. Matthieu Sozeau, Abhishek Anand, Simon Boulier, Cyril Cohen, Yannick Forster, Fabian Kunze, Gregory Malecha, Nicolas Tabareau, and Théo Winterhalter. The MetaCoq Project. Journal of Automated Reasoning, 2020.
  15. Matthieu Sozeau, Simon Boulier, Yannick Forster, Nicolas Tabareau, and Théo Winterhalter. Coq Coq Correct! Verification of Type Checking and Erasure for Coq, in Coq. POPL 2020.
  16. Gordon Stewart, Lennart Beringer, and Andrew W. Appel. Verified Heap Theorem Prover by Paramodulation. ICFP 2012.
  17. Zoe Paraskevopoulou. Machine-Generated, Machine-Checked Proofs for a Verified Compiler (Experience Report). ICFP 2026.