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

Staging with class: a specification for typed template Haskell

Published: 12 January 2022 Publication History

Abstract

Multi-stage programming using typed code quotation is an established technique for writing optimizing code generators with strong type-safety guarantees. Unfortunately, quotation in Haskell interacts poorly with type classes, making it difficult to write robust multi-stage programs.
We study this unsound interaction and propose a resolution, staged type class constraints, which we formalize in a source calculus λ that elaborates into an explicit core calculus F. We show type soundness of both calculi, establishing that well-typed, well-staged source programs always elaborate to well-typed, well-staged core programs, and prove beta and eta rules for code quotations.
Our design allows programmers to incorporate type classes into multi-stage programs with confidence. Although motivated by Haskell, it is also suitable as a foundation for other languages that support both overloading and quotation.

Supplementary Material

Auxiliary Presentation Video (popl22main-p706-p-video.mp4)
Multi-stage programming using typed code quotation is an established technique for writing optimizing code generators with strong type-safety guarantees. Unfortunately, quotation in Haskell interacts poorly with type classes, making it difficult to write robust multi-stage programs. We study this unsound interaction and propose a resolution, staged type class constraints, which we formalize in a source calculus $\lambda^{\llbracket\Rightarrow\rrbracket}$ that elaborates into an explicit core calculus $F^{\llbracket\rrbracket}$. We show type soundness of both calculi, establishing that well-typed, well-staged source programs always elaborate to well-typed, well-staged core programs, and prove beta and eta rules for code quotations. Our design allows programmers to incorporate type classes into multi-stage programs with confidence. Although motivated by Haskell, it is also suitable as a foundation for other languages that support both overloading and quotation.

References

[1]
Martin Berger, Laurence Tratt, and Christian Urban. 2017. Modelling Homogeneous Generative Meta-Programming. In 31st European Conference on Object-Oriented Programming, ECOOP 2017, June 19-23, 2017, Barcelona, Spain, Peter Müller (Ed.) (LIPIcs, Vol. 74). Schloss Dagstuhl - Leibniz-Zentrum für Informatik, 5:1–5:23. isbn:978-3-95977-035-4 https://doi.org/10.4230/LIPIcs.ECOOP.2017.5
[2]
Gert-Jan Bottu, Georgios Karachalias, Tom Schrijvers, Bruno C. d. S. Oliveira, and Philip Wadler. 2017. Quantified Class Constraints. In Proceedings of the 10th ACM SIGPLAN International Symposium on Haskell (Haskell 2017). Association for Computing Machinery, New York, NY, USA. 148–161. isbn:9781450351829 https://doi.org/10.1145/3122955.3122967
[3]
Edwin Brady and Kevin Hammond. 2006. A Verified Staged Interpreter is a Verified Compiler. In Proceedings of the 5th International Conference on Generative Programming and Component Engineering (GPCE ’06). Association for Computing Machinery, New York, NY, USA. 111–120. isbn:1595932372 https://doi.org/10.1145/1173706.1173724
[4]
Cristiano Calcagno, Walid Taha, Liwen Huang, and Xavier Leroy. 2003. Implementing multi-stage languages using ASTs, Gensym, and reflection. In Proceedings of the 2nd international conference on Generative programming and component engineering (GPCE03). Association for Computing Machinery, 57–76. https://doi.org/10.5555/954186.954190
[5]
Manuel M. T. Chakravarty, Gabriele Keller, and Simon Peyton Jones. 2005. Associated Type Synonyms. SIGPLAN Not., 40, 9 (2005), Sept., 241–253. issn:0362-1340 https://doi.org/10.1145/1090189.1086397
[6]
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
[7]
Seth Fogarty, Emir Pasalic, Jeremy Siek, and Walid Taha. 2007. Concoqtion: Indexed Types Now!. In Proceedings of the 2007 ACM SIGPLAN Symposium on Partial Evaluation and Semantics-Based Program Manipulation (PEPM ’07). Association for Computing Machinery, New York, NY, USA. 112–121. isbn:9781595936202 https://doi.org/10.1145/1244381.1244400
[8]
Daniel Gratzer, GA Kavvos, Andreas Nuyts, and Lars Birkedal. 2020. Multimodal Dependent Type Theory. In submission.
[9]
Cordelia V. Hall, Kevin Hammond, Simon L. Peyton Jones, and Philip Wadler. 1996. Type Classes in Haskell. ACM Trans. Program. Lang. Syst., 18, 2 (1996), 109–138. https://doi.org/10.1145/227699.227700
[10]
Yuichiro Hanada and Atsushi Igarashi. 2014. On Cross-Stage Persistence in Multi-Stage Programming. In Functional and Logic Programming - 12th International Symposium, FLOPS 2014, Kanazawa, Japan, June 4-6, 2014. Proceedings, Michael Codish and Eijiro Sumii (Eds.) (Lecture Notes in Computer Science, Vol. 8475). Springer, 103–118. isbn:978-3-319-07150-3 https://doi.org/10.1007/978-3-319-07151-0_7
[11]
M.P. Jones. 1993. Coherence for qualified types. Yale University, Dept. of Computer Science.
[12]
Mark P. Jones. 1994. Qualified Types: Theory and Practice. Cambridge University Press.
[13]
Manohar Jonnalagedda, Thierry Coppey, Sandro Stucki, Tiark Rompf, and Martin Odersky. 2014. Staged Parser Combinators for Efficient Data Processing. In Proceedings of the 2014 ACM International Conference on Object Oriented Programming Systems Languages & Applications (OOPSLA ’14). Association for Computing Machinery, New York, NY, USA. 637–653. isbn:9781450325851 https://doi.org/10.1145/2660193.2660241
[14]
Akira Kawata and Atsushi Igarashi. 2019. A Dependently Typed Multi-stage Calculus. In Asian Symposium on Programming Languages and Systems. 53–72. https://doi.org/10.1007/978-3-030-34175-6_4
[15]
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
[16]
Oleg Kiselyov. 2017. Generating Code with Polymorphic let: A Ballad of Value Restriction, Copying and Sharing. Electronic Proceedings in Theoretical Computer Science, 241 (2017), Feb, 1–22. issn:2075-2180 https://doi.org/10.4204/eptcs.241.1
[17]
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
[18]
Yuichiro Kokaji and Yukiyoshi Kameyama. 2011. Polymorphic multi-stage language with control effects. In Asian Symposium on Programming Languages and Systems. 105–120. https://doi.org/10.1007/978-3-642-25318-8_11
[19]
Neelakantan R. Krishnaswami and Jeremy Yallop. 2019. A Typed, Algebraic Approach to Parsing. In Proceedings of the 40th ACM SIGPLAN Conference on Programming Language Design and Implementation (PLDI 2019). Association for Computing Machinery, New York, NY, USA. 379–393. isbn:9781450367127 https://doi.org/10.1145/3314221.3314625
[20]
Aleksandar Nanevski. 2002. Meta-Programming with Names and Necessity. 206–217. isbn:1581134878 https://doi.org/10.1145/581478.581498
[21]
Andreas Nuyts and Dominique Devriese. 2018. Degrees of Relatedness: A Unified Framework for Parametricity, Irrelevance, Ad Hoc Polymorphism, Intersections, Unions and Algebra in Dependent Type Theory. In Proceedings of the 33rd Annual ACM/IEEE Symposium on Logic in Computer Science (LICS ’18). Association for Computing Machinery, New York, NY, USA. 779–788. isbn:9781450355834 https://doi.org/10.1145/3209108.3209119
[22]
Junpei Oishi and Yukiyoshi Kameyama. 2017. Staging with control: type-safe multi-stage programming with control operators. In Proceedings of the 16th ACM SIGPLAN International Conference on Generative Programming: Concepts and Experiences, GPCE 2017, Vancouver, BC, Canada, October 23-24, 2017, Matthew Flatt and Sebastian Erdweg (Eds.). ACM, 29–40. isbn:978-1-4503-5524-7 https://doi.org/10.1145/3136040.3136049
[23]
Emir Pašalic. 2004. The role of type equality in meta-programming. Ph.D. Dissertation. OGI School of Science & Engineering at OHSU.
[24]
Emir Pašalic, Walid Taha, and Tim Sheard. 2002. Tagless Staged Interpreters for Typed Languages. In Proceedings of the Seventh ACM SIGPLAN International Conference on Functional Programming (ICFP ’02). Association for Computing Machinery, New York, NY, USA. 218–229. isbn:1581134878 https://doi.org/10.1145/581478.581499
[25]
Simon Peyton Jones, Mark Jones, and Erik Meijer. 1997. Type classes: an exploration of the design space. In Haskell Workshop.
[26]
Simon Peyton Jones, Dimitrios Vytiniotis, Stephanie Weirich, and Geoffrey Washburn. 2006. Simple Unification-based Type Inference for GADTs. In Proceedings of the Eleventh ACM SIGPLAN International Conference on Functional Programming (ICFP ’06). ACM, New York, NY, USA. 50–61. isbn:1-59593-309-3 https://doi.org/10.1145/1159803.1159811
[27]
Frank Pfenning. 2001. Intensionality, extensionality, and proof irrelevance in modal type theory. In Proceedings 16th Annual IEEE Symposium on Logic in Computer Science. 221–230.
[28]
Matthew Pickering. 2019. Overloaded Quotations. GHC proposal. https://github.com/ghc-proposals/ghc-proposals/blob/master/proposals/0246-overloaded-bracket.rst
[29]
Matthew Pickering, Andres Löh, and Nicolas Wu. 2020. Staged sums of products. In Proceedings of the 13th ACM SIGPLAN International Symposium on Haskell, Haskell@ICFP 2020, Virtual Event, USA, August 7, 2020, Tom Schrijvers (Ed.). ACM, 122–135. https://doi.org/10.1145/3406088.3409021
[30]
Matthew Pickering, Nicolas Wu, and Csongor Kiss. 2019. Multi-Stage Programs in Context. In Proceedings of the 12th ACM SIGPLAN International Symposium on Haskell (Haskell 2019). Association for Computing Machinery, New York, NY, USA. 71–84. isbn:9781450368131 https://doi.org/10.1145/3331545.3342597
[31]
Gabriel Radanne. 2017. Tierless Web programming in ML. (Programmation Web sans-étages en ML). Ph.D. Dissertation. Paris Diderot University, France. https://tel.archives-ouvertes.fr/tel-01788885
[32]
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
[33]
Evgeny Roubinchtein. 2015. IR-MetaOCaml: (re)implementing MetaOCaml. Master’s thesis. University of British Columbia. https://doi.org/10.14288/1.0166800
[34]
Philipp Schuster, Jonathan Immanuel Brachthäuser, and Klaus Ostermann. 2020. Zero-cost Effect Handlers by Staging. In submission.
[35]
Alejandro Serrano, Jurriaan Hage, Simon Peyton Jones, and Dimitrios Vytiniotis. 2020. A Quick Look at Impredicativity. Proc. ACM Program. Lang., 4, ICFP (2020), Article 89, Aug., 29 pages. https://doi.org/10.1145/3408971
[36]
Tim Sheard and Simon Peyton Jones. 2002. Template Meta-programming for Haskell. In Proceedings of the 2002 ACM SIGPLAN Workshop on Haskell (Haskell ’02). ACM, New York, NY, USA. 1–16. isbn:1-58113-605-6 https://doi.org/10.1145/581690.581691
[37]
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
[38]
Walid Taha and Tim Sheard. 1997. Multi-stage Programming with Explicit Annotations. In Proceedings of the 1997 ACM SIGPLAN Symposium on Partial Evaluation and Semantics-based Program Manipulation (PEPM ’97). ACM, New York, NY, USA. 203–217. isbn:0-89791-917-3 https://doi.org/10.1145/258993.259019
[39]
Walid Taha and Tim Sheard. 2000. MetaML and multi-stage programming with explicit annotations. Theor. Comput. Sci., 248, 1-2 (2000), 211–242. https://doi.org/10.1016/S0304-3975(00)00053-0
[40]
Walid Taha and Tim Sheard. 1998. Multi-stage programming: Axiomatization and type safety. In International Colloquium on Automata, Languages, and Programming. 918–929.
[41]
Walid Mohamed Taha. 1999. Multistage programming: its theory and applications. Oregon Graduate Institute of Science and Technology.
[42]
Dimitrios Vytiniotis, Simon Peyton Jones, Tom Schrijvers, and Martin Sulzmann. 2011. OutsideIn(X) Modular Type Inference with Local Assumptions. J. Funct. Program., 21, 4-5 (2011), Sept., 333–412. issn:0956-7968 https://doi.org/10.1017/S0956796811000098
[43]
Phillip Wadler and Stephen Blott. 1989. How to Make Ad-hoc Polymorphism Less Ad Hoc. In Proceedings of the 16th ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages (POPL ’89). ACM, New York, NY, USA. 60–76. isbn:0-89791-294-2 https://doi.org/10.1145/75277.75283
[44]
Stefan Wehr and Manuel M. T. Chakravarty. 2008. ML Modules and Haskell Type Classes: A Constructive Comparison. In Programming Languages and Systems, 6th Asian Symposium, APLAS 2008, Bangalore, India, December 9-11, 2008. Proceedings, G. Ramalingam (Ed.) (Lecture Notes in Computer Science, Vol. 5356). Springer, 188–204. https://doi.org/10.1007/978-3-540-89330-1_14
[45]
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
[46]
Jamie Willis, Nicolas Wu, and Matthew Pickering. 2020. Staged selective parser combinators. Proc. ACM Program. Lang., 4, ICFP (2020), 120:1–120:30. https://doi.org/10.1145/3409002
[47]
Jeremy Yallop. 2017. Staged Generic Programming. Proc. ACM Program. Lang., 1, ICFP (2017), Article 29, Aug., 29 pages. https://doi.org/10.1145/3110273
[48]
Jeremy Yallop, Tamara von Glehn, and Ohad Kammar. 2018. Partially-Static Data as Free Extension of Algebras. Proc. ACM Program. Lang., 2, ICFP (2018), Article 100, July, 30 pages. https://doi.org/10.1145/3236795
[49]
Jeremy Yallop and Leo White. 2015. Modular Macros. OCaml Users and Developers Workshop.

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
  • (2024)Closure-Free Functional Programming in a Two-Level Type TheoryProceedings of the ACM on Programming Languages10.1145/36746488:ICFP(659-692)Online publication date: 15-Aug-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
  • 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. Staging
  2. Type Classes
  3. Typed Template Haskell

Qualifiers

  • Research-article

Funding Sources

Contributors

Other Metrics

Bibliometrics & Citations

Bibliometrics

Article Metrics

  • Downloads (Last 12 months)249
  • Downloads (Last 6 weeks)41
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
  • (2024)Closure-Free Functional Programming in a Two-Level Type TheoryProceedings of the ACM on Programming Languages10.1145/36746488:ICFP(659-692)Online publication date: 15-Aug-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
  • (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