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.
- David Abrahams and Aleksey Gurtovoy. 2004. C++ template metaprogramming: concepts, tools, and techniques from Boost and beyond. Pearson Education. Google Scholar
- 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. Google ScholarDigital Library
- Cristiano Calcagno, Eugenio Moggi, and Tim Sheard. 2003. Closed types for a safe imperative MetaML. Journal of functional programming, 13, 3 (2003), 545–571. Google ScholarDigital Library
- 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. Google ScholarCross Ref
- 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 Google ScholarDigital Library
- 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. Google ScholarDigital Library
- 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. Google Scholar
- 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 Google ScholarCross Ref
- 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 Google ScholarDigital Library
- Daniel de Rauglaudre. 2007. Camlp5 - Reference Manual. Google Scholar
- 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 Google Scholar
- 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 Google ScholarDigital Library
- 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 Google ScholarDigital Library
- 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 Google ScholarDigital Library
- 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 Google ScholarDigital Library
- 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. Google ScholarDigital Library
- 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 Google ScholarDigital Library
- 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 Google ScholarDigital Library
- 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 Google ScholarCross Ref
- 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 Google ScholarDigital Library
- 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 Google ScholarCross Ref
- 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. Google ScholarDigital Library
- 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 Google ScholarDigital Library
- 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. Google ScholarDigital Library
- Lexifi. 2023. LRT: LexiFi runtime types. https://github.com/LexiFi/lrt Online; accessed March 2023 Google Scholar
- 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 Google ScholarDigital Library
- 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 Google ScholarDigital Library
- 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 Google ScholarDigital Library
- 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 Google ScholarDigital Library
- 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 Google ScholarDigital Library
- 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 Google ScholarDigital Library
- 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 Google ScholarDigital Library
- 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 Google ScholarDigital Library
- 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. Google Scholar
- Benoît Vaugon. 2013. A new implementation of OCaml formats based on GADTs. OCaml Users and Developers workshop. Google Scholar
- Jerome Vouillon. 2023. Re: A regular expression library for OCaml.. https://github.com/ocaml/ocaml-re Online; accessed March 2023 Google Scholar
- 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 Google Scholar
- Leo White. 2013. Extension points for OCaml. OCaml Users and Developers Workshop. Google Scholar
- 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 Google ScholarCross Ref
- 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 Google ScholarDigital Library
- Ningning Xie, Leo White, Olivier Nicole, and Jeremy Yallop. 2023. MacoCaml: Staging Composable and Compilable Macros (artifact). https://doi.org/10.5281/zenodo.7993994 Google ScholarCross Ref
- Jeremy Yallop. 2017. Staged generic programming. Proc. ACM Program. Lang., 1, ICFP (2017), 29:1–29:29. https://doi.org/10.1145/3110273 Google ScholarDigital Library
- 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 Google ScholarCross Ref
Index Terms
- MacoCaml: Staging Composable and Compilable Macros
Recommendations
Composable and compilable macros:: you want it when?
Many macro systems, especially for Lisp and Scheme, allow macro transformers to perform general computation. Moreover, the language for implementing compile-time macro transformers is usually the same as the language for implementing run-time functions. ...
Composable and compilable macros:: you want it when?
ICFP '02: Proceedings of the seventh ACM SIGPLAN international conference on Functional programmingMany macro systems, especially for Lisp and Scheme, allow macro transformers to perform general computation. Moreover, the language for implementing compile-time macro transformers is usually the same as the language for implementing run-time functions. ...
Composable and hygienic typed syntax macros
SAC '15: Proceedings of the 30th Annual ACM Symposium on Applied ComputingSyntax extension mechanisms are powerful, but reasoning about syntax extensions can be difficult. Recent work on type-specific languages (TSLs) addressed reasoning about composition, hygiene and typing for extensions introducing new literal forms. We ...
Comments