Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
skip to main content
research-article
Open access

MacoCaml: Staging Composable and Compilable Macros

Published: 31 August 2023 Publication History

Abstract

We introduce MacoCaml, a new design and implementation of compile-time code generation for the OCaml language. MacoCaml features a novel combination of macros with phase separation and quotation-based staging, where macros are considered as compile-time bindings, expression cross evaluation phases using staging annotations, and compile-time evaluation happens inside top-level splices. We provide a theoretical foundation for MacoCaml by formalizing a typed source calculus maco that supports interleaving typing and compile-time code generation, references with explicit compile-time heaps, and modules. We study various crucial properties including soundness and phase distinction. We have implemented MacoCaml in the OCaml compiler, and ported two substantial existing libraries to validate our implementation.

References

[1]
David Abrahams and Aleksey Gurtovoy. 2004. C++ template metaprogramming: concepts, tools, and techniques from Boost and beyond. Pearson Education.
[2]
Eugene Burmako. 2013. Scala macros: Let our powers combine! On how rich syntax and static types work with metaprogramming. In Proceedings of the 4th Workshop on Scala. 1–10.
[3]
Cristiano Calcagno, Eugenio Moggi, and Tim Sheard. 2003. Closed types for a safe imperative MetaML. Journal of functional programming, 13, 3 (2003), 545–571.
[4]
Cristiano Calcagno, Walid Taha, Liwen Huang, and Xavier Leroy. 2003. Implementing multi-stage languages using ASTs, gensym, and reflection. In International Conference on Generative Programming and Component Engineering. 57–76.
[5]
Jacques Carette and Oleg Kiselyov. 2005. Multi-stage Programming with Functors and Monads: Eliminating Abstraction Overhead from Generic Code. In Generative Programming and Component Engineering, 4th International Conference, GPCE 2005, Tallinn, Estonia, September 29 - October 1, 2005, Proceedings, Robert Glück and Michael R. Lowry (Eds.) (Lecture Notes in Computer Science, Vol. 3676). Springer, 256–274. https://doi.org/10.1007/11561347_18
[6]
William Clinger and Jonathan Rees. 1991. Macros that work. In Proceedings of the 18th ACM SIGPLAN-SIGACT symposium on Principles of Programming Languages. 155–162.
[7]
Ryan Culpepper, Sam Tobin-Hochstadt, and Matthew Flatt. 2007. Advanced Macrology and the Implementation of Typed Scheme. In Proceedings of the Eighth Workshop on Scheme and Functional Programming.
[8]
Rowan Davies. 1996. A Temporal-Logic Approach to Binding-Time Analysis. In Proceedings, 11th Annual IEEE Symposium on Logic in Computer Science, New Brunswick, New Jersey, USA, July 27-30, 1996. IEEE Computer Society, 184–195. https://doi.org/10.1109/LICS.1996.561317
[9]
Rowan Davies and Frank Pfenning. 1996. A Modal Analysis of Staged Computation. In Conference Record of POPL’96: The 23rd ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages, Papers Presented at the Symposium, St. Petersburg Beach, Florida, USA, January 21-24, 1996, Hans-Juergen Boehm and Guy L. Steele Jr. (Eds.). ACM Press, 258–270. https://doi.org/10.1145/237721.237788
[10]
Daniel de Rauglaudre. 2007. Camlp5 - Reference Manual.
[11]
Marcell Fischbach and Benedikt Meurer. 2011. Towards a native toplevel for the OCaml language. CoRR, abs/1110.1029 (2011), arXiv:1110.1029. arxiv:1110.1029
[12]
Matthew Flatt. 2002. Composable and Compilable Macros: You Want It When? In Proceedings of the Seventh ACM SIGPLAN International Conference on Functional Programming (ICFP ’02). Association for Computing Machinery, New York, NY, USA. 72–83. isbn:1581134878 https://doi.org/10.1145/581478.581486
[13]
Matthew Flatt. 2013. Submodules in Racket: You Want It When, Again? In Proceedings of the 12th International Conference on Generative Programming: Concepts and Experiences (GPCE ’13). Association for Computing Machinery, New York, NY, USA. 13–22. isbn:9781450323734 https://doi.org/10.1145/2517208.2517211
[14]
Matthew Flatt, Ryan Culpepper, David Darais, and Robert Bruce Findler. 2012. Macros that Work Together - Compile-time bindings, partial expansion, and definition contexts. J. Funct. Program., 22, 2 (2012), 181–216. https://doi.org/10.1017/S0956796812000093
[15]
Steven E. Ganz, Amr Sabry, and Walid Taha. 2001. Macros as Multi-Stage Computations: Type-Safe, Generative, Binding Macros in MacroML. In Proceedings of the Sixth ACM SIGPLAN International Conference on Functional Programming (ICFP ’01), Firenze (Florence), Italy, September 3-5, 2001, Benjamin C. Pierce (Ed.). ACM, 74–85. https://doi.org/10.1145/507635.507646
[16]
Robert Harper, John C Mitchell, and Eugenio Moggi. 1989. Higher-order modules and the phase distinction. In Proceedings of the 17th ACM SIGPLAN-SIGACT symposium on Principles of programming languages. 341–354.
[17]
Jun Inoue, Oleg Kiselyov, and Yukiyoshi Kameyama. 2016. Staging beyond Terms: Prospects and Challenges. In Proceedings of the 2016 ACM SIGPLAN Workshop on Partial Evaluation and Program Manipulation (PEPM ’16). Association for Computing Machinery, New York, NY, USA. 103–108. isbn:9781450340977 https://doi.org/10.1145/2847538.2847548
[18]
Junyoung Jang, Samuel Gélineau, Stefan Monnier, and Brigitte Pientka. 2022. Mœ bius: metaprogramming using contextual types: the stage where system f can pattern match on itself. Proc. ACM Program. Lang., 6, POPL (2022), 1–27. https://doi.org/10.1145/3498700
[19]
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
[20]
Oleg Kiselyov, Aggelos Biboudis, Nick Palladinos, and Yannis Smaragdakis. 2017. Stream fusion, to completeness. In Proceedings of the 44th ACM SIGPLAN Symposium on Principles of Programming Languages, POPL 2017, Paris, France, January 18-20, 2017, Giuseppe Castagna and Andrew D. Gordon (Eds.). ACM, 285–299. isbn:978-1-4503-4660-3 https://doi.org/10.1145/3009837
[21]
Oleg Kiselyov, Yukiyoshi Kameyama, and Yuto Sudo. 2016. Refined Environment Classifiers - Type- and Scope-Safe Code Generation with Mutable Cells. In Programming Languages and Systems - 14th Asian Symposium, APLAS 2016, Hanoi, Vietnam, November 21-23, 2016, Proceedings, Atsushi Igarashi (Ed.) (Lecture Notes in Computer Science, Vol. 10017). 271–291. https://doi.org/10.1007/978-3-319-47958-3_15
[22]
Eugene Kohlbecker, Daniel P Friedman, Matthias Felleisen, and Bruce Duba. 1986. Hygienic macro expansion. In Proceedings of the 1986 ACM Conference on LISP and Functional Programming. 151–161.
[23]
András Kovács. 2022. Staged compilation with two-level type theory. Proc. ACM Program. Lang., 6, ICFP (2022), 540–569. https://doi.org/10.1145/3547641
[24]
Xavier Leroy. 1994. Manifest types, modules, and separate compilation. In Proceedings of the 21st ACM SIGPLAN-SIGACT symposium on Principles of programming languages. 109–122.
[25]
Lexifi. 2023. LRT: LexiFi runtime types. https://github.com/LexiFi/lrt Online; accessed March 2023
[26]
John C. Mitchell and Robert Harper. 1988. The Essence of ML. In Conference Record of the Fifteenth Annual ACM Symposium on Principles of Programming Languages, San Diego, California, USA, January 10-13, 1988, Jeanne Ferrante and Peter Mager (Eds.). ACM Press, 28–46. https://doi.org/10.1145/73560.73563
[27]
Aleksandar Nanevski, Frank Pfenning, and Brigitte Pientka. 2008. Contextual modal type theory. ACM Trans. Comput. Log., 9, 3 (2008), 23:1–23:49. https://doi.org/10.1145/1352582.1352591
[28]
Lionel Parreaux, Antoine Voizard, Amir Shaikhha, and Christoph E. Koch. 2018. Unifying analytic and statically-typed quasiquotes. Proc. ACM Program. Lang., 2, POPL (2018), 13:1–13:33. https://doi.org/10.1145/3158101
[29]
Tiark Rompf and Martin Odersky. 2010. Lightweight Modular Staging: A Pragmatic Approach to Runtime Code Generation and Compiled DSLs. In Proceedings of the Ninth International Conference on Generative Programming and Component Engineering (GPCE ’10). ACM, New York, NY, USA. 127–136. isbn:978-1-4503-0154-1 https://doi.org/10.1145/1868294.1868314
[30]
Yuhi Sato and Yukiyoshi Kameyama. 2021. Type-Safe Generation of Modules in Applicative and Generative Styles. In Proceedings of the 20th ACM SIGPLAN International Conference on Generative Programming: Concepts and Experiences (GPCE 2021). Association for Computing Machinery, New York, NY, USA. 184–196. isbn:9781450391122 https://doi.org/10.1145/3486609.3487209
[31]
Yuhi Sato, Yukiyoshi Kameyama, and Takahisa Watanabe. 2020. Module Generation without Regret. In Proceedings of the 2020 ACM SIGPLAN Workshop on Partial Evaluation and Program Manipulation (PEPM 2020). Association for Computing Machinery, New York, NY, USA. 1–13. isbn:9781450370967 https://doi.org/10.1145/3372884.3373160
[32]
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
[33]
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, Boston, MA, USA, November 5-6, 2018, Eric Van Wyk and Tiark Rompf (Eds.). ACM, 14–27. https://doi.org/10.1145/3278122.3278139
[34]
Walid Taha, Zine-El-Abidine Benaissa, and Tim Sheard. 1998. Multi-stage programming: Axiomatization and type safety. In International Colloquium on Automata, Languages, and Programming. 918–929.
[35]
Benoît Vaugon. 2013. A new implementation of OCaml formats based on GADTs. OCaml Users and Developers workshop.
[36]
Jerome Vouillon. 2023. Re: A regular expression library for OCaml. https://github.com/ocaml/ocaml-re Online; accessed March 2023
[37]
Liang Wang and Jianxin Zhao. 2022. Architecture of Advanced Numerical Analysis Systems: Designing a Scientific Computing System using OCaml. Apress Berkeley, CA. ISBN 978-1-4842-8852-8
[38]
Leo White. 2013. Extension points for OCaml. OCaml Users and Developers Workshop.
[39]
Leo White, Frédéric Bour, and Jeremy Yallop. 2014. Modular implicits. In Proceedings ML Family/OCaml Users and Developers workshops, ML/OCaml 2014, Gothenburg, Sweden, September 4-5, 2014, Oleg Kiselyov and Jacques Garrigue (Eds.) (EPTCS, Vol. 198). 22–63. https://doi.org/10.4204/EPTCS.198.2
[40]
Ningning Xie, Matthew Pickering, Andres Löh, Nicolas Wu, Jeremy Yallop, and Meng Wang. 2022. Staging with Class: A Specification for Typed Template Haskell. Proc. ACM Program. Lang., 6, POPL (2022), Article 61, jan, 30 pages. https://doi.org/10.1145/3498723
[41]
Ningning Xie, Leo White, Olivier Nicole, and Jeremy Yallop. 2023. MacoCaml: Staging Composable and Compilable Macros (artifact). https://doi.org/10.5281/zenodo.7993994
[42]
Jeremy Yallop. 2017. Staged generic programming. Proc. ACM Program. Lang., 1, ICFP (2017), 29:1–29:29. https://doi.org/10.1145/3110273
[43]
Jeremy Yallop, David Sheets, and Anil Madhavapeddy. 2018. A modular foreign function interface. Sci. Comput. Program., 164 (2018), 82–97. https://doi.org/10.1016/j.scico.2017.04.002

Cited By

View all
  • (2024)Staged Compilation with Module FunctorsProceedings of the ACM on Programming Languages10.1145/36746498:ICFP(693-727)Online publication date: 15-Aug-2024

Recommendations

Comments

Information & Contributors

Information

Published In

cover image Proceedings of the ACM on Programming Languages
Proceedings of the ACM on Programming Languages  Volume 7, Issue ICFP
August 2023
981 pages
EISSN:2475-1421
DOI:10.1145/3554311
Issue’s Table of Contents
This work is licensed under a Creative Commons Attribution 4.0 International License.

Publisher

Association for Computing Machinery

New York, NY, United States

Publication History

Published: 31 August 2023
Published in PACMPL Volume 7, Issue ICFP

Permissions

Request permissions for this article.

Check for updates

Badges

Author Tags

  1. Compile-time code generation
  2. Macros
  3. OCaml
  4. Staging

Qualifiers

  • Research-article

Funding Sources

Contributors

Other Metrics

Bibliometrics & Citations

Bibliometrics

Article Metrics

  • Downloads (Last 12 months)330
  • Downloads (Last 6 weeks)49
Reflects downloads up to 16 Oct 2024

Other Metrics

Citations

Cited By

View all
  • (2024)Staged Compilation with Module FunctorsProceedings of the ACM on Programming Languages10.1145/36746498:ICFP(693-727)Online publication date: 15-Aug-2024

View Options

View options

PDF

View or Download as a PDF file.

PDF

eReader

View online with eReader.

eReader

Get Access

Login options

Full Access

Media

Figures

Other

Tables

Share

Share

Share this Publication link

Share on social media