一句话:将类型推断重构为完形填空任务,用提示调优的代码掩码语言模型作为神经知识库实现模糊类型推断。
部分代码中常含非全限定类型名(non-FQN)与未声明的接收者对象,解析它们的全限定名(类型推断)是有效搜索与复用部分代码的前提。现有基于字典查找的方法需构建 API 名与代码上下文的符号知识库,编译开销大,且对未见 API 名与上下文变化敏感。本文把类型推断形式化为 cloze 式填空语言任务,基于源码自然性,采用新颖的“预训练—提示—预测”范式,在原始源码上微调代码掩码语言模型(MLM),将其训练为代码元素的神经知识库,把 FQN 的语法与用法编码进模型参数。该方法轻量、对代码编译依赖极小,并支持模糊的神经类型推断,而非符号化的名称与上下文匹配。作者在来自 GitHub 与 Stack Overflow 的大量源码上系统评估,验证了方法设计的有效性与对部分代码类型推断的实用性,作为同类首创为部分代码的创新使用打开了空间。
Partial code usually involves non-fully-qualified type names (non-FQNs) and undeclared receiving objects. Resolving the FQNs of these non-FQN types and undeclared receiving objects (referred to as type inference) is the prerequisite to effective search and reuse of partial code. Existing dictionary-lookup based methods build a symbolic knowledge base of API names and code contexts, which involve significant compilation overhead and are sensitive to unseen API names and code context variations. In this paper, we formulate type inference as a cloze-style fill-in-blank language task. Built on source code naturalness, our approach fine-tunes a code masked language model (MLM) as a neural knowledge base of code elements with a novel “pre-train, prompt and predict” paradigm from raw source code. Our approach is lightweight and has minimum requirements on code compilation. Unlike existing symbolic name and context matching for type inference, our prompt-tuned code MLM packs FQN syntax and usage in its parameters and supports fuzzy neural type inference. We systematically evaluate our approach on a large amount of source code from GitHub and Stack Overflow. Our results confirm the effectiveness of our approach design and the practicality for partial code type inference. As the first of its kind, our neural type inference method opens the door to many innovative ways of using partial code.
@inproceedings{paperbot7200,
title = {Prompt-tuned Code Language Model as a Neural Knowledge Base for Type Inference in Statically-Typed Partial Code},
author = {Qing Huang and Zhiqiang Yuan and Zhenchang Xing and Xiwei Xu and Liming Zhu and Qinghua Lu},
booktitle = {ASE},
year = {2022},
doi = {10.1145/3551349.3556912}
}