In Part 6, we proved that the length of my_app joining two lists always equals the sum of their two lengths. Now we split that function and theorem across two source files, build them together, and extract my_app into standalone OCaml and Haskell source files.
1. Organizing a Multi-File Project with _CoqProject
Start by creating a clean project folder! In a terminal where rocq and GNU Make are available, run the commands below and save each following file at the indicated path.
mkdir rocq-list-demo
cd rocq-list-demo
mkdir srcCreate a _CoqProject file in the project root. The flag -Q src MyProject maps the physical src folder to the logical module prefix MyProject, and the next two lines list the source files to build.
-Q src MyProject
src/ListBasics.v
src/VerifiedList.vIn the first file, src/ListBasics.v, we place the list length function from Part 6. We will use this function when stating the length theorem for concatenated lists.
From Stdlib Require Import List.
Import ListNotations.
Fixpoint my_length {A : Type} (xs : list A) : nat :=
match xs with
| [] => 0
| _ :: rest => S (my_length rest)
end.In the second file, src/VerifiedList.v, we import ListBasics and define the list concatenation function along with its length conservation theorem. In From MyProject Require Import ListBasics., MyProject is the logical module path we configured earlier and ListBasics is the file name inside it, letting us use my_length in our theorem without redefining it.
From Stdlib Require Import List.
From MyProject Require Import ListBasics.
Import ListNotations.
Fixpoint my_app {A : Type} (xs ys : list A) : list A :=
match xs with
| [] => ys
| x :: rest => x :: my_app rest ys
end.
Theorem my_app_length : forall (A : Type) (xs ys : list A),
my_length (my_app xs ys) = my_length xs + my_length ys.
Proof.
intros A xs ys.
induction xs as [| x rest IH].
- simpl. reflexivity.
- simpl. rewrite -> IH. reflexivity.
Qed.Once all three files are saved, generate a Makefile in the folder containing _CoqProject and run make to compile both source files into .vo object files.1
GNU Make runs build commands in dependency order. rocq makefile writes the Makefile containing those commands; make then reads it and builds ListBasics.v first. That way its compiled module is ready when Rocq checks VerifiedList.v.
On the build side, read the arrows below as module dependency order rather than logical implication: each step on the right imports the compiled artifact produced on its left.
The first two artifacts are built by make. In the next section, Extract.v will use that same logical module path so it can load VerifiedList.vo.2
rocq makefile -f _CoqProject -o Makefile
make2. Extracting OCaml and Haskell from the Same Verified Function
Create Extract.v in the project root. The first line, From Corelib Require Extraction., loads Rocq’s code extraction plugin. After running Check my_app_length. to confirm that our compiled theorem is visible, we select the target language with Extraction Language and pass the output file name and my_app to Extraction. The generated files contain only the list type and the computational function; the length proof living in Prop is erased.
Using the notation from Part 6, the object we extract is the concatenation function , while the length theorem is the property we proved about it for any element type :
Only the function in the first equation and the list constructors it uses are emitted into the target source file. The proof in the second equation has already been verified by Rocq, and because that proof term is not a computational input to my_app, it is omitted from the extracted output. That is why we pass the theorem name to Check and the function name my_app to Extraction.34
From Corelib Require Extraction.
From MyProject Require Import VerifiedList.
Check my_app_length.
Set Extraction Output Directory ".".
Extraction Language OCaml.
Extraction "verified_list.ml" my_app.
Extraction Language Haskell.
Extraction "VerifiedList.hs" my_app.Run the command below in your project root, passing -Q src MyProject so Extract.v resolves VerifiedList along the same logical module path as in Section 1:
rocq compile -Q src MyProject Extract.vFirst, Check my_app_length. prints my_app_length : forall (A : Type) (xs ys : list A), ListBasics.my_length (my_app xs ys) = ListBasics.my_length xs + ListBasics.my_length ys. Rocq then writes the OCaml source verified_list.ml, its interface verified_list.mli, and the Haskell source VerifiedList.hs in the current directory. In the theorem output, ListBasics is the module that defines my_length.
Extraction produces source code in another language. For OCaml, the .ml holds the implementation and the .mli declares the names and types other modules can use. To build a runnable program, add code to call the extracted function and use a compiler for the target language. Part 8 does that with a driver.ml file.
OCaml:
(* Extracted OCaml code *)
type 'a list =
| Nil
| Cons of 'a * 'a list
(** val my_app : 'a1 list -> 'a1 list -> 'a1 list **)
let rec my_app xs ys =
match xs with
| Nil -> ys
| Cons (x, rest) -> Cons (x, (my_app rest ys))Haskell:
module VerifiedList where
import qualified Prelude
data List a =
Nil
| Cons a (List a)
my_app :: (List a1) -> (List a1) -> List a1
my_app xs ys =
case xs of {
Nil -> ys;
Cons x rest -> Cons x (my_app rest ys)}Looking at the extracted code in both languages, Rocq’s list A has been translated cleanly to type 'a list = Nil | Cons of 'a * 'a list with a polymorphic type variable 'a in OCaml and to data List a = Nil | Cons a (List a) in Haskell, while Fixpoint my_app became a recursive function using let rec my_app and case xs of ... respectively.
At the same time, the length theorem my_app_length and its proof inside Prop perform no runtime computation, so the extractor stripped them away completely! If you want to preview the extracted code in your editor before writing files to disk, select the target language and run Recursive Extraction my_app..
3. Qed vs. Defined: When to Keep a Proof Body Opaque and When to Leave It Transparent
Earlier we closed our length theorem with Qed.. Depending on whether a tactic block builds a logical proof or a computational value, you can choose how its body is sealed:
Qed.: seals the proof and marks its body opaque.Computewill not unfold an opaque body.Defined.: leaves the body transparent. When a computational value is constructed via tactics and closed withDefined.,Computecan unfold and evaluate its body.
To see the difference during computation, try Definition two : nat. Proof. exact 2. Defined. in a separate file, followed by Compute two.. The body closed with Defined. unfolds and you get 2. Close the same declaration with Qed. and the result still contains the name two.
Extraction operates separately from Compute: under its default settings, the extractor can still inspect opaque bodies when needed to produce code. In our exercise, the extraction target is my_app, which was defined directly with Fixpoint.5
Opacity controls whether Rocq unfolds a defined name to its body during reduction. In contrast, our length proof is omitted from the extracted output because its type lives in Prop and my_app takes no computational dependency on that proof; closing the same theorem with Defined would leave the extracted code of my_app unchanged.6
Even in much larger verification projects, work begins by stating the exact property to preserve. For example, CompCert’s semantic preservation theorem proves that translating C source programs into assembly preserves the observable behavior of the program.7
Footnotes
- The Rocq Prover Reference Manual 9.2. Building Rocq Projects. Summarizes writing
_CoqProjectand generating a Makefile withrocq makefile. ↩ - The Rocq Prover Reference Manual 9.2. Building Rocq Projects: Logical Paths and the Load Path · Building Rocq Projects: Computing Module Dependencies. Explains mapping physical directories to logical module paths with
-Qand computing compilation order. ↩ - The Rocq Prover Reference Manual 9.2. Program Extraction: Generating ML Code · Program Extraction: Setting the Target Language. Explains selecting a target language and extracting named definitions to source files. ↩
- Pierre Letouzey, CiE 2008. Extraction in Coq: an Overview. Introduces the design of program extraction that separates non-computational proofs in
Propfrom computational terms. ↩ - The Rocq Prover Reference Manual 9.2. Program extraction. Explains OCaml and Haskell extraction and the default setting for accessing opaque definitions. ↩
- The Rocq Prover Reference Manual 9.2. Program Extraction: Accessing Opaque Proofs · Proof Mode: Entering and Exiting Proof Mode. Explains how
Qedopacity affects ordinary reduction versus extraction. ↩ - Xavier Leroy. The CompCert C Verified Compiler: Documentation and User’s Manual · Formal verification of a realistic compiler, Communications of the ACM 52(7), 2009. Covers the semantic preservation theorem and OCaml extraction architecture of the Rocq-verified C compiler. ↩