This directory contains a Coq formalization of the 5-layer grammar correction pipeline designed for the Rholang programming language. The verification establishes correctness, soundness, completeness, and optimality properties for the error correction system.
Status: Active grammar proof suite with checked core, layer, composition, and NFA modules. The current .v sources have no active Admitted, Axiom, Parameter, Conjecture, or Hypothesis proof escapes.
Related Design Document: docs/design/grammar-correction/MAIN_DESIGN.md
The verification is organized into three main categories:
theories/Core/)1. Types.v
Q rationals)2. Edit.v
levenshtein s1 s2 = levenshtein s2 s1levenshtein s1 s3 <= levenshtein s1 s2 + levenshtein s2 s3levenshtein s1 s2 = 0 <-> s1 = s23. Lattice.v
4. Program.v
apply_edits original edits = correctedtheories/Layers/)Layer 1: Layer1.v (Levenshtein Lattice)
layer1_produces_wf_lattice: Lattice is well-formedlayer1_completeness: All strings within distance are includedlayer1_soundness: All paths respect distance boundlayer1_optimality: Optimal paths exist\mathcal{O}(n^d \times \sigma ^d)$Layer 2: Layer2.v (Tree-sitter Parsing)
layer2_soundness: Parse trees are validparse_program_deterministic: Parsing is deterministicLayer 3: Layer3.v (Type Checking)
Layer 4: Layer4.v (Semantic Repair)
Layer 5: Layer5.v (Process Calculus)
theories/Composition/)1. Forward.v (Sequential Composition)
forward_composition_valid - composition preserves validity2. Backward.v (Feedback)
3. Pipeline.v (Pipeline Execution)
pipeline_always_produces_result - termination guarantee4. Correctness.v (End-to-End)
grammar_correction_correctness
all_corrections_sound - all corrections are valid transformationspipeline_terminates_always - pipeline always terminatesbest_correction_optimal - best correction minimizes edit distancepipeline_makes_progress - pipeline makes forward progressTheorem levenshtein_symmetric : forall s1 s2,
levenshtein s1 s2 = levenshtein s2 s1.
Theorem levenshtein_triangle : forall s1 s2 s3,
levenshtein s1 s3 <= levenshtein s1 s2 + levenshtein s2 s3.
Theorem levenshtein_zero_iff_eq : forall s1 s2,
levenshtein s1 s2 = 0 <-> s1 = s2.
Theorem optimal_edit_exists : forall s1 s2,
exists edits, optimal_edit_sequence s1 s2 edits.
Theorem lattice_has_path : forall lat,
wf_lattice lat ->
exists path, complete_path lat path = true.
Theorem best_path_achievable : forall lat,
wf_lattice lat ->
exists path,
complete_path lat path = true /\
path_score lat path == best_path_score lat.
Theorem top_k_paths_sorted : forall lat k,
let paths := top_k_paths lat k in
forall i j,
i < j < length paths ->
path_score lat (nth i paths []) >= path_score lat (nth j paths []).
Theorem layer1_completeness : forall config input output,
levenshtein input output <= config.(max_edit_distance) ->
exists path,
let lat := build_error_lattice config input in
complete_path lat path = true.
Theorem layer1_soundness : forall config input path,
let lat := build_error_lattice config input in
complete_path lat path = true ->
exists output edits,
apply_edits input edits = output /\
edit_distance edits <= config.(max_edit_distance).
Theorem layer1_optimality : forall config input output,
levenshtein input output <= config.(max_edit_distance) ->
let lat := build_error_lattice config input in
exists path edits,
complete_path lat path = true /\
apply_edits input edits = output /\
edit_distance edits = levenshtein input output.
Theorem pipeline_execution_valid : forall p pipe,
(forall layer, In layer pipe -> valid_layer_result p (layer p)) ->
valid_layer_result p (execute_pipeline p pipe).
Theorem pipeline_always_terminates : forall p pipe,
pipeline_terminates p pipe.
Theorem correction_correctness : forall p pipe goal,
let result := execute_pipeline p pipe in
match result.(layer_best_correction) with
| Some corr =>
correction_sound p corr /\
correction_complete goal p corr
| None => True
end.
Theorem grammar_correction_correctness :
forall input config1 config2 goal,
let layer1 := execute_layer1 config1 in
let layer2 := fun p r => execute_layer2 config2 p r in
let pipe := [layer1; fun p => layer2 p (layer1 p)] in
let result := execute_pipeline input pipe in
match result.(layer_best_correction) with
| Some corr =>
correction_sound input corr /\
correction_complete goal input corr
| None => True
end.
QArith (rational numbers)cd docs/verification/grammar
coq_makefile -f _CoqProject -o Makefile
make
This compiles all configured .v files and produces .vo object files. For agent or CI runs on memory-constrained machines, prefer a capped invocation:
systemd-run --user --scope -p MemoryMax=2G -p MemorySwapMax=0 \
make -C docs/verification/grammar -j1
Current Status: checked proof suite. Recent capped verification compiled the active grammar core, layer, composition, and NFA slices; targeted escape-hatch scans over docs/verification/grammar/theories/**/*.v found no active Admitted, Axiom, Parameter, Conjecture, or Hypothesis.
The current codebase provides:
Levenshtein Symmetry:
length s1 + length s2Triangle Inequality:
s1 -> s2 -> s3Zero Iff Equal:
Path Existence:
Best Path:
Completeness:
output within edit distance dSoundness:
Optimality:
Validity Preservation:
Forall to propagate propertyTermination:
The Rocq verification models the architectural contracts used by the Rust implementation and by the WFST/lattice design work:
| Coq Module | Rust Module |
|---|---|
Core/Types.v | correction/lattice data model used by the design docs |
Core/Edit.v | edit operations and distance contracts |
Core/Lattice.v | lattice-path scoring and expansion contracts |
Layers/Layer1.v | Levenshtein lattice layer contract |
Layers/Layer2.v | parser-validity layer contract |
Layers/Layer3.v | type-annotation preservation contract |
Composition/Pipeline.v | pipeline execution and result contract |
NFA/*.v | phonetic/grammar NFA soundness and path-extraction contracts |
The verification focuses on algorithmic correctness, not Rust-specific concerns (memory safety, concurrency). Those are handled by Rust's type system.
.v Files: 26Core/*.vLayers/*.vComposition/*.vNFA/*.vBefore changing this proof suite:
systemd-run --user --scope with MemoryMax and MemorySwapMax=0.rg -n "\\bAdmitted\\b|\\badmit\\b|\\bAxiom\\b|\\bParameter\\b|\\bConjecture\\b|\\bHypothesis\\b" docs/verification/grammar/theories -g '*.v'..vo, .glob, .vok, .vos, .aux, .lia.cache, and makefile dependency artifacts unless they are intentionally tracked.docs/design/grammar-correction/MAIN_DESIGN.mddocs/design/grammar-correction/README.mddocs/verification/phonetic/Can you improve this documentation?Edit on GitHub
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |