Date: 2025-11-04 Status: Complete Project: liblevenshtein-rust
This directory contains a comprehensive theoretical analysis of the multi-layer error correction pipeline described in grammar-correction.md.
File: complete-analysis.md (52 KB)
Audience: Researchers, advanced developers, theoretical computer scientists
Contents:
Key Sections:
Read if: You want rigorous proofs and formal analysis.
File: quick-reference.md (11 KB)
Audience: Developers, implementers, project leads
Contents:
Key Sections:
Read if: You need quick answers or implementation guidance.
File: visual-guide.md (33 KB)
Audience: Visual learners, new developers, educators
Contents:
Key Diagrams:
Read if: You prefer visual explanations or want intuition.
File: ../../../research/grammar-correction/analysis-log.md (23 KB)
Audience: Researchers, scientific reviewers, future maintainers
Contents:
Key Sections:
Read if: You want to understand the scientific process or reproduce the analysis.
File: ../../../guides/grammar-correction/implementing-guarantees.md (33 KB)
Audience: Implementers, software engineers, contributors
Contents:
Key Sections:
Read if: You're implementing the pipeline or writing code.
| Layer | Determinism | Correctness | Optimality | Complexity | Decidability |
|---|---|---|---|---|---|
| 1. Lexical | ✓ (with tie-breaking) | ✓ (within distance d) | ✓ (top-k by distance) | $\mathcal{O}(n \times d)$ | ✓ Yes |
| 2. Grammar | ~ (requires unambiguous grammar) | ✓ (syntactically valid) | ~ (BFS: uniform cost only) | $\mathcal{O}(k \times d \times n \times p)$ beam | ✓ Yes |
| 3. Semantic Validation | ✓ (with deterministic fresh vars) | ✓ (only well-typed pass) | ✓ (perfect filter) | $\mathcal{O}(n \log n)$ avg | ✓ Yes |
| 4. Semantic Repair | ~ (requires deterministic solver) | ✓ (syntactic) ~ (semantic) | ✗ (undecidable) | NP-hard (SMT) | ~ Conditional |
| 5. Process Verification | ✓ | ✓ (session type safety) | N/A (verification) | $\mathcal{O}(n)$ to $\mathcal{O}(n^k)$ | ✓ Yes (restricted) |
| Composition | ~ (depends on all layers) | ✓ (syntactic) | ✗ (greedy suboptimal) | Sum of layers | ✓ Yes (restricted) |
Legend: ✓ = Always holds, ~ = Conditional, ✗ = Does not hold
Determinism is achievable but requires engineering effort (tie-breaking, fixed seeds, no online feedback).
Correctness is guaranteed syntactically at each layer. Semantic correctness (preserving programmer intent) is harder.
Optimality does not compose: Layer-wise optimal $\ne$ globally optimal. Use approximations (beam search, Pareto).
Joint optimization is intractable: Exponential search space. Practical systems need heuristics.
Most layers are decidable and efficient: Lexical, grammar, semantic validation, session types (restricted). Exception: SMT repair is NP-hard.
Practical performance is acceptable: 450ms for 100 tokens (within 500ms IDE target).
Fundamental trade-offs exist:
Theorem (Lexical Determinism): Levenshtein automaton construction is deterministic; query is deterministic with lexicographic tie-breaking.
Theorem (BFS Optimality): BFS finds minimum-distance parse tree for uniform edit costs only.
Theorem (Beam Search Non-Optimality): Beam search does not guarantee optimality even for uniform costs.
Theorem (Non-Compositional Optimality): Optimal layers do not compose to optimal pipeline. Sequential composition is globally suboptimal.
Theorem (Type Safety): Well-typed programs satisfy Progress and Preservation (don't go wrong).
Theorem (Session Type Safety): Well-typed processes with dual session types are deadlock-free and communication-safe.
Theorem (Repair Undecidability): Semantic optimal repair is undecidable (by reduction from Rice's Theorem).
Theorem (Joint Optimization Intractability): Joint optimization over all layers is intractable (exponential search space).
Input: "teh", distance=1, top-1
Dictionary: {tea, ten, the}
Without tie-breaking → non-deterministic
Fix: Lexicographic ordering
Grammar: Expr → Expr + Expr | Expr * Expr | number
Input: "1 + 2 3" (missing operator)
Two corrections (both cost 1): insert '+' or insert '*'
Fix: Unambiguous grammar or deterministic tie-breaking
Input: "prnt(x + y" (typo + missing ')')
Sequential: "prnt"→"print" (cost 1) + insert ')' (cost 1) = 2
Optimal: Keep "prnt" + insert ')' (cost 1) if "prnt" is user-defined
Buggy: withdraw(amount: String) with balance: Int
SMT repair: Change balance: String (type-correct but semantically wrong)
Fix: Add domain constraints ("balance must be Int")
\mathcal{O}(\log n)$ per edit)| Parameter | Default | Range | Effect |
|---|---|---|---|
lexical_max_distance | 2 | 1-3 | Higher = more candidates, slower |
lexical_top_k | 5 | 1-20 | Number of candidates per token |
grammar_beam_width | 20 | 5-100 | Higher = better quality, slower |
grammar_max_distance | 2 | 1-5 | Max syntax errors to fix |
type_inference_timeout | 1000ms | 100-5000ms | Timeout for complex types |
smt_repair_timeout | 2000ms | 500-10000ms | Timeout for SMT solver |
session_type_depth | 10 | 5-50 | Max session type nesting |
enable_feedback | true | bool | Enable learning from corrections |
deterministic_mode | false | bool | Sacrifice learning for reproducibility |
Approximation Ratio for Beam Search: Is there a beam width k that guarantees α-approximation?
Optimal Feedback Update: What feedback rule maximizes long-term correction quality?
Decidability Boundary: Exactly which session type systems are decidable for which process calculi?
Semantic Repair Ranking: How to rank type-correct repairs by semantic likelihood?
Error Localization: For cascading errors, how to identify root cause?
Incremental Multi-Layer Correction: Can we update corrections incrementally as user types?
quick-reference.md (11 KB, 15 min)visual-guide.md (33 KB, 30 min)implementing-guarantees.md (33 KB, 45 min)Time: ~90 minutes Outcome: Understand properties, see visuals, implement
complete-analysis.md (52 KB, 2-3 hours)analysis-log.md (23 KB, 1 hour)quick-reference.md (11 KB, 15 min review)Time: ~4 hours Outcome: Complete understanding, reproducible analysis
quick-reference.md (11 KB, 15 min)implementing-guarantees.md (33 KB, 45 min)complete-analysis.md (52 KB, as reference)Time: ~60 minutes + ongoing reference Outcome: Ready to implement with code examples
analysis-log.md (23 KB, 1 hour)complete-analysis.md (52 KB, 2-3 hours)Time: ~4 hours + paper reading Outcome: Reproducible analysis, identify research directions
| Document | Size | Reading Time | Lines | Sections |
|---|---|---|---|---|
| complete-analysis.md | 52 KB | 2-3 hours | 2200+ | 10 + 3 appendices |
| quick-reference.md | 11 KB | 15 min | 450+ | 14 |
| visual-guide.md | 33 KB | 30 min | 1100+ | 9 diagrams |
| analysis-log.md | 23 KB | 1 hour | 900+ | 13 |
| implementing-guarantees.md | 33 KB | 45 min | 1300+ | 7 |
| Total | 152 KB | 4-6 hours | 5950+ | 53 |
The full analysis references 18+ papers in:
See complete-analysis.md Section 11 for full references.
To extend this analysis:
complete-analysis.md Section 3-7This analysis is part of the liblevenshtein-rust project.
See project LICENSE for details.
For questions or discussions about this analysis:
Document Version: 1.0 Date: 2025-11-04 Status: Complete Maintained by: liblevenshtein-rust project
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 |