编程语言与软件工程2020arxiv.org
编程语言的无监督翻译
Unsupervised translation of programming languages
进阶 · 约 60 分钟读完前置知识:机器翻译/Seq2Seq 基础、编程语言基础、无监督表示学习
导读
这篇论文研究编程语言之间的无监督翻译:模型无需平行语料,即可在 C++、Java、Python 等语言间进行源码级转换。作者称系统还能从无类型语言中推断类型信息,并以计算准确性作为主要评估指标。
HN 关注点:能否真正替代传统 transpiler——示例显示它能识别语言惯用法并生成地道的目标代码,而非逐句机械直译。
后来怎么样
Meta 后续开源了 TransCoder,成为无监督代码翻译方向的代表性工作,引出一系列后续研究。
HN 评论精选
这几条评论里:支持 1 质疑 2 补充 1
支持亮点在于能识别语言惯用法并推断类型,而非机械直译,可用于先翻译签名再翻译代码
很有意思,例子虽有些挑好的,但结果确实亮眼。看到 C++ 的 memset 初始化数组被翻译成 Python 更简洁的惯用写法,令人印象深刻——大多数 transpiler 只会像编译到机器码那样逐句直译,生成冗长糟糕的输出。有 C 转 Rust 的工具把指针索引翻成 unsafe 指针运算,毫无安全性提升。系统还擅长从无类型语言猜类型,比如 int foo(char* s) 里 s 到底是单字符还是数组指针,需要结合上下文判断,这类系统或许能识别意图。一个好的用法可能是先翻译所有函数签名和类型定义,再据此翻译可执行代码。
Animats查看英文原文
Now that's interesting. The examples are a bit cherry-picked, but there are some interesting results. Seeing C++ int allocation[n]; memset(allocation, -1, sizeof(allocation)); for(int i = 0; i < n; i ++) translated into Python: allocation = [-1] * n for i in range(n): is impressive. The translator picked up on two idioms of C++ and translated them to more concise forms in Python. Most "transpilers" just compile into the target language as if compiling to something like machine code, generating worst case wordy output. There's a C to Rust translator which translated C pointer indexing into Rust unsafe pointer arithmetic, which is not a gain in safety. It's also claimed that this system is good at guessing types from untyped languages. So a good way to use this technology might be to have something that looks at source code and tries to translate all the function signatures and type definitions. This includes looking at function calls and bodies to help guess ("infer" is a stretch for this approach) the types of ambiguous variables. Example is int foo(char* s); Is "s" a single character or a pointer to an array? You can't tell without context. This system might be able to do that. Or int getdata(int *buf, size_t bufl); A system like this should be able to recognize the intent there. Then try to translate the executable code with that information available. Bad guesses about types will usually result in translation failures or code that compiles with type errors, so someone will notice. So this is promising for modernizing code. I want to see one able to turn C pointer arithmetic into slice syntax. Now that would be a big step forward.
质疑缺少训练成本和硬件细节,实用性存疑,可能只有大公司玩得起
论文很有意思,但和许多公司资助的 DNN 论文一样,缺少技术细节:大学论文几乎总会给出数据集规模、硬件配置、预处理/训练/推理耗时,这篇却没有。我无法判断这个方案是否实际可行——如果准备数据和训练新语言对需要中型数据中心的算力,那它就只有 GAFA 及其最接近的竞争者用得起。不过我很赞赏他们用计算准确性作为主要评估指标。
qayxc查看英文原文
This paper is very interesting, but as with many recent company-funded DNN-related publications, I am missing some technical information here. Publications by universities will almost always include details like dataset sizes, hardware used, and durations for pre-processing, training, and inference. As-is, this paper leaves me puzzled as to whether this novel solution is even practical in any way shape or form. If I essentially need the computational resources of a medium-sized data centre to prepare the data, and train the system for adding a new language-pair or fine-tuning an existing one, it'd not be a practical method outside of the Four Horsemen (GAFA) and their closest competitors... I applaud the use of computational accuracy as the primary metric for evaluating the results, though.
质疑质疑跨范式(如命令式转函数式/逻辑式)翻译是否同样有效
论文说可以为任意语言对训练翻译器,但他们只用了 C++、Java、Python 这些同属面向对象命令式的语言。换成 C++ 转 OCaml,或 Prolog 转 Java 还能同样有效吗?跨编程范式的转换可能相当棘手。
p4bl0查看英文原文
It says that it is possible to train the translator for any pair of languages. But they did it with C++, Java, and Python, which are all object oriented imperative languages. Would it work as well for a C++ to OCaml translation, or a Prolog to Java translation? Changing of programming paradigm can be quite tricky…
补充补充先例:IRS 曾尝试汇编转 Java,最终被搁置
相关新闻:据报道一位 IRS(美国国税局)员工曾把约 90% 的汇编代码(没错,真是汇编)翻译成了 Java,但 IRS 后来奇怪地放弃了该项目。
avsteele查看英文原文
Related: An IRS employee reportedly got 90% of their assembler (yes really) code translated to JAVA. IRS strangely abandoned the project. https://federalnewsnetwork.com/tom-temin-commentary/2020/01/irs-programming-mystery-continues/
相关资源
从这篇论文所有 HN 讨论帖的全部评论里整理出来的链接。
历次讨论
- 2020Hacker News 讨论帖166 分 · 44 评
- 2020Hacker News 讨论帖14 分 · 1 评
同领域还值得读
Google 的软件工程(2017)
Software Engineering at Google (2017)
介绍了 Google 的软件工程实践,包括其单一代码库(monorepo)、几乎全部开发在主干进行而非分支、代码审查、构建系统 Blaze、协议缓冲区等工具与流程,并讨论了频繁重…
编程语言与软件工程2017 · 1,127 分 · 465 评 · 引用 15入门 · 约 45 分钟编辑精选Peter Naur:编程作为理论构建 (1985)
Peter Naur – Programming as Theory Building (1985)
Peter Naur 1985 年的经典论文《Programming as Theory Building》提出:编程的本质不是写代码或文档,而是在程序员头脑中构建对程序及其运行…
编程语言与软件工程2016 · 629 分 · 74 评入门 · 约 45 分钟编辑精选SciPy 1.0:Python 科学计算的基础算法
SciPy 1.0: fundamental algorithms for scientific computing in Python
这篇发表于 Nature Methods 的论文介绍了 SciPy 1.0——Python 科学计算生态的基石库,涵盖其核心算法模块(优化、积分、插值、线性代数等)、软件架构与社…
编程语言与软件工程2020 · 387 分 · 98 评入门 · 约 45 分钟编辑精选Lisp 的历史(1979)
History of Lisp (1979)
这是 Lisp 发明者 John McCarthy 于 1979 年为 HOPL(编程语言史)会议撰写的回顾文章,讲述 Lisp 的起源、设计动机与早期演进,文中也涉及 if 表…
编程语言与软件工程2018 · 352 分 · 44 评入门 · 约 45 分钟编辑精选使用 NumPy 进行数组编程
Array Programming with NumPy
这篇 Nature 文章介绍 NumPy 的数组编程模型及其在科学计算中的核心作用:以 ndarray 为中心,通过向量化操作让 Python 高效处理多维数组。
编程语言与软件工程2020 · 318 分 · 134 评入门 · 约 40 分钟编辑精选