TL;DR
ChopChop is the first programmable framework that allows developers to enforce complex semantic constraints—such as type safety and program equivalence—on language model (LM) outputs during generation. By modeling the space of possible program completions as infinite cyclic data structures (regular codata), ChopChop prunes invalid generation paths at the abstract syntax tree (AST) level, significantly boosting code correctness with practical computational overhead.
What Problem Does This Paper Address?
While Large Language Models (LLMs) are excellent at generating code, they offer no guarantees of correctness. They frequently produce code that looks plausible but violates critical semantic properties, such as referencing undefined variables, mutating constants, or failing type checks.
Existing solutions generally fall into two camps, both with major limitations:
- Syntactic Constraints: Tools can force an LM to adhere to a specific Context-Free Grammar (CFG) or JSON schema. However, many crucial properties (like type safety) cannot be expressed by context-free grammars alone.
- Ad-hoc Semantic Monitors: Some frameworks use external tools (like Language Server Protocols) to check code during generation. However, these operate directly on raw strings. This makes it incredibly difficult to reason about incomplete code fragments. For example, the partial string
1 + myStringlooks like a type error, but it could be cleanly completed into the valid program1 + myString.length.
ChopChop bridges this gap by providing an expressive, programmable framework that decouples syntactic grammar from structural semantic constraints.
Methods
ChopChop frames semantic constrained decoding as a realizability problem from program synthesis: given a partial token prefix generated by the LM, does there exist any valid completion that satisfies the user's semantic constraints?
The framework operates in four main steps:
- Incremental Lexing and Parsing: ChopChop takes the partial string prefix and uses parsing-with-derivatives to construct a symbolic representation of all possible ASTs that could complete the program.
- Regular Codata Representation: Because the space of potential completions is often infinite, ChopChop represents these program spaces as regular coterms—infinite, cyclic terms that can be represented finitely in memory as cyclic graphs.
- Semantic Pruners: Users write modular, corecursive functions called "pruners" in a Python-embedded DSL. These pruners analyze the program space and filter out branches that are guaranteed to violate semantic properties (e.g., removing ill-typed paths).
- Nonemptiness Check: ChopChop runs a fixpoint computation over the pruned graph. If the remaining space is non-empty, the prefix is realizable, and the LM continues decoding. If it is empty, the framework forces the LM to backtrack and try a different token.
The complete flow of this architecture is illustrated below:
The authors demonstrate ChopChop's flexibility by implementing two distinct pruners: one that enforces program equivalence (using e-graphs and equality saturation via egglog) and another that enforces type safety (using a bidirectional type checker lifted to operate over program spaces).
Datasets
The authors evaluated ChopChop across two distinct benchmark suites using instruction-tuned models (DeepSeek-Coder-6.7b, CodeLlama-7B, and CodeLlama-13B) at various sampling temperatures:
- Equivalence-Guided Decoding: A custom suite of 10 benchmark tasks in a basic functional language where the LM is asked to refactor complex arithmetic expressions (e.g., factoring out subexpressions into
letbindings) while strictly maintaining semantic equivalence to the original program. - Type-Safe Decoding: 72 benchmarks sourced from the TypeScript translations of the MBPP (Mostly Basic Python Problems) dataset within the MultiPL-E framework, filtered to fit the supported language fragment.
Key Insights and Ideas
- Prefixes as Spaces, Not Strings: The core conceptual shift is treating an incomplete token sequence not as a broken string, but as a window into a space of all possible future completions.
- Infinite Spaces as Finite Graphs: By leveraging regular coinduction, infinite sets of programs can be evaluated using finite graph structures. This allows standard static analysis techniques (like type inference) to be lifted from single ASTs to infinite sets of potential ASTs.
- The Danger of Pure Syntax Constraints: Interestingly, the paper notes that grammar-constrained decoding (GCD) alone can sometimes perform worse than unconstrained decoding. Forcing a model into a strict language subset without enforcing semantics often causes the model to "hallucinate" invalid variable mutations or undefined functions just to satisfy the grammar rules.
Key Results
- Dramatic Success Rate Gains: In the TypeScript generation tasks, ChopChop consistently outperformed both unconstrained and grammar-constrained baselines. When aggregated across all temperatures, ChopChop improved the success rate (producing code that successfully compiles) by an average of 28.0% for DeepSeek-Coder-6.7b, 10.1% for CodeLlama-7B, and 23.2% for CodeLlama-13B.
- Perfect Equivalence: In the refactoring benchmarks, unconstrained models almost completely failed to output valid, equivalent code blocks. ChopChop achieved high success rates by systematically blocking non-equivalent paths.
- Practical Latency Overhead: Enforcing these deep semantic properties adds a per-token overhead ranging from tens to a few hundred milliseconds. Crucially, the authors found that 96.4% of the time, the first token proposed by the LM is accepted, meaning the framework rarely needs to trigger expensive backtracking loops.
Limitations and Future Work
- Language Fragment Restrictions: The current prototype supports subsets of TypeScript and a basic functional language. Scaling this up to full, real-world language specifications requires handling highly complex language features.
- Bounded Type Depth: To guarantee that the type-checking analysis remains regular and terminates, the type pruner conservatively bounds types to a depth of 3, sacrificing soundness for highly nested, complex types (though the authors note this didn't impact practical benchmarks).
- Performance Optimization: The current Python implementation is relatively unoptimized. The authors suggest that future iterations could integrate fast syntactic pre-filtering tools (like
LLGuidance) to eliminate syntactically invalid tokens before the semantic pruner even runs.
Summary updated