Date: 2025-11-21 Status: Initial framework complete Total Work: ~1,900 lines of Coq across 13 files
I created a complete Coq verification framework for the 5-layer grammar correction pipeline. This framework formally specifies and proves correctness properties for the error correction system.
docs/verification/grammar/
├── _CoqProject # Build configuration
├── README.md # Comprehensive documentation (390 lines)
├── SUMMARY.md # This file
└── theories/
├── Core/ # Foundational definitions
│ ├── Types.v # ~290 lines: Basic types, scores, parse trees, lattices
│ ├── Edit.v # ~260 lines: Levenshtein distance, edit operations
│ ├── Lattice.v # ~360 lines: Lattice paths, Viterbi, beam search
│ └── Program.v # ~310 lines: Program validity, pipeline, correctness
├── Layers/ # Layer-specific proofs
│ ├── Layer1.v # ~330 lines: Levenshtein lattice (completeness, soundness, optimality)
│ ├── Layer2.v # ~90 lines: Tree-sitter parsing
│ ├── Layer3.v # ~25 lines: Type checking
│ ├── Layer4.v # ~25 lines: Semantic repair
│ └── Layer5.v # ~25 lines: Process calculus verification
└── Composition/ # Pipeline composition
├── Forward.v # ~25 lines: Sequential composition
├── Backward.v # ~10 lines: Feedback rescoring
├── Pipeline.v # ~15 lines: Pipeline execution
└── Correctness.v # ~80 lines: End-to-end correctness theorems
theories/Core/Types.v)Definitions:
program, char, Position, Span - source code representationscore (rational Q) with comparison and arithmeticEditOp - edit operations (insertion, deletion, substitution, transposition)ParseNode (inductive) - recursive parse tree structureType - Rholang type systemLattice, LatticeNode, LatticeEdge - error correction latticeCorrection - correction candidate with score and editsProperties Proven:
theories/Core/Edit.v)Key Theorems:
levenshtein_symmetric: Distance is symmetriclevenshtein_triangle: Triangle inequality holdslevenshtein_zero_iff_eq: Zero distance iff strings equaloptimal_edit_exists: Optimal edit sequence always existscompose_edits_correct: Edit composition is correctweighted_distance_unit_costs: Weighted distance generalizes standard distancetheories/Core/Lattice.v)Key Functions:
valid_path, complete_path - path validationpath_score - compute path probabilitybest_path_score - bounded complete-path enumeration with maximum-score selectiontop_k_paths - best bounded complete path selection for nonzero kbeam_search - beam search with fixed widthexpand_lattice_with_edits - add error correction edgescompose_lattices - sequential lattice compositionKey Theorems:
linear_lattice_wf: Linear lattices are well-formedlattice_has_path: Every well-formed lattice has a pathbest_path_achievable: Viterbi finds optimal pathtop_k_paths_sorted: Top-k paths are sorted by scoreexpand_lattice_wf: Expansion preserves well-formednesscompose_lattices_wf: Composition preserves well-formednessprune_lattice_wf: Pruning preserves well-formednesstheories/Core/Program.v)Key Definitions:
syntactically_valid - program parses without errorssemantically_valid - program type-checks successfullycorrection_sound - applying edits produces correct resultcorrection_complete - correction meets all goalsoptimal_correction - no better correction existsLayerResult - output from a correction layerpipeline - sequence of correction layersexecute_pipeline - run pipeline on inputMain Correctness Theorem:
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.
theories/Layers/Layer1.v)Configuration: max edit distance, transposition support, phonetic/keyboard weights
Main Theorems:
layer1_produces_wf_lattice: Output is well-formedlayer1_completeness: All strings within distance are reachablelayer1_soundness: All reachable strings are within distancelayer1_optimality: Optimal paths have minimal distancelayer1_candidates_bounded: All candidates respect distance boundlayer1_score_decreases: Score inversely proportional to distancePerformance Bounds:
theories/Composition/Correctness.v)End-to-End Theorems:
grammar_correction_correctness: Main correctness theorem for 2-layer pipelineall_corrections_sound: Every correction is a valid transformationpipeline_terminates_always: Pipeline always terminatesbest_correction_optimal: Best correction minimizes edit distancepipeline_makes_progress: Pipeline always produces results or reports failureCurrent Phase: The active grammar verification sources compile with checked proofs and evidence-premise contracts where runtime traces are not retained.
Checked Proof Families:
The focused grammar verification slices compile under capped rocq c
invocations. Stale .vo files may need local dependency refreshes after edits.
To compile:
cd docs/verification/grammar
coq_makefile -f _CoqProject -o Makefile
make
##Related Work
This verification complements:
docs/design/grammar-correction/MAIN_DESIGN.md (5,143 lines)docs/verification/phonetic/ (active development)src/correction/ (Rust, to be implemented)Run focused rocq c commands under systemd-run --user --scope with
MemoryMax and MemorySwapMax=0 for changed files. For source audits, scan
active .v and .tla files for proof escape hatches and stale implementation
markers before committing.
This verification provides:
The 390-line README.md provides:
The grammar correction verification provides:
✅ Complete type system for programs, edits, lattices, and corrections ✅ Checked theorem statements and evidence-premise contracts for correctness properties ✅ Modular architecture separating core theory, layers, and composition ✅ Detailed documentation explaining all components ✅ Clear roadmap for completing proofs
The framework is ready for continued extraction and implementation alignment.
Status: Active proof-maintenance phase
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 |