Home
Scholarly Works
Macros as multi-stage computations
Journal article

Macros as multi-stage computations

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.

Authors

Ganz SE; Sabry A; Taha W

Journal

ACM SIGPLAN Notices, Vol. 36, No. 10, pp. 74–85

Publisher

Association for Computing Machinery (ACM)

Publication Date

January 1, 2001

DOI

10.1145/507669.507646

ISSN

0362-1340

Contact the Experts team