skip to main content
research-article
Open Access

Mœbius: metaprogramming using contextual types: the stage where system f can pattern match on itself

Published:12 January 2022Publication History
Skip Abstract Section

Abstract

We describe the foundation of the metaprogramming language, Mœbius, which supports the generation of polymorphic code and, more importantly, the analysis of polymorphic code via pattern matching.

Mœbius has two main ingredients: 1) we exploit contextual modal types to describe open code together with the context in which it is meaningful. In Mœbius, open code can depend on type and term variables (level 0) whose values are supplied at a later stage, as well as code variables (level 1) that stand for code templates supplied at a later stage. This leads to a multi-level modal lambda-calculus that supports System-F style polymorphism and forms the basis for polymorphic code generation. 2) we extend the multi-level modal lambda-calculus to support pattern matching on code. As pattern matching on polymorphic code may refine polymorphic type variables, we extend our type-theoretic foundation to generate and track typing constraints that arise. We also give an operational semantics and prove type preservation.

Our multi-level modal foundation for Mœbius provides the appropriate abstractions for both generating and pattern matching on open code without committing to a concrete representation of variable binding and contexts. Hence, our work is a step towards building a general type-theoretic foundation for multi-staged metaprogramming that, on the one hand, enforces strong type guarantees and, on the other hand, makes it easy to generate and manipulate code. This will allow us to exploit the full potential of metaprogramming without sacrificing the reliability of and trust in the code we are producing and running.

Skip Supplemental Material Section

Supplemental Material

Auxiliary Presentation Video

This is a 5-min presentation video of my talk at POPL 2022 on the foundation of the metaprogramming language, Mœbius. Mœbius supports the generation of System F style polymorphic code, generation of code depending on code/type templates, and analysis of code/type via pattern matching. Our work extends the contextual modal type theory to type variables and a new dimension, "level", which allows us to distinguish code/type templates from values/types. These extensions provide the power to generate polymorphic code fragments without extra redexes, which programmers do not intend to write. Furthermore, Mœbius supports pattern matching on code with an algorithm for the type variable refinement. Our work is a step towards building a general type-theoretic foundation for multi-staged metaprogramming that enforces strong type guarantees as well as makes it easy to generate and manipulate code fragments as a programmer intends.

References

  1. Abhishek Anand, Simon Boulier, Cyril Cohen, Matthieu Sozeau, and Nicolas Tabareau. 2018. Towards Certified Meta-Programming with Typed Template-Coq. In 9th International Conference Interactive Theorem Proving (ITP’18) (Lecture Notes in Computer Science (LNCS 10895)). Springer, 20–39. https://doi.org/10.1007/978-3-319-94821-8_2 Google ScholarGoogle ScholarCross RefCross Ref
  2. Nick Benton, Chung-Kil Hur, Andrew Kennedy, and Conor McBride. 2012. Strongly Typed Term Representations in Coq. J. Autom. Reasoning, 49, 2 (2012), 141–159. https://doi.org/10.1007/s10817-011-9219-0 Google ScholarGoogle ScholarDigital LibraryDigital Library
  3. Mathieu Boespflug and Brigitte Pientka. 2011. Multi-level Contextual Modal Type Theory. In 6th International Workshop on Logical Frameworks and Meta-languages: Theory and Practice (LFMTP’11) (Electronic Proceedings in Theoretical Computer Science (EPTCS), Vol. 71). 29–43. https://doi.org/10.4204/EPTCS.71.3 Google ScholarGoogle ScholarCross RefCross Ref
  4. Matt Brown and Jens Palsberg. 2016. Breaking through the normalization barrier: a self-interpreter for F-Omega. In 43rd ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages (POPL’16). ACM, 5–17. https://doi.org/10.1145/2837614.2837623 Google ScholarGoogle ScholarDigital LibraryDigital Library
  5. Chiyan Chen and Hongwei Xi. 2003. Meta-Programming through Typeful Code Representation. In 8th ACM SIGPLAN International Conference on Functional Programming (ICFP’03). 275–286. https://doi.org/10.1145/944746.944730 Google ScholarGoogle ScholarDigital LibraryDigital Library
  6. Chiyan Chen and Hongwei Xi. 2005. Meta-programming through Typeful Code Representation. Journal of Functional Programming, 15, 5 (2005), 797–835. https://doi.org/10.1017/S0956796805005617 Google ScholarGoogle ScholarDigital LibraryDigital Library
  7. Rowan Davies. 1996. A Temporal-Logic Approach to Binding-Time Analysis. In 11th IEEE Symposium on Logic in Computer Science (LICS). IEEE Computer Society, 184–195. https://doi.org/10.1109/LICS.1996.561317 Google ScholarGoogle ScholarCross RefCross Ref
  8. Rowan Davies and Frank Pfenning. 2001. A modal analysis of staged computation. J. ACM, 48, 3 (2001), 555–604. https://doi.org/10.1145/382780.382785 Google ScholarGoogle ScholarDigital LibraryDigital Library
  9. Daniel Gratzer, Jonathan Sterling, and Lars Birkedal. 2019. Implementing a Modal Dependent Type Theory. Proceedings of the ACM on Programming Languages (PACMPL), 3, ICFP (2019), 107:1–107:29. https://doi.org/10.1145/3341711 Google ScholarGoogle ScholarDigital LibraryDigital Library
  10. Junyoung Jang, Samuel Gélineau, Stefan Monnier, and Brigitte Pientka. 2021. Moebius: Metaprogramming using Contextual Types – The stage where System F can pattern match on itself (Long Version). arxiv:2111.08099.Google ScholarGoogle Scholar
  11. Akira Kawata and Atsushi Igarashi. 2019. A Dependently Typed Multi-stage Calculus. In 17th Asian Symposium on Programming Languages and Systems (APLAS) (Lecture Notes in Computer Science, Vol. 11893). Springer, 53–72. https://doi.org/10.1007/978-3-030-34175-6_4 Google ScholarGoogle ScholarCross RefCross Ref
  12. Ik-Soon Kim, Kwangkeun Yi, and Cristiano Calcagno. 2006. A Polymorphic Modal Type System for Lisp-like Multi-Staged Languages. In 33rd ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages (POPL’06). ACM Press, 257–268. https://doi.org/10.1145/1111037.1111060 Google ScholarGoogle ScholarDigital LibraryDigital Library
  13. Oleg Kiselyov, Yukiyoshi Kameyama, and Yuto Sudo. 2016. Refined Environment Classifiers - Type- and Scope-Safe Code Generation with Mutable Cells. In 14th Asian Symposium on Programming Languages and Systems (APLAS’16) (Lecture Notes in Computer Science, Vol. 10017). 271–291. https://doi.org/10.1007/978-3-319-47958-3_15 Google ScholarGoogle ScholarCross RefCross Ref
  14. Dale Miller. 1991. Unification of Simply Typed Lambda-Terms as Logic Programming. In 8th International Logic Programming Conference. MIT Press, 255–269.Google ScholarGoogle Scholar
  15. Aleksandar Nanevski, Frank Pfenning, and Brigitte Pientka. 2008. Contextual Modal Type Theory. ACM Trans. Comput. Logic, 9, 3 (2008), June, 1–49. issn:1529-3785 https://doi.org/10.1145/1352582.1352591 Google ScholarGoogle ScholarDigital LibraryDigital Library
  16. Lionel Parreaux, Antoine Voizard, Amir Shaikhha, and Christoph E. Koch. 2018. Unifying Analytic and Statically-Typed Quasiquotes. Proceedings of the ACM on Programming Languages (PACMPL), 2, POPL (2018), 13:1–13:33. https://doi.org/10.1145/3158101 Google ScholarGoogle ScholarDigital LibraryDigital Library
  17. Brigitte Pientka. 2003. Tabled higher-order logic programming. Ph. D. Dissertation. Department of Computer Science, Carnegie Mellon University. CMU-CS-03-185Google ScholarGoogle Scholar
  18. Brigitte Pientka. 2008. A type-theoretic foundation for programming with higher-order abstract syntax and first-class substitutions. In 35th ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages (POPL’08). ACM Press, 371–382. https://doi.org/10.1145/1328897.1328483 Google ScholarGoogle ScholarDigital LibraryDigital Library
  19. Brigitte Pientka, Andreas Abel, Francisco Ferreira, David Thibodeau, and Rebecca Zucchini. 2019. A Type Theory for Defining Logics and Proofs. In 34th IEEE/ ACM Symposium on Logic in Computer Science (LICS’19). IEEE Computer Society, 1–13. https://doi.org/10.1109/LICS.2019.8785683 Google ScholarGoogle ScholarCross RefCross Ref
  20. Brigitte Pientka and Andrew Cave. 2015. Inductive Beluga:Programming Proofs (System Description). In 25th International Conference on Automated Deduction (CADE-25) (Lecture Notes in Computer Science (LNCS 9195)). Springer, 272–281. https://doi.org/10.1007/978-3-319-21401-6_18 Google ScholarGoogle ScholarCross RefCross Ref
  21. Brigitte Pientka and Jana Dunfield. 2008. Programming with proofs and explicit contexts. In ACM SIGPLAN Symposium on Principles and Practice of Declarative Programming (PPDP’08). ACM, 163–173. https://doi.org/10.1145/1389449.1389469 Google ScholarGoogle ScholarDigital LibraryDigital Library
  22. Brigitte Pientka and Jana Dunfield. 2010. Beluga: a Framework for Programming and Reasoning with Deductive Systems (System Description). In 5th International Joint Conference on Automated Reasoning (IJCAR’10) (Lecture Notes in Artificial Intelligence (LNAI 6173)). Springer, 15–21. https://doi.org/10.1007/978-3-642-14203-1_2 Google ScholarGoogle ScholarDigital LibraryDigital Library
  23. Morten Rhiger. 2012. Staged Computation with Staged Lexical Scope. In 21st European Symposium on Programming Languages and Systems (ESOP’12) (Lecture Notes in Computer Science, Vol. 7211). Springer, 559–578. https://doi.org/10.1007/978-3-642-28869-2_28 Google ScholarGoogle ScholarDigital LibraryDigital Library
  24. Tim Sheard and Simon Peyton Jones. 2022. Template Meta-Programming for Haskell. In ACM SIGPLAN Workshop on Haskell (Haskell’02). ACM, 1–16. https://doi.org/10.1145/581690.581691 Google ScholarGoogle ScholarDigital LibraryDigital Library
  25. Walid Taha. 2000. A Sound Reduction Semantics for Untyped CBN Multi-stage Computation. Or, the Theory of MetaML is Non-trivial (Extended Abstract). In ACM SIGPLAN Workshop on Partial Evaluation and Semantics-Based Program Manipulation (PEPM’00). ACM, 34–43. https://doi.org/10.1145/328690.328697 Google ScholarGoogle ScholarDigital LibraryDigital Library
  26. Walid Taha and Michael Florentin Nielsen. 2003. Environment Classifiers. In 30th SIGPLAN-SIGACT Symposium on Principles of Programming Languages (POPL’03). ACM, 26–37. https://doi.org/10.1145/604131.604134 Google ScholarGoogle ScholarDigital LibraryDigital Library
  27. Walid Taha and Tim Sheard. 2000. MetaML and Multi-stage Programming with Explicit Annotations. Theoretical Computer Science, 248, 1-2 (2000), Oct., 211–242. issn:0304-3975 https://doi.org/10.1016/S0304-3975(00)00053-0 Google ScholarGoogle ScholarDigital LibraryDigital Library
  28. Paul van der Walt and Wouter Swierstra.. 2012. Engineering Proof by Reflection in Agda. In 24th Intern. Symp. on Implementation and Application of Functional Languages (IFL). Springer, 157–173. https://doi.org/10.1007/978-3-642-41582-1_10 Google ScholarGoogle ScholarCross RefCross Ref
  29. Marcos Viera and Alberto Pardo. 2006. A Multi-Stage Language with Intensional Analysis. In 5th International Conference on Generative Programming and Component Engineering (GPCE’06). ACM, 11–20. isbn:1595932372 https://doi.org/10.1145/1173706.1173709 Google ScholarGoogle ScholarDigital LibraryDigital Library

Index Terms

  1. Mœbius: metaprogramming using contextual types: the stage where system f can pattern match on itself

          Recommendations

          Comments

          Login options

          Check if you have access through your login credentials or your institution to get full access on this article.

          Sign in

          Full Access

          • Published in

            cover image Proceedings of the ACM on Programming Languages
            Proceedings of the ACM on Programming Languages  Volume 6, Issue POPL
            January 2022
            1886 pages
            EISSN:2475-1421
            DOI:10.1145/3511309
            Issue’s Table of Contents

            Copyright © 2022 Owner/Author

            Publisher

            Association for Computing Machinery

            New York, NY, United States

            Publication History

            • Published: 12 January 2022
            Published in pacmpl Volume 6, Issue POPL

            Permissions

            Request permissions about this article.

            Request Permissions

            Check for updates

            Qualifiers

            • research-article

          PDF Format

          View or Download as a PDF file.

          PDF

          eReader

          View online with eReader.

          eReader
          About Cookies On This Site

          We use cookies to ensure that we give you the best experience on our website.

          Learn more

          Got it!