paperbot · PL 论文追踪

全部论文 待读 收藏

P1 Type-Constrained Code Generation with Language Models

使用语言模型的类型约束代码生成
PLDI 9(PLDI)2025匹配度 7/10 · 论文处于 PL 形式化方法(类型系统、前缀自动机)与 LLM 代码生成的交叉点,且有理论形式化、TypeScript 实现和在强模型上的评估,符合用户对理论+工程、问题驱动和较强模型验证的偏好。但它并非对 Agentic 程序或 ML 系统做静态分析,也不是用 LLM/Agent 改进静态分析/形式化验证,且不涉及浮点程序分析或贝叶斯程序分析,因此不是正中研究方向核心。引用 9 inbox
Niels Mündler, Jingxuan He, Hao Wang, Koushik Sen, Dawn Song, Martin Vechev
crossref: 2026-09-10

AI 速览

一句话:提出类型约束解码:用前缀自动机与可居留类型搜索保证 LLM 生成代码良类型,编译错误减少过半。

LLM 虽在代码生成上表现突出,但其逐 token 推理不建模代码的形式性质,常生成无法编译、尤其带类型错误的代码;已有约束解码仅覆盖 DSL 或语法特征,难以约束类型。本文提出类型约束解码:用类型系统引导生成,设计了新的前缀自动机与可居留类型(inhabitable types)搜索,形成可靠的良类型性约束,并在基础简单类型语言上形式化、扩展到 TypeScript 以验证实用性。在 HumanEval 与 MBPP 上,跨多个模型规模与家族的代码合成、翻译与修复任务中,编译错误减少一半以上,功能正确性显著提升。

问题
LLM 代码生成常因不建模形式类型规则而产生类型错误、导致无法编译;已有约束解码只能处理 DSL 或语法层面,无法可靠约束类型。
方法
提出类型约束解码,设计前缀自动机并对可居留类型进行搜索,在解码时可靠地强制良类型性;先在简单类型语言上形式化,再扩展到 TypeScript。
评估
在 HumanEval 与 MBPP 上,对多种规模与家族的 LLM(含 30B 以上开源权重模型)开展代码合成、翻译与修复评测,编译错误减少一半以上且功能正确性显著提升。
原文摘要(Abstract)

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.

链接与引用

DOI 原文 · PDF(开放获取) ·

BibTeX
@article{paperbot3123,
  title = {Type-Constrained Code Generation with Language Models},
  author = {Niels Mündler and Jingxuan He and Hao Wang and Koushik Sen and Dawn Song and Martin Vechev},
  journal = {Proceedings of the ACM on Programming Languages},
  volume = {9},
  number = {PLDI},
  year = {2025},
  doi = {10.1145/3729274}
}