Liking cljdoc? Tell your friends :D

OCaml Extraction Status

Date: 2025-11-18 Status: Extraction infrastructure created, compilation challenges identified

Summary

The Rocq/Coq formal verification is 100% complete with all 5 theorems proven. OCaml extraction infrastructure has been set up, but full compilation of the extracted code has technical challenges related to Coq's module system.

What Works ✅

  1. All proofs are complete: Zero Admitted statements in all 5 theorems
  2. Extraction directives created: phonetic/extraction.v with proper configuration
  3. Makefile target added: make extract successfully runs extraction
  4. Extraction generates output: Coq successfully extracts OCaml code

Technical Challenges 🔧

Module System Issues

Coq's extraction treats inductive types Phone and Context as if they should be modules (Phone.Vowel, Context.Initial), but when using Separate Extraction, these modules are not generated as standalone files. Instead, the extracted code references Phone.t and Context.t without defining these modules.

Example of the issue (from generated rewrite_rules.ml):

type coq_RewriteRule = { rule_id : nat; rule_name : string;
                         pattern : Phone.t list;  (* Phone module not defined! *)
                         replacement : Phone.t list;
                         context : Context.t; (* Context module not defined! *)
                         weight : coq_Q }

Attempted Solutions

  1. Custom extraction directives - Tried manual type mapping, but Coq's extraction syntax is complex
  2. Separate Extraction - Generated multiple modules but Phone/Context missing
  3. Recursive Extraction - Outputs to stdout rather than files

Root Cause

The issue stems from how Coq handles inductive types defined in one module and referenced in records defined in another module. The extraction system assumes these will be modules but doesn't create them with Separate Extraction.

Potential Solutions

Option 1: Manual Post-Processing (Recommended)

Add Phone and Context definitions manually to the extracted code:

(* Add to rewrite_rules.ml *)
module Phone = struct
  type t =
    | Vowel of ascii
    | Consonant of ascii
    | Digraph of ascii * ascii
    | Silent
end

module Context = struct
  type t =
    | Initial
    | Final
    | BeforeVowel of ascii list
    | AfterConsonant of ascii list
    | BeforeConsonant of ascii list
    | AfterVowel of ascii list
    | Anywhere
end

Option 2: Restructure Coq Code

Move Phone and Context definitions into their own modules in the Coq source, which may help extraction treat them properly.

Option 3: Skip OCaml Extraction

The OCaml extraction is primarily for reference. The proven Coq code can be directly translated to Rust manually, using the theorems as documentation of correctness.

Current Files

Extraction Configuration

  • phonetic/extraction.v - Extraction directives (68 lines)
  • Makefile - Updated with extract target

Generated Files (Last Attempt)

When using Separate Extraction, generated:

  • Core library modules: Ascii, BinNums, Bool, Datatypes, List, Nat, etc.
  • Our modules: rewrite_rules.ml/.mli, zompist_rules.ml/.mli
  • Issue: Missing Phone and Context module definitions

Recommendation

For this project: Proceed with direct Rust implementation using the Coq proofs as specification. The theorems guarantee correctness, and the Coq code is clear enough to translate manually.

Advantages of manual translation:

  1. Full control over Rust idioms and performance
  2. No dependency on OCaml extraction quirks
  3. Direct mapping from proven specifications
  4. Can add Rust-specific optimizations

The Coq proofs remain the source of truth: They mathematically guarantee the correctness of the algorithm, regardless of implementation language.

Next Steps

  1. ✅ Document extraction status (this file)
  2. ⏭️ Begin Rust implementation with references to theorems
  3. ⏭️ Write QuickCheck property tests that mirror the 5 theorems
  4. ⏭️ Validate Rust implementation against proven properties

Conclusion: The formal verification is complete and successful. The OCaml extraction has known technical challenges that don't diminish the value of the proofs. Moving forward with direct Rust implementation is the recommended path.

Can you improve this documentation?Edit on GitHub

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
Move to previous article
Move to next article
Ctrl+/Jump to the search field
× close