一句话:为支持动态继承的静态类型语言 CP 设计类型安全编译方案,并优化合并操作性能。
动态类型语言中的动态继承很强大,但静态类型化困难,多数静态语言只支持静态继承而牺牲灵活性。CP 是带 merge 算子与参数多态的静态类型语言,支持多继承、一等类、虚拟类和族多态,但高效编译尤其分离编译很困难,且其子类型是强制转换式的。本文提出将 CP 编译到支持可扩展记录的语言,记录标签由类型生成以高效查找 merge;核心方案在 Coq 中形式化并证明类型安全,实际编译器以 JavaScript 对象建模记录。基准测试表明,经 CP 专用优化后,合并编译比朴素方案快若干数量级,性能与基于类的 JavaScript 程序相当。
Inheritance is a key concept in many programming languages. Dynamically typed languages, such as JavaScript, often support powerful forms of dynamic inheritance. However, dynamic inheritance poses significant challenges for static typing. Most statically typed languages only provide static inheritance to achieve type safety at the cost of flexibility. This article presents a compiler for the CP language, which is a statically typed language that supports dynamic inheritance via a merge operator and also has an expressive form of parametric polymorphism. The merge operator enables a form of multiple inheritance and first-class classes, as well as virtual classes and family polymorphism. With these features, CP allows the development of highly modular and loosely coupled components. However, the efficient compilation of CP code is non-trivial, especially if separate compilation is desired. In particular, subtyping in CP is coercive for type safety, which poses significant challenges in obtaining an efficient compilation scheme. We show how CP is compilable to languages supporting extensible records or similar data structures, where record labels are generated from types for efficient lookup on merges. The main ideas of the compilation scheme are formalized in Coq and proven to be type-safe. The concrete implementation of the CP compiler targets JavaScript, where records are modeled as JavaScript objects. We conduct an empirical evaluation with various benchmarks and evaluate the impact of several CP-specific optimizations. With our optimizations, CP can be orders of magnitude faster than with a naive compilation scheme for merges, obtaining performance on par with class-based JavaScript programs.
@article{SunHO25,
title = {Type-Safe Compilation of Dynamic Inheritance via Merging},
author = {Yaozhu Sun and Xuejing Huang and Bruno C. D. S. Oliveira},
journal = {ACM Transactions on Programming Languages and Systems},
volume = {47},
number = {4},
year = {2025},
doi = {10.1145/3765518}
}