Abstract
With few exceptions, macros have traditionally been viewed as operations on syntax trees or even on plain strings. This view makes macros seem ad hoc, and is at odds with two desirable features of contemporary typed functional languages: static typing and static scoping. At a deeper level, there is a need for a simple, usable semantics for macros. This paper argues that these problems can be addressed by formally viewing macros as multi-stage computations. This view eliminates the need for freshness conditions and tests on variable names, and provides a compositional interpretation that can serve as a basis for designing a sound type system for languages supporting macros, or even for compilation. To illustrate our approach, we develop and present MacroML, an extension of ML that supports inlining, recursive macros, and the definition of new binding constructs. The latter is subtle, and is the most novel addition in a statically typed setting. The semantics of a core subset of MacroML is given by an interpretation into MetaML, a statically-typed multi-stage programming language. It is then easy to show that MacroML is stage- and type-safe: macro expansion does not depend on runtime evaluation, and both stages do not "go wrong.
Publisher
Association for Computing Machinery (ACM)
Subject
Computer Graphics and Computer-Aided Design,Software
Reference47 articles.
1. Correctness of monadic state
2. Studies in Logic and the Foundations of Mathematics;BARENDREGT H. P.,1984
3. Lecture Notes in Computer Science;CALCAGNO C.,2000
Cited by
27 articles.
订阅此论文施引文献
订阅此论文施引文献,注册后可以免费订阅5篇论文的施引文献,订阅后可以查看论文全部施引文献
1. Rhombus: A New Spin on Macros without All the Parentheses;Proceedings of the ACM on Programming Languages;2023-10-16
2. MacoCaml: Staging Composable and Compilable Macros;Proceedings of the ACM on Programming Languages;2023-08-30
3. Multi-stage programming with generative and analytical macros;Proceedings of the 20th ACM SIGPLAN International Conference on Generative Programming: Concepts and Experiences;2021-10-17
4. mimium: a self-extensible programming language for sound and music;Proceedings of the 9th ACM SIGPLAN International Workshop on Functional Art, Music, Modelling, and Design;2021-08-22
5. A Survey of Metaprogramming Languages;ACM Computing Surveys;2020-11-30