The CertiRocq Verified Compiler
December 04, 2026
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 program
pis well-typed, - CertiRocq compiles
pto the C programpcwith the verified passes only,2 - the program uses no registered primitives and no inductive types remapped by
CertiRocq Extract Inductives,3 and - the garbage collector and the runtime’s pointer test meet their specifications, and the memory is set up before the call.
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
- The runtime. The garbage collector and the pointer test are written in C, and the theorem assumes that they meet their specifications. For the second theorem, the same holds for the functions that read and print characters. The garbage collector has its own correctness proof [12, 13], but the two proofs are not connected yet. Connecting them is not straightforward. Each proof describes the collector in its own terms, and one has to prove that what the collector’s proof guarantees is exactly what the compiler’s proof assumes. Specifications of separately verified components rarely line up out of the box.
- Quoting, printing, and running the compiler. These steps around the proved passes are not verified. Quoting the Rocq program into MetaRocq’s syntax tree and printing the generated Clight as a C file are both done by OCaml code. CertiRocq itself is a Rocq program that has to be compiled to run. Its standard plugin runs it as OCaml code produced by Rocq’s extraction, the same unverified route that CertiRocq replaces for your programs. CertiRocq can also compile itself through the bootstrapped plugin that runs the C code that CertiRocq generates for its own pipeline.
- Termination and separate compilation. The theorem is about whole programs that evaluate to a value. This is the usual case in Rocq, which only accepts functions that terminate, unless they use coinduction. The CertiRocq part of the pipeline, from λ□ to Clight, also preserves divergence and supports separately compiled programs, but the end-to-end theorem does not state these properties yet.
How CertiRocq works
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.
- 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.
- 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.
- 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.
- 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.
- 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 CertiRocq compiler compiling itself, the largest Rocq program we have. The OCaml numbers are for the extracted compiler, which is what CertiRocq’s standard plugin runs, and the CertiRocq numbers are for the bootstrapped plugin.
- VeriStar, a verified theorem prover for separation logic [16], on a hard problem.
- Graph coloring, a program that colors graphs, as register allocators do (written by Andrew Appel).
The setup is the following:
- CertiRocq is the pipeline described above, and + new passes adds the eight new verified λANF passes.
- The C code is compiled with clang -O2 or with CompCert 3.17. With CompCert, only the C code that CertiRocq generates and the garbage collector are compiled by CompCert.
- The extracted code is compiled with OCaml 4.14.2, without flambda (ocamlopt) and with flambda at -O3.
- In all cases, the young generation of the garbage collector has 256K words, OCaml’s default.
- The machine is an Apple M5 Max. All times are in milliseconds. The last column divides the time with the new passes by the time of flambda. Below 1 means that CertiRocq is faster.
| 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:
- Divergence and separate compilation for the whole pipeline. The CertiRocq passes, from λ□ to Clight, already preserve divergence (i.e., when the source program runs forever, the generated code also runs forever, unless the garbage collector aborts it). The optimizations and the C code generator also support separate compilation (i.e., two programs compiled separately and then linked behave like the two source programs linked). Extending this to the whole pipeline is work in progress.
- Connecting the garbage collector’s proof. The end-to-end theorem assumes that the garbage collector meets the specification that the compiler’s proof uses, while the collector’s own proof uses a different specification. We need to prove the collector’s specification implies the one that the compiler assumes.
- Better documentation and UX. Yes, we could use some :). Some of the docs (like the CertiRocq wiki) are out of date, but hopefully this is coming soon too! Stay tuned.
CertiRocq is open source, under the MIT license. The code, installation instructions, and examples are on GitHub.
Footnotes
-
The MetaRocq project provides a verified extraction to OCaml [6], but Rocq 9.3 still ships with the unverified one. ↩
-
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. ↩ -
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 asPrimInt63.add, and on big integers from the GMP library. The theorem does not cover these C functions yet. ↩ -
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
- Olivier Savary Bélanger and Andrew W. Appel. Shrink Fast Correctly! PPDP 2017.
- Zoe Paraskevopoulou and Andrew W. Appel. Closure Conversion Is Safe for Space. ICFP 2019.
- Zoe Paraskevopoulou, John M. Li, and Andrew W. Appel. Compositional Optimizations for CertiCoq. ICFP 2021.
- Zoe Paraskevopoulou and Anvay Grover. Compiling with Continuations, Correctly. OOPSLA 2021.
- Wolfgang Meier, Martin Jensen, Jean Pichon-Pharabod, and Bas Spitters. CertiCoq-Wasm: A Verified WebAssembly Backend for CertiCoq. CPP 2025.
- Yannick Forster, Matthieu Sozeau, and Nicolas Tabareau. Verified Extraction from Coq to OCaml. PLDI 2024.
- Xavier Leroy. Formal Verification of a Realistic Compiler. Communications of the ACM, 2009.
- GitHub Staff. Octoverse: Commits, pushes, reach new highs in 2026 as developers build with agents. GitHub Blog, October 2026.
- Sundar Pichai. Cloud Next ‘26: Momentum and innovation at Google scale. Google Blog, April 2026.
- Nicholas Carlini. Building a C compiler with a team of parallel Claudes. Anthropic Engineering Blog, February 2026.
- Keane Lucas et al. LLMs are making formal software verification feasible at a much larger scale. Anthropic, October 2026.
- Shengyi Wang, Qinxiang Cao, Anshuman Mohan, and Aquinas Hobor. Certifying Graph-Manipulating C Programs via Localizations within Data Structures. OOPSLA 2019.
- Shengyi Wang, Kathrin Stark, and Andrew W. Appel. Verification of a Generational Garbage Collector. arXiv:2609.13186, 2026.
- 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.
- 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.
- Gordon Stewart, Lennart Beringer, and Andrew W. Appel. Verified Heap Theorem Prover by Paramodulation. ICFP 2012.
- Zoe Paraskevopoulou. Machine-Generated, Machine-Checked Proofs for a Verified Compiler (Experience Report). ICFP 2026.