Macros as multi-stage computations: type-safe, generative, binding macros in MacroML

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.

[1]  John Launchbury,et al.  Implicit parameters: dynamic scoping with static types , 2000, POPL '00.

[2]  Haskell B. Curry,et al.  Combinatory Logic, Volume I , 1959 .

[3]  Dave Sands,et al.  Computing with Contexts: A simple approach , 1998, HOOTS.

[4]  Eugenio Moggi,et al.  A categorical account of two-level languages , 1997, MFPS.

[5]  Alan Bawden,et al.  First-class macros have types , 2000, POPL '00.

[6]  Robert Glück,et al.  Fast Binding-Time Analysis for Multi-Level Specialization , 1996, Ershov Memorial Conference.

[7]  Martín Abadi,et al.  Extensible Grammars for Language Specialization , 1993, DBPL.

[8]  Robert Glück,et al.  An Automatic Program Generator for Multi-Level Specialization , 1997, LISP Symb. Comput..

[9]  Amr Sabry,et al.  Correctness of Monadic State: An Imperative Call-by-Need Calculus , 1997, Electron. Notes Theor. Comput. Sci..

[10]  Gordon D. Plotkin,et al.  Abstract syntax and variable binding , 1999, Proceedings. 14th Symposium on Logic in Computer Science (Cat. No. PR00158).

[11]  Matthias Felleisen,et al.  Hygienic macro expansion , 1986, LFP '86.

[12]  Walid Taha,et al.  Logical Modalities and Multi-Stage Programming , 1999 .

[13]  Walid Taha,et al.  Closed Types as a Simple Approach to Safe Imperative Multi-stage Programming , 2000, ICALP.

[14]  Amr Sabry,et al.  What is a purely functional language? , 1998, Journal of Functional Programming.

[15]  Eugenio Moggi,et al.  Notions of Computation and Monads , 1991, Inf. Comput..

[16]  M. Hoffman Semantical analysis of higher-order abstract syntax , 1999 .

[17]  Zhong Shao,et al.  Inlining as staged computation , 2003, J. Funct. Program..

[18]  Timothy G. Griffin,et al.  Notational definition-a formal account , 1988, [1988] Proceedings. Third Annual Information Symposium on Logic in Computer Science.

[19]  John C. Mitchell On Abstraction and the Expressive Power of Programming Languages , 1991, Sci. Comput. Program..

[20]  Benjamin C. Pierce,et al.  Theoretical Aspects of Computer Software , 2001, Lecture Notes in Computer Science.

[21]  Andrew Moran,et al.  Improvement in a lazy context: an operational theory for call-by-need , 1999, POPL '99.

[22]  Walid Taha,et al.  An Idealized MetaML: Simpler, and More Expressive , 1999, ESOP.

[23]  Andrew M. Pitts,et al.  A Metalanguage for Programming with Bound Names Modulo Renaming , 2000, MPC.

[24]  Walid Taha,et al.  Multi-Stage Programming: Its Theory and Applications , 1999 .

[25]  Rowan Davies,et al.  A temporal-logic approach to binding-time analysis , 1995, Proceedings 11th Annual IEEE Symposium on Logic in Computer Science.

[26]  Simon L. Peyton Jones,et al.  Dynamic typing as staged type inference , 1998, POPL '98.

[27]  Carolyn Talcott,et al.  Computing with Contexts , 2022 .

[28]  Robert Hieb,et al.  Syntactic abstraction in scheme , 1992, LISP Symb. Comput..

[29]  StatePeter Thiemann Partial Evaluation for Higher-Order Languages with , 1996 .

[30]  Mitchell Wand,et al.  The VLISP verified PreScheme compiler , 1995, LISP Symb. Comput..

[31]  Ian A. Mason Computing with Contexts , 1999, High. Order Symb. Comput..

[32]  Walid Taha,et al.  Multi-stage programming with explicit annotations , 1997 .

[33]  Mitchell Wand,et al.  Embedding type structure in semantics , 1985, POPL.

[34]  Erik Meijer,et al.  Impli it Parameters: Dynami S oping with Stati Types , 2000 .

[35]  Tim Sheard Using MetaML: A Staged Programming Language , 1998, Advanced Functional Programming.

[36]  Henk Barendregt,et al.  The Lambda Calculus: Its Syntax and Semantics , 1985 .

[37]  Matthias Felleisen,et al.  On the Expressive Power of Programming Languages , 1990, European Symposium on Programming.

[38]  Peter Sestoft,et al.  Partial evaluation and automatic program generation , 1993, Prentice Hall international series in computer science.

[39]  Mitchell Wand,et al.  The Theory of Fexprs is Trivial , 1998, LISP Symb. Comput..

[40]  Walid Taha,et al.  Multi-Stage Programming: Axiomatization and Type Safety , 1998, ICALP.

[41]  Walid Taha,et al.  A sound reduction semantics for untyped CBN mutli-stage computation. Or, the theory of MetaML is non-trival (extended abstract) , 1999, PEPM '00.

[42]  Frank Pfenning,et al.  Higher-order abstract syntax , 1988, PLDI '88.

[43]  Atsushi Ohori,et al.  A typed context calculus , 2001, Theor. Comput. Sci..

[44]  Flemming Nielson,et al.  Two-Level Semantics and Code Generation , 1988, Theor. Comput. Sci..

[45]  Dale Miller,et al.  An Extension to ML to Handle Bound Variables in Data Structures: Preliminary Report , 1990 .

[46]  Walid Taha A sound reduction semantics for untyped CBN multi-stage computation. Or, the theory of MetaML is non-trivial (preliminary Report) , 1999 .

[47]  T. Sheard,et al.  DalI: An Untyped CBV Operational Semantics and Equational Theoryofr Datatypes with Binders(Technical Development) , 2000 .

[48]  Robert Glück,et al.  Efficient Multi-level Generating Extensions for Program Specialization , 1995, PLILP.

[49]  Neil D. Jones,et al.  A partial evaluator for the untyped lambda-calculus , 1991, Journal of Functional Programming.

[50]  Simon L. Peyton Jones,et al.  Imperative functional programming , 1993, POPL '93.

[51]  John Launchbury,et al.  Recursive monadic bindings , 2000, ICFP '00.

[52]  Eugenio Moggi,et al.  Functor Categories and Two-Level Languages , 1998, FoSSaCS.

[53]  Walid Taha,et al.  Tag Elimination - or - Type Specialisation is a Type-Indexed Effect , 2000 .

[54]  Gordon D. Plotkin,et al.  Call-by-Name, Call-by-Value and the lambda-Calculus , 1975, Theor. Comput. Sci..

[55]  Chris Okasaki,et al.  Purely functional data structures , 1998 .