[go: up one dir, main page]
More Web Proxy on the site http://driver.im/ skip to main content
article

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

Published: 01 October 2001 Publication History

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.

References

[1]
ARIOLA, Z. M., AND SABRY, A. Correctness of monadic state: An imperative call-by-need calculus. In ACM Symposium on Principles of Programming Languages (1998), ACM Press, pp. 62-74.]]
[2]
BARENDREGT, H. P. The Lambda Calculus: Its Syntax and Semantics, revised ed, vol. 103 of Studies in Logic and the Foundations of Mathematics. North-Holland, Amsterdam, 1984.]]
[3]
BENAISSA, Z. E.-A., MOGGI, E., TAHA, W., AND SHEARD, T. Logical modalities and multi-stage programming. In Federated Logic Conference (FLoe,) Satellite Workshop on Intuitionistic Modal Logics and Applications (IMLA) (1999).]]
[4]
CALCAGNO, C., MOGGI, E., AND TAHA, W. Closed types as a simple approach to safe imperative multi-stage programming. In 27th International Colloquium on Automata, Languages, and Programming (JCALP) (Geneva, 2000), vol. 1853 of Lecture Notes in Computer Science, ACM Press, pp. 25-36.]]
[5]
CARDELLI, L., MATTHES, F. AND AHADI, M. Extensible grammars for language specialization. In Database Programming Languages (DBPL-4) (Feb. 1994), C. Been, A. Ohori, and D. E. Shasha, Eds., Workshops in Computing, Springer-Verlag.]]
[6]
CURRY, H. B., AND FEYS, R. Combinatory Logic, Volume I. Studies in Logic and the Foundations of Mathematics. North-Holland, Amsterdam, 1958. Second printing 1968.]]
[7]
DAVIES, R. A temporal-logic approach to binding-time analysis. In 11 Annual IEEE Symposium on Logic in Computer Science (LICS) (New Brunswick, 1996), IEEE Computer Society Press, pp. 184-195.]]
[8]
DAVIES, R. AND PFENNING, F. A modal analysis of staged computation. In In proceedings of the AC/I! Symposium on Principles of Programming Languages (POPL) (St. Petersburg Beach, 1996), pp. 258-270.]]
[9]
DYBVIG, R. K., HIEB, R., AND BRUGGEMAN, C. Syntactic abstraction in Scheme. Lisp and Symbolic Computation 5, 4 (Dec. 1992), 295-326,]]
[10]
ERKK, L., AND LAUNCHBURY, J. Recursive monadic bindings, In Proceedings of the Fifth ACM SIGPLAN International Conference on Functional Programming, ICFP'OO (September 2000), ACM Press, pp. 174-185.]]
[11]
FELLEISEN, M. On the expressive power of programming languages. In Science of Computer Programming (1991), vol. 17, pp. 35-75. Preliminary version in: Proc. European Symposium on Programming, Lecture Notes in Computer Science, 432. Springer-Verlag (1990), 134-151.]]
[12]
FIORE, M., PLOTKIN, G. AND TURI, D. Abstract syntax and variable binding. In Proceedings of the 14th Annual Symposium on Logic in Computer Science (LICS'99) ('frento, Italy, July 1999), C. Longo, Ed., IEEE Computer Society Press, pp. 193-202.]]
[13]
GLCK, R., AND JORGENSEN, J. Efficient multi-level generating extensions for program specialization. In Programming Languages: Implementations, Logics and Programs (PLILP'gS) (1995), S. D. Swierstra and NI. Hermenegildo, Eds., vol. 982 of Lecture Notes in Computer Science, Springer-Verlag. pp. 259-278.]]
[14]
GLCK, H., AND JORGENSEN, J. Fast binding-time analysis for multi-level specialization. In Perspectives of System Informatics (1996), D. Bjorner, M. Broy, and I. V. Pottosin, Eds., vol. 1181 of Lecture Notes in Computer Science, Springer-Verlag, pp. 261- 272.]]
[15]
GLCK, R., AND JORGENSEN, J. An automatic program generator for multi-level specialization. LISP and Symbolic Computation 10, 2 (1997), 113-158.]]
[16]
G0MARD, C. K., AND JONES, N. D. A partial evaluator for untyped lambda calculus. Journal of Functional Programming 1, 1 (1991), 21-69.]]
[17]
GRIFFIN, T. G. Notational definitions--a formal account. In Proceedings of the Third Symposium on Logic in Computer Science (1988).]]
[18]
HASHIMOTO, M AND OHORI, A. A typed context calculus. Theoretical Computer Science. To appear. Preliminary version. Preprint RIMS-1098, Research Institute for Mathematical Sciences, Kyoto University, 1996.]]
[19]
HOFMANN, M. Semantical analysis of higher-order abstract syntax. In Proceedings of the 14th Annual Symposium on Logic in Computer Science (LICS'gg) (Trento, Italy, July 1999), G. Longo, Ed., IEEE Computer Society Press.]]
[20]
JONES, N. D., GOMARD, C. K., AND SESTOFT, P. Partial Evaluation and Automatic Program Generation. Prentice-Hall, 1993.]]
[21]
KOHLBECKER, E., FRIEDMAN, D. P., FELLEISEN, M., AND DUBA, B. Hygienic macro expansion. In Proceedings of the ACM Conference on LISP and Functional Programming (Cambridge, MA, Aug. 1986), R. P. Gabriel, Ed., ACM Press, pp. 151-181.]]
[22]
LEWIS, J. R., LAUNCHBURY, J., MEIJER, E., AND SHIELDS, M. implicit parameters: Dynamic seeping with static types. In In proceedings of the ACM Symposium on Principles of Programirong Languages (POPL) (N.Y., Jan. 19-21 2000). ACM Press, pp. 108-118.]]
[23]
MASON, I. A. Computing with contexts. Higher-Order and Symbolic Computation 12, 2 (Sept. 1999), 171-201.]]
[24]
MIILLER, D. An extension to NIL to handle bound variables in data structures: Preliminary report. In Informal Proceedings of the Logical Frameworks BRA Workshop (June 1990). Available as UPenn CIS technical report MS-CIS-90-59.]]
[25]
MITCHELL, J. C. On abstraction and the expressive power of programming languages. In Theoretical Aspects of Computer Software (1991), T. Ito and A. R. Meyer, Eds., vol. 526 of Lecture Notes in Computer Science. Springer-Verlag, pp. 290-310.]]
[26]
MOGGI, E. Notions of computation and monads. Information and Computation 93, 1 (1991).]]
[27]
MOGGI, E. A categorical account of two-level languages. In Mathematics Foundations of Program Semantics (1997). Elsevier Science.]]
[28]
MOGGI, E. Functor categories and two-level languages. In Foundations of Software Science and Computation Structures (FoSSaCS) (1998). vol. 1378 of Lecture Notes in Computer Science, Springer Verlag.]]
[29]
MOGGI, E., TAHA, W., BENAISSA, Z. E.-A., AND SHEARD, T. An idealized NletaML: Simpler, and more expressive. In European SYMPOSIUM on Programming (ESOP) (1999), vol. 1576 of Lecture Notes in Computer Science, SpringerVerlag, pp. 193 207.]]
[30]
MONNIER, S., AND SHA0, Z. Inlirung as staged computation. Tech. Rep. YALEU/DCS/TR-l 193, Department of Computer Science. Yale University, Mar. 2000.]]
[31]
MORAN, A., AND SANDS, D. Improvement in a lazy context: An operational theory for call-by-need. In in proceedings of the ACM Symposium on Principles of Programming Languages (POPL) (San Antonio, Texas, Jan. 1999), ACM, pp. 43-36.]]
[32]
NIELSON, F., AND NIELSON, H. R. Two-level semantics and code generation. Theoretical Computer Science 56. 1 (1988), 59-133.]]
[33]
OKASAKI, C. Purely Functional Data Structures, PhD thesis, School of Computer Science, Carnegie Mellon University. 1996.]]
[34]
OLIVA, D. P., RAMSDELL, J. D., AND WAND, M. The VLISP verified prescheme compiler. Lisp and Symbolic Computation 8, 1/2 (1995), 111-182.]]
[35]
Oregon Graduate Institute Technical Reports. P.C. Box 91000, Portland. OR 972911000,IJSA. Available online front ftp://cse.ogi.edu/pub/tech-repo~s/README.html. Last viewed August 1999.]]
[36]
PASALIC, E., SHEARD, T., AND TAHA, W. DALI: An untyped, CBV functional language supporting first-order datatypes with binders (technical development). Tech. Rep. CSE-00007, OGI, 2000. Available from {35}.]]
[37]
PFENNING, F. AND ELLIOTT, C. Higher-order abstract syntax. In Proceedings of the Symposium on Language Design and Implementation (Atlanta 1988), pp. 199 208.]]
[38]
PITTS, A. M. AND GABBAY, M. J. A metalanguage for programming with bound names modulo renaming. In Mathematics of Progrumme Construction (2000), vol. 1837 of Lecture Notes in Computer Science, Springer-Verlag, pp. 230-255.]]
[39]
PLOTKIN, G. D. Call-by-name, call-by-value and the lambda-calculus. Theoretical Computer Science 1 (1975), 125-159.]]
[40]
SABRY, A. What is a purely functional language? Journal of Functional Programming 8, 1 (Jan. 1998), 1-22.]]
[41]
SANDS, D. Computing with contexts: A simple approach. In Second Workshop on HigherOrder Operational Techniques in Semantics (HOOTS II), A. 0. Gordon. A. NI. Pitts, and C. L. Talcott, Eds., vol. 10 of Electron,( Notes in Theoretical Computer Science. Elsevier Science Publishers B.V., 1998.]]
[42]
SHEARD, T. Using MetahIL: A staged programming language. Lecture Notes in Computer Science 1608 1 207-239.]]
[43]
SHIELDS, M., SHEARD, T., AND PETYON JONES, S. Dynamic typing through staged type inference. In In proceedings of the ACM Symposium on Principles of Programming Languages (POPL) (1998), pp. 289 302.]]
[44]
TAHA, W. Multi-Stage Programming: Its Theory ond Applications. PhD thesis, Oregon Graduate Instil it' Science and Technology, 1999. Available from {35}.]]
[45]
TAHA, W. A sound reduction semantics for untyped CBN multi-stage computation. Or, the theory of MetaML, is non-trivial. In Proceedings of the Workshop on J'ortiiil Evaluation and Semantics-Based Program Maniplot ion (PEPM) (Boston, 2000), ACM Press.]]
[46]
TAHA, W., BENAISSA, Z.-E.-A., AND SHEARD, T. Multi-stage programming: Axinmatization and type-safety In 25th International Colloquium on Automata, Languages, and Programming (ICALP) (Aalborg, 1998), vol. 1443 of Lecture Notes in Computer Science, pp. 918-929.]]
[47]
TAHA, W., AND MAKHOLM, H. Tag elimination - or type specialisation is a type-indexed effect. In Subtyping and Dependent Types in Programming. APPSEM Workshop. INRIA technical report, 2000.]]
[48]
TAHA, W., AND SHEARD, T. Multi-stage programming with explicit annotations. In Proceedings of the Symposium of Partial Evaluation and SemanticBased Program Manipulation (PEPM) (Amsterdam, 1997), ACM Press. pp. 203-217.]]
[49]
THIEMANN, P. AND DUSSART, D. Partial evaluation for higher-order languages with state. Available online from http://www.informatik.uni_freiburgdethiemanri/papers index,html. 1996]]
[50]
WAND, M. Embedding type structure in semantics. In In proceedings of the ACM Symposium on Principles of Programming Languages (POI'L) (1985). pp. 1-6.]]
[51]
WAND, M. The theory of fexprs is Iris ml, Lisp and Symbolic Computation 10 (1998). I 59-199.]]

Cited By

View all
  • (2021)Multi-stage programming with generative and analytical macrosProceedings of the 20th ACM SIGPLAN International Conference on Generative Programming: Concepts and Experiences10.1145/3486609.3487203(110-122)Online publication date: 17-Oct-2021
  • (2021)mimium: a self-extensible programming language for sound and musicProceedings of the 9th ACM SIGPLAN International Workshop on Functional Art, Music, Modelling, and Design10.1145/3471872.3472969(1-12)Online publication date: 27-Aug-2021
  • (2020)Multi-stage programming in the large with staged classesProceedings of the 19th ACM SIGPLAN International Conference on Generative Programming: Concepts and Experiences10.1145/3425898.3426961(35-49)Online publication date: 16-Nov-2020
  • Show More Cited By

Recommendations

Comments

Please enable JavaScript to view thecomments powered by Disqus.

Information & Contributors

Information

Published In

cover image ACM SIGPLAN Notices
ACM SIGPLAN Notices  Volume 36, Issue 10
October 2001
276 pages
ISSN:0362-1340
EISSN:1558-1160
DOI:10.1145/507669
Issue’s Table of Contents
  • cover image ACM Conferences
    ICFP '01: Proceedings of the sixth ACM SIGPLAN international conference on Functional programming
    October 2001
    277 pages
    ISBN:1581134150
    DOI:10.1145/507635
Permission to make digital or hard copies of all or part of this work for personal or classroom use is granted without fee provided that copies are not made or distributed for profit or commercial advantage and that copies bear this notice and the full citation on the first page. Copyrights for components of this work owned by others than ACM must be honored. Abstracting with credit is permitted. To copy otherwise, or republish, to post on servers or to redistribute to lists, requires prior specific permission and/or a fee. Request permissions from [email protected]

Publisher

Association for Computing Machinery

New York, NY, United States

Publication History

Published: 01 October 2001
Published in SIGPLAN Volume 36, Issue 10

Check for updates

Qualifiers

  • Article

Contributors

Other Metrics

Bibliometrics & Citations

Bibliometrics

Article Metrics

  • Downloads (Last 12 months)12
  • Downloads (Last 6 weeks)0
Reflects downloads up to 31 Dec 2024

Other Metrics

Citations

Cited By

View all
  • (2021)Multi-stage programming with generative and analytical macrosProceedings of the 20th ACM SIGPLAN International Conference on Generative Programming: Concepts and Experiences10.1145/3486609.3487203(110-122)Online publication date: 17-Oct-2021
  • (2021)mimium: a self-extensible programming language for sound and musicProceedings of the 9th ACM SIGPLAN International Workshop on Functional Art, Music, Modelling, and Design10.1145/3471872.3472969(1-12)Online publication date: 27-Aug-2021
  • (2020)Multi-stage programming in the large with staged classesProceedings of the 19th ACM SIGPLAN International Conference on Generative Programming: Concepts and Experiences10.1145/3425898.3426961(35-49)Online publication date: 16-Nov-2020
  • (2018)Reasonably programmable literal notationProceedings of the ACM on Programming Languages10.1145/32368012:ICFP(1-32)Online publication date: 30-Jul-2018
  • (2017)Quoted staged rewriting: a practical approach to library-defined optimizationsACM SIGPLAN Notices10.1145/3170492.313604352:12(131-145)Online publication date: 23-Oct-2017
  • (2017)Quoted staged rewriting: a practical approach to library-defined optimizationsProceedings of the 16th ACM SIGPLAN International Conference on Generative Programming: Concepts and Experiences10.1145/3136040.3136043(131-145)Online publication date: 23-Oct-2017
  • (2017)Squid: type-safe, hygienic, and reusable quasiquotesProceedings of the 8th ACM SIGPLAN International Symposium on Scala10.1145/3136000.3136005(56-66)Online publication date: 22-Oct-2017
  • (2014)RomeoACM SIGPLAN Notices10.1145/2692915.262816249:9(53-65)Online publication date: 19-Aug-2014
  • (2014)RomeoProceedings of the 19th ACM SIGPLAN international conference on Functional programming10.1145/2628136.2628162(53-65)Online publication date: 19-Aug-2014
  • (2014)Early detection of type errors in C++ templatesProceedings of the ACM SIGPLAN 2014 Workshop on Partial Evaluation and Program Manipulation10.1145/2543728.2543731(133-144)Online publication date: 11-Jan-2014
  • Show More Cited By

View Options

Login options

View options

PDF

View or Download as a PDF file.

PDF

eReader

View online with eReader.

eReader

Media

Figures

Other

Tables

Share

Share

Share this Publication link

Share on social media