skip to main content
10.1145/3486609.3487203acmconferencesArticle/Chapter ViewAbstractPublication PagesgpceConference Proceedingsconference-collections
research-article

Multi-stage programming with generative and analytical macros

Published: 22 November 2021 Publication History

Abstract

In metaprogramming, code generation and code analysis are complementary. Traditionally, principled metaprogramming extensions for programming languages, like MetaML and BER MetaOCaml, offer strong foundations for code generation but lack equivalent support for code analysis. Similarly, existing macro systems are biased towards the code generation aspect.
In this work, we present a calculus for macros featuring both code generation and code analysis. The calculus directly models separate compilation of macros, internalizing a commonly neglected aspect of macros. The system ensures that the generated code is well-typed and hygienic.
We implement our system in Scala 3, provide a formalization, and prove its soundness.

Supplementary Material

Auxiliary Presentation Video (splashws21gpcemain-p21-p-video.mp4)
In metaprogramming, code generation and code analysis are complementary. Traditionally, principled metaprogramming extensions for programming languages, like MetaML and BER MetaOCaml, offer strong foundations for code generation but lack equivalent support for code analysis. Similarly, existing macro systems are biased towards the code generation aspect. In this work, we present a calculus for macros featuring both code generation and code analysis. The calculus directly models separate compilation of macros, internalizing a commonly neglected aspect of macros. The system ensures that the generated code is well-typed and hygienic. We implement our system in Scala 3, provide a formalization, and prove its soundness.

References

[1]
Leif Andersen, Stephen Chang, and Matthias Felleisen. 2017. Super 8 Languages for Making Movies (Functional Pearl). Proc. ACM Program. Lang., 1, ICFP (2017), Article 30, Aug., 29 pages. https://doi.org/10.1145/3110274
[2]
Henk P. Barendregt. 1992. Lambda Calculi with Types.
[3]
Cristiano Calcagno, Walid Taha, Liwen Huang, and Xavier Leroy. 2003. Implementing Multi-stage Languages Using ASTs, Gensym, and Reflection. In Proc. of the 2nd International Conference on Generative Programming and Component Engineering (GPCE ’03). Springer-Verlag, Berlin, Heidelberg. 57–76. isbn:3-540-20102-5 https://doi.org/10.1007/978-3-540-39815-8_4
[4]
Krzysztof Czarnecki, John T. O’Donnell, Jörg Striegnitz, and Walid Taha. 2004. DSL Implementation in MetaOCaml, Template Haskell, and C++. Springer Berlin Heidelberg, Berlin, Heidelberg. 51–72. isbn:978-3-540-25935-0 https://doi.org/10.1007/978-3-540-25935-0_4
[5]
Krzysztof Czarnecki, Kasper Ø sterbye, and Markus Völter. 2002. Generative Programming. In Object-Oriented Technology ECOOP 2002 Workshop Reader, Juan Hernández and Ana Moreira (Eds.). Springer Berlin Heidelberg, Berlin, Heidelberg. 15–29. isbn:978-3-540-36208-1 https://doi.org/10.1007/3-540-36208-8_2
[6]
Rowan Davies. 2017. A Temporal Logic Approach to Binding-Time Analysis. J. ACM, 64, 1 (2017), Article 1, March, 45 pages. issn:0004-5411 https://doi.org/10.1145/3011069
[7]
Rowan Davies and Frank Pfenning. 2001. A Modal Analysis of Staged Computation. J. ACM, 48, 3 (2001), May, 555–604. issn:0004-5411 https://doi.org/10.1145/382780.382785
[8]
Matthew Flatt. 2002. Composable and Compilable Macros: You Want It When? SIGPLAN Not., 37, 9 (2002), Sept., 72–83. issn:0362-1340 https://doi.org/10.1145/583852.581486
[9]
Steven E. Ganz, Amr Sabry, and Walid Taha. 2001. Macros as Multi-Stage Computations: Type-Safe, Generative, Binding Macros in MacroML. SIGPLAN Not., 36, 10 (2001), Oct., 74–85. issn:0362-1340 https://doi.org/10.1145/507669.507646
[10]
Ulrik Jørring and William L. Scherlis. 1986. Compilers and Staging Transformations. Association for Computing Machinery, New York, NY, USA. 86–96. isbn:9781450373470 https://doi.org/10.1145/512644.512652
[11]
Oleg Kiselyov. 2014. The Design and Implementation of BER MetaOCaml. In Functional and Logic Programming, Michael Codish and Eijiro Sumii (Eds.). Springer International Publishing, Cham. 86–102. isbn:978-3-319-07151-0 https://doi.org/10.1007/978-3-319-07151-0_6
[12]
Oleg Kiselyov. 2018. Reconciling Abstraction with High Performance: A MetaOCaml approach. Foundations and Trends® in Programming Languages, 5, 1 (2018), 1–101. issn:2325-1107 https://doi.org/10.1561/2500000038
[13]
Oleg Kiselyov and Chung-chieh Shan. 2010. The MetaOCaml files - Status report and research proposal. In ACM SIGPLAN Workshop on ML.
[14]
Yannis Lilis and Anthony Savidis. 2019. A Survey of Metaprogramming Languages. ACM Comput. Surv., 52, 6 (2019), Article 113, Oct., 39 pages. issn:0360-0300 https://doi.org/10.1145/3354584
[15]
Fred McBride. 1970. Computer Aided Manipulation of Symbols. Ph.D. Dissertation. Queen’s University of Belfast.
[16]
Lionel Parreaux, Amir Shaikhha, and Christoph E. Koch. 2017. Quoted Staged Rewriting: A Practical Approach to Library-Defined Optimizations. SIGPLAN Not., 52, 12 (2017), Oct., 131–145. issn:0362-1340 https://doi.org/10.1145/3170492.3136043
[17]
Lionel Parreaux, Amir Shaikhha, and Christoph E. Koch. 2017. Squid: Type-Safe, Hygienic, and Reusable Quasiquotes. In Proceedings of the 8th ACM SIGPLAN International Symposium on Scala (SCALA 2017). Association for Computing Machinery, New York, NY, USA. 56–66. isbn:9781450355292 https://doi.org/10.1145/3136000.3136005
[18]
Lionel Parreaux, Antoine Voizard, Amir Shaikhha, and Christoph E. Koch. 2017. Unifying Analytic and Statically-Typed Quasiquotes. Proc. ACM Program. Lang., 2, POPL (2017), Article 13, Dec., 33 pages. https://doi.org/10.1145/3158101
[19]
F. Pfenning and C. Elliott. 1988. Higher-Order Abstract Syntax. SIGPLAN Not., 23, 7 (1988), June, 199–208. issn:0362-1340 https://doi.org/10.1145/960116.54010
[20]
Tim Sheard and Simon Peyton Jones. 2002. Template Meta-Programming for Haskell. In Proceedings of the 2002 ACM SIGPLAN Workshop on Haskell (Haskell ’02). Association for Computing Machinery, New York, NY, USA. 1–16. isbn:1581136056 https://doi.org/10.1145/581690.581691
[21]
Yannis Smaragdakis, Aggelos Biboudis, and George Fourtounis. 2017. Structured Program Generation Techniques. In Grand Timely Topics in Software Engineering, Jácome Cunha, João P. Fernandes, Ralf Lämmel, João Saraiva, and Vadim Zaytsev (Eds.). Springer International Publishing, Cham. 154–178. isbn:978-3-319-60074-1 https://doi.org/10.1007/978-3-319-60074-1_7
[22]
Nicolas Stucki, Aggelos Biboudis, Sébastien Doeraene, and Martin Odersky. 2020. Semantics-Preserving Inlining for Metaprogramming. In Proceedings of the 11th ACM SIGPLAN International Symposium on Scala (SCALA 2020). Association for Computing Machinery, New York, NY, USA. 14–24. isbn:9781450381772 https://doi.org/10.1145/3426426.3428486
[23]
Nicolas Stucki, Aggelos Biboudis, and Martin Odersky. 2018. A Practical Unification of Multi-Stage Programming and Macros. In Proceedings of the 17th ACM SIGPLAN International Conference on Generative Programming: Concepts and Experiences (GPCE 2018). Association for Computing Machinery, New York, NY, USA. 14–27. isbn:9781450360456 https://doi.org/10.1145/3278122.3278139
[24]
Nicolas Stucki, Jonathan Immanuel Brachthäuser, and Martin Odersky. 2021. Proof of Multi-Stage Programming with Generative and Analytical Macros. EPFL.
[25]
Walid Taha and Tim Sheard. 1997. Multi-Stage Programming with Explicit Annotations. SIGPLAN Not., 32, 12 (1997), Dec., 203–217. issn:0362-1340 https://doi.org/10.1145/258994.259019
[26]
Sam Tobin-Hochstadt, Vincent St-Amour, Ryan Culpepper, Matthew Flatt, and Matthias Felleisen. 2011. Languages as Libraries. SIGPLAN Not., 46, 6 (2011), June, 132–141. issn:0362-1340 https://doi.org/10.1145/1993316.1993514
[27]
Sam Tobin-Hochstadt, Vincent St-Amour, Eric Dobson, and Asumu Takikawa. 2021. The Typed Racket Guide - Caveats and Limitations.
[28]
T Veldhuizen and E Gannon. 1998. Active libraries: Rethinking the roles of compilers and libraries. In Proc. of the 1998 SIAM Workshop: Object Oriented Methods for Interoperable Scientific and Engineering Computing. 286–295.

Cited By

View all
  • (2024)Seamless Scope-Safe Metaprogramming through Polymorphic Subtype Inference (Short Paper)Proceedings of the 23rd ACM SIGPLAN International Conference on Generative Programming: Concepts and Experiences10.1145/3689484.3690733(121-127)Online publication date: 21-Oct-2024
  • (2023)Contextual Modal Type Theory with Polymorphic ContextsProgramming Languages and Systems10.1007/978-3-031-30044-8_11(281-308)Online publication date: 22-Apr-2023
  • (2022)The essence of online data processingProceedings of the ACM on Programming Languages10.1145/35633206:OOPSLA2(899-928)Online publication date: 31-Oct-2022

Recommendations

Comments

Information & Contributors

Information

Published In

cover image ACM Conferences
GPCE 2021: Proceedings of the 20th ACM SIGPLAN International Conference on Generative Programming: Concepts and Experiences
October 2021
209 pages
ISBN:9781450391122
DOI:10.1145/3486609
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]

Sponsors

Publisher

Association for Computing Machinery

New York, NY, United States

Publication History

Published: 22 November 2021

Permissions

Request permissions for this article.

Check for updates

Author Tags

  1. formalization
  2. macro systems
  3. metaprogramming
  4. multi-stage programming

Qualifiers

  • Research-article

Funding Sources

  • Swiss National Science Foundation

Conference

GPCE '21
Sponsor:
GPCE '21: Concepts and Experiences
October 17 - 18, 2021
IL, Chicago, USA

Acceptance Rates

Overall Acceptance Rate 56 of 180 submissions, 31%

Upcoming Conference

Contributors

Other Metrics

Bibliometrics & Citations

Bibliometrics

Article Metrics

  • Downloads (Last 12 months)61
  • Downloads (Last 6 weeks)10
Reflects downloads up to 15 Oct 2024

Other Metrics

Citations

Cited By

View all
  • (2024)Seamless Scope-Safe Metaprogramming through Polymorphic Subtype Inference (Short Paper)Proceedings of the 23rd ACM SIGPLAN International Conference on Generative Programming: Concepts and Experiences10.1145/3689484.3690733(121-127)Online publication date: 21-Oct-2024
  • (2023)Contextual Modal Type Theory with Polymorphic ContextsProgramming Languages and Systems10.1007/978-3-031-30044-8_11(281-308)Online publication date: 22-Apr-2023
  • (2022)The essence of online data processingProceedings of the ACM on Programming Languages10.1145/35633206:OOPSLA2(899-928)Online publication date: 31-Oct-2022

View Options

Get Access

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