一句话:提出“去嵌入”通用策略,将 HOAS 风格嵌入表示转为 de Bruijn 索引项,以支持增量计算与双向变换等语义域。
嵌入是一种以宿主语言库形式实现对象语言的技术,轻量且能继承宿主语言特性;HOAS 利用高阶函数表示带绑定器的抽象语法树,但难以处理无法自然表达为函数的语义域,尤其是涉及开放表达式的情况,如增量计算与可逆/双向语言。本文指出 HOAS 的困难源于宿主语言与对象语言函数语义域之间的不匹配,并提出基于去嵌入(unembedding)的解决方案:将 finally-tagless 表示转换为带强正确性保证的 de Bruijn 索引项。作者给出通用的“Embedding by Unembedding”策略,并在增量计算和双向变换两个实质性案例中验证其有效性,所得嵌入式实现的功能可与各自领域最先进的实现相媲美,同时保持 HOAS 的简洁性。
Embedding is a language development technique that implements the object language as a library in a host language. There are many advantages of the approach, including being lightweight and the ability to inherit features of the host language. A notable example is the technique of HOAS, which makes crucial use of higher-order functions to represent abstract syntax trees with binders. Despite its popularity, HOAS has its limitations. We observe that HOAS struggles with semantic domains that cannot be naturally expressed as functions, particularly when open expressions are involved. Prominent examples of this include incremental computation and reversible/bidirectional languages. In this paper, we pin-point the challenge faced by HOAS as a mismatch between the semantic domain of host and object language functions, and propose a solution. The solution is based on the technique of unembedding , which converts from the finally-tagless representation to de Bruijn-indexed terms with strong correctness guarantees. We show that this approach is able to extend the applicability of HOAS while preserving its elegance. We provide a generic strategy for Embedding by Unembedding, and then demonstrate its effectiveness with two substantial case studies in the domains of incremental computation and bidirectional transformations. The resulting embedded implementations are comparable in features to the state-of-the-art language implementations in the respective areas.
@article{paperbot1931,
title = {Embedding by Unembedding},
author = {Kazutaka Matsuda and Samantha Frohlich and Meng Wang and Nicolas Wu},
journal = {Proceedings of the ACM on Programming Languages},
volume = {7},
number = {ICFP},
year = {2023},
doi = {10.1145/3607830}
}