Type-Constrained Code Generation with Language Models
Niels Mündler, Jingxuan He, Hao Wang, Koushik Sen, Dawn Song, Martin T. Vechev
摘要
Large language models (LLMs) have achieved notable success in code generation. However, they still frequently produce uncompilable output because their next-token inference procedure does not model formal aspects of code. Although constrained decoding is a promising approach to alleviate this issue, it has only been applied to handle either domain-specific languages or syntactic features of general-purpose programming languages. However, LLMs frequently generate code with typing errors, which are beyond the domain of syntax and generally hard to adequately constrain. To address this challenge, we introduce a type-constrained decoding approach that leverages type systems to guide code generation. For this purpose, we develop novel prefix automata and a search over inhabitable types, forming a sound approach to enforce well-typedness on LLM-generated code. We formalize our approach on a foundational simply-typed language and extend it to TypeScript to demonstrate practicality. Our evaluation on the HumanEval and MBPP datasets shows that our approach reduces compilation errors by more than half and significantly increases functional correctness in code synthesis, translation, and repair tasks across LLMs of various sizes and model families, including state-of-the-art open-weight models with more than 30B parameters. The results demonstrate the generality and effectiveness of our approach in constraining LLM code generation with formal rules of type systems.
问 Lune
几分钟读透这篇 Paper,不用再花一下午。
Lune 能读完全文,也能读引用它的顶会 Paper,讲清它的结论、证据和局限,每一点都注明出自哪篇 Paper。不用硬啃,也不靠猜。
已有 14 篇顶会 Paper 引用了它。别等审稿人问起,先弄清它们发现了什么。
脉络
- ChopChop: A Programmable Framework for Semantically Constraining the Output of Language Models 引用了 Synchromesh: Reliable Code Generation from Pre-trained Language Models
- Asleep at the Keyboard? Assessing the Security of GitHub Copilot's Code Contributions 引用了 Language Models are Few-Shot Learners
- Equality Saturation Theory Exploration à la Carte 引用了 ChopChop: A Programmable Framework for Semantically Constraining the Output of Language Models
- Constrained Decoding of Diffusion LLMs with Context-Free Grammars 引用了 SWT-Bench: Testing and Validating Real-World Bug-Fixes with Code Agents
- Constrained Decoding of Diffusion LLMs with Context-Free Grammars 引用了 Language Models are Few-Shot Learners
- Constrained Decoding of Diffusion LLMs with Context-Free Grammars 引用了 Synchromesh: Reliable Code Generation from Pre-trained Language Models
- RefineStat: Efficient Exploration for Probabilistic Program Synthesis 引用了 Synchromesh: Reliable Code Generation from Pre-trained Language Models
- Synchromesh: Reliable Code Generation from Pre-trained Language Models 引用了 Language Models are Few-Shot Learners
- Prompting Is Programming: A Query Language for Large Language Models 引用了 Language Models are Few-Shot Learners
- Prompting Is Programming: A Query Language for Large Language Models 引用了 Synchromesh: Reliable Code Generation from Pre-trained Language Models
引用它的顶会 Paper14
- Unveiling the Potential of Diffusion Large Language Model in Controllable GenerationZhen Xiong, Yujun Cai, Zhecheng Li, Yiwei WangICLR 2026 · 19 次引用
- Constrained Decoding of Diffusion LLMs with Context-Free GrammarsNiels Mündler, Jasper Dekoninck, Martin VechevICLR 2026 · 18 次引用
- Equality Saturation Theory Exploration à la CarteAnjali Pal, Brett Saiki, Ryan Tjoa, Cynthia Richey 等OOPSLA 2023 · 11 次引用
- RefineStat: Efficient Exploration for Probabilistic Program SynthesisMadhav Kanda, Shubham Ugare, Sasa MisailovicICLR 2026 · 5 次引用
- ChopChop: A Programmable Framework for Semantically Constraining the Output of Language ModelsShaan Nagy, Timothy Zhou, Nadia Polikarpova, Loris D'AntoniPOPL 2026 · 1 次引用
它引用的 Paper16
- Language Models are Few-Shot LearnersTom B. Brown, Benjamin Mann, Nick Ryder, Melanie Subbiah 等NeurIPS 2020 · 64,255 次引用
- Asleep at the Keyboard? Assessing the Security of GitHub Copilot's Code ContributionsHammond Pearce, Baleegh Ahmad, Benjamin Tan, Brendan Dolan-Gavitt 等S&P 2022 · 725 次引用
- Synchromesh: Reliable Code Generation from Pre-trained Language ModelsGabriel Poesia, Alex Polozov, Vu Le, Ashish Tiwari 等ICLR 2022 · 200 次引用
- SWT-Bench: Testing and Validating Real-World Bug-Fixes with Code AgentsNiels Mündler, Mark Niklas Müller, Jingxuan He, Martin T. VechevNeurIPS 2024 · 172 次引用
- Prompting Is Programming: A Query Language for Large Language ModelsLuca Beurer-Kellner, Marc Fischer, Martin T. VechevPLDI 2023 · 114 次引用
相关 Paper
- Towards Understanding the Characteristics of Code Generation Errors Made by Large Language ModelsZhijie Wang, Zijie Zhou, Da Song, Yuheng Huang 等ICSE 2025 · 12 次引用
- IntentCoding: Amplifying User Intent in Code GenerationZheng Fang, Yihong Dong, Lili Mou, Dongming Jin 等ACL Findings 2026
- CodeT5+: Open Code Large Language Models for Code Understanding and GenerationYue Wang, Hung Le, Akhilesh Gotmare, Nghi D. Q. Bui 等EMNLP 2023 · 339 次引用
- ContractEval: A Benchmark for Evaluating Contract-Satisfying Assertions in Code GenerationSoohan Lim, Joonghyuk Hahn, Hyunwoo Park, Sang-Ki Ko 等ACL Findings 2026
- HumanEval Pro and MBPP Pro: Evaluating Large Language Models on Self-invoking Code Generation TaskZhaojian Yu, Yilun Zhao, Arman Cohan, Xiaoping ZhangACL Findings 2025 · 13 次引用
