Abstract
Large Language Models (LLMs) often struggle with type correctness in code generation, leading to frequent compilation errors. We present TyFlow, a framework that internalizes type reasoning by representing programs as sequences of synthesis decisions rather than plain text. By leveraging the mathematical isomorphism between type derivation trees and program construction, TyFlow ensures type correctness and improves functional accuracy. Evaluation on SuFu and Java benchmarks shows that TyFlow eliminates compilation errors in low-resource settings and significantly outperforms traditional text-based baselines.
Introduction
While Large Language Models (LLMs) are remarkably good at writing code, they often fail at maintaining type consistency. Even state-of-the-art models frequently produce code that doesn't compile because of undefined variables, mismatched types, or scoping errors. This "representation gap" exists because standard LLMs treat code as a flat sequence of text tokens, whereas type checking is a structural, logical process. TyFlow aims to bridge this gap by making the type-checking process the foundation of how the model generates code.
Methodology
The core innovation of TyFlow is a type-guided program synthesis system based on constructive logic. TyFlow leverages the principle that a proof of a program's existence contains the program itself.
Synthesis Decision Sequences and Isomorphism
Instead of predicting the next word, the TyFlow model predicts the next logical rule to apply. The authors prove that the tree of decisions used to build a program is mathematically isomorphic to the tree used to type-check it.
As shown in Figure 1, the type derivation provides a structured logical path. TyFlow internalizes this by generating a sequence of synthesis decisions that correspond to these logical steps.
Figure 2 demonstrates the synthesis process. The model identifies a "Current Goal" (a specific type and context) and applies a rule to refine it into subgoals. This ensures that every generated fragment is valid within its specific scope.
Dual-Encoder Architecture and Pruning
TyFlow uses a specialized encoder-decoder setup. A static encoder handles the natural language prompt, while a dynamic encoder re-processes the current "synthesis goal" at every step. This provides the model with localized type information, relieving it of the burden of long-range memory. During generation, TyFlow employs syntactic and type pruning to immediately reject any decision that would violate the language's rules, guaranteeing type-correct output.
Experimental Evaluation
TyFlow was evaluated on two datasets: SuFu, a low-resource functional language, and a subset of Java based on the MBJP benchmark.
Key Results
- Compilation Accuracy: In the SuFu benchmark, TyFlow reduced the Compilation Error Rate (CER) to 0%. In Java, the CER dropped from 38.51% to 3.52%.
- Functional Correctness: On SuFu, the Pass@10 metric improved from 32.76% to 46.55% compared to baselines.
- Efficiency: TyFlow is more concise, using 45-48% fewer tokens than methods that generate a proof and code separately.
Conclusion
TyFlow demonstrates that internalizing type-system rules is more effective than external filtering. By providing the model with localized type information and enforcing logical consistency through isomorphism, TyFlow significantly improves the reliability of neural code generation. Future work will extend this approach to more complex logical constraints, such as memory safety in Rust.
Summary updated