一句话:提出组合式嵌入,在 CP 语言中兼得浅层与深层嵌入优势,支持 DSL 语言复用、模式匹配与可扩展解释。
定义领域特定语言(DSL)的一种常见方式是将它们直接嵌入宿主语言,包括浅层嵌入和深层嵌入,但二者在现有编程语言中各有取舍,实践中常需混用并引入大量代码和高级技巧。本文利用近期提出的组合编程范式和 CP 语言,提出一种新的组合式嵌入。它一方面具备浅层嵌入的多种语言复用优势,例如复用宿主语言优化、轻松添加新的 DSL 构造;另一方面又类似深层嵌入,支持对 DSL 抽象语法进行模式匹配或动态分派,包括依赖解释、变换和优化,并能新增解释。作者以文档编写 DSL ExT 为例展示组合式嵌入,该 DSL 高度灵活且可扩展,可轻松创建非平凡扩展,如生成类 wiki 文档、LaTeX 文档、矢量图或图表。最后通过三个应用评估组合式嵌入在 ExT 中的可行性。
A common approach to defining domain-specific languages (DSLs) is via a direct embedding into a host language. There are several well-known techniques to do such embeddings, including shallow and deep embeddings. However, such embeddings come with various trade-offs in existing programming languages. Owing to such trade-offs, many embedded DSLs end up using a mix of approaches in practice, requiring a substantial amount of code, as well as some advanced coding techniques. In this paper, we show that the recently proposed Compositional Programming paradigm and the CP language provide improved support for embedded DSLs. In CP we obtain a new form of embedding, which we call a compositional embedding, that has most of the advantages of both shallow and deep embeddings. On the one hand, compositional embeddings enable various forms of linguistic reuse that are characteristic of shallow embeddings, including the ability to reuse host-language optimizations in the DSL and add new DSL constructs easily. On the other hand, similarly to deep embeddings, compositional embeddings support definitions by pattern matching or dynamic dispatching (including dependent interpretations, transformations, and optimizations) over the abstract syntax of the DSL and have the ability to add new interpretations. We illustrate an instance of compositional embeddings with a DSL for document authoring called ExT. The DSL is highly flexible and extensible, allowing users to create various non-trivial extensions easily. For instance, ExT supports various extensions that enable the production of wiki-like documents, LaTeX documents, vector graphics or charts. The viability of compositional embeddings for ExT is evaluated with three applications.
@article{paperbot1423,
title = {Compositional embeddings of domain-specific languages},
author = {Yaozhu Sun and Utkarsh Dhandhania and Bruno C. d. S. Oliveira},
journal = {Proceedings of the ACM on Programming Languages},
volume = {6},
number = {OOPSLA2},
year = {2022},
doi = {10.1145/3563294}
}