Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
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 2022 Publication History

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.

Supplementary Material

Auxiliary Presentation Video (popl22main-p294-p-video.mp4)
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
[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
[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
[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
[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
[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
[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
[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
[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
[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.
[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
[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
[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
[14]
Dale Miller. 1991. Unification of Simply Typed Lambda-Terms as Logic Programming. In 8th International Logic Programming Conference. MIT Press, 255–269.
[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
[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
[17]
Brigitte Pientka. 2003. Tabled higher-order logic programming. Ph. D. Dissertation. Department of Computer Science, Carnegie Mellon University. CMU-CS-03-185
[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
[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
[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
[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
[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
[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
[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
[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
[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
[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
[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
[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

Cited By

View all
  • (2024)Linear Contextual Metaprogramming and Session TypesElectronic Proceedings in Theoretical Computer Science10.4204/EPTCS.401.1401(1-10)Online publication date: 6-Apr-2024
  • (2024)Staged Compilation with Module FunctorsProceedings of the ACM on Programming Languages10.1145/36746498:ICFP(693-727)Online publication date: 15-Aug-2024
  • (2024)Polymorphic Reachability Types: Tracking Freshness, Aliasing, and Separation in Higher-Order Generic ProgramsProceedings of the ACM on Programming Languages10.1145/36328568:POPL(393-424)Online publication date: 5-Jan-2024
  • Show More Cited By

Recommendations

Comments

Information & Contributors

Information

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
This work is licensed under a Creative Commons Attribution International 4.0 License.

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 for this article.

Check for updates

Author Tags

  1. Contextual Types
  2. Metaprogramming
  3. Polymorphism
  4. Type Systems

Qualifiers

  • Research-article

Funding Sources

Contributors

Other Metrics

Bibliometrics & Citations

Bibliometrics

Article Metrics

  • Downloads (Last 12 months)243
  • Downloads (Last 6 weeks)24
Reflects downloads up to 22 Sep 2024

Other Metrics

Citations

Cited By

View all
  • (2024)Linear Contextual Metaprogramming and Session TypesElectronic Proceedings in Theoretical Computer Science10.4204/EPTCS.401.1401(1-10)Online publication date: 6-Apr-2024
  • (2024)Staged Compilation with Module FunctorsProceedings of the ACM on Programming Languages10.1145/36746498:ICFP(693-727)Online publication date: 15-Aug-2024
  • (2024)Polymorphic Reachability Types: Tracking Freshness, Aliasing, and Separation in Higher-Order Generic ProgramsProceedings of the ACM on Programming Languages10.1145/36328568:POPL(393-424)Online publication date: 5-Jan-2024
  • (2024)A Tutorial on Meta-Services and Services Computing in MetaverseIEEE Internet of Things Journal10.1109/JIOT.2023.334690111:10(16981-16995)Online publication date: 15-May-2024
  • (2023)MacoCaml: Staging Composable and Compilable MacrosProceedings of the ACM on Programming Languages10.1145/36078517:ICFP(604-648)Online publication date: 31-Aug-2023
  • (2023)Normalization by evaluation for modal dependent type theoryJournal of Functional Programming10.1017/S095679682300006033Online publication date: 2-Oct-2023
  • (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)Staged compilation with two-level type theoryProceedings of the ACM on Programming Languages10.1145/35476416:ICFP(540-569)Online publication date: 31-Aug-2022

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