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.
Supplemental Material
- 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 Google Scholar
Cross Ref
- 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 Google Scholar
Digital Library
- 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 Google Scholar
Digital Library
- 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.954190Google Scholar
Digital Library
- 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 Google Scholar
Digital Library
- 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 Google Scholar
Digital Library
- 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 Google Scholar
Digital Library
- Daniel Gratzer, GA Kavvos, Andreas Nuyts, and Lars Birkedal. 2020. Multimodal Dependent Type Theory. In submission.Google Scholar
- 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 Google Scholar
Digital Library
- 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 Google Scholar
Cross Ref
- M.P. Jones. 1993. Coherence for qualified types. Yale University, Dept. of Computer Science.Google Scholar
- Mark P. Jones. 1994. Qualified Types: Theory and Practice. Cambridge University Press.Google Scholar
Cross Ref
- 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 Google Scholar
Digital Library
- 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 Google Scholar
Cross Ref
- 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 Scholar
Cross Ref
- 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 Google Scholar
Cross Ref
- 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 Scholar
Cross Ref
- 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 Google Scholar
Digital Library
- 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 Google Scholar
Digital Library
- Aleksandar Nanevski. 2002. Meta-Programming with Names and Necessity. 206–217. isbn:1581134878 https://doi.org/10.1145/581478.581498 Google Scholar
Digital Library
- 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 Google Scholar
Digital Library
- 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 Google Scholar
Digital Library
- Emir Pašalic. 2004. The role of type equality in meta-programming. Ph.D. Dissertation. OGI School of Science & Engineering at OHSU.Google Scholar
- 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 Google Scholar
Digital Library
- Simon Peyton Jones, Mark Jones, and Erik Meijer. 1997. Type classes: an exploration of the design space. In Haskell Workshop.Google Scholar
- 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 Google Scholar
Digital Library
- 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.Google Scholar
Digital Library
- Matthew Pickering. 2019. Overloaded Quotations. GHC proposal. https://github.com/ghc-proposals/ghc-proposals/blob/master/proposals/0246-overloaded-bracket.rstGoogle Scholar
- Matthew Pickering, Andres Löh, and Nicolas Wu. 2020. Staged sums of products. In Proceedings of the 13th ACM SIGPLAN International Symposium on Haskell, [email protected] 2020, Virtual Event, USA, August 7, 2020, Tom Schrijvers (Ed.). ACM, 122–135. https://doi.org/10.1145/3406088.3409021 Google Scholar
Digital Library
- 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 Google Scholar
Digital Library
- 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-01788885Google Scholar
- 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 Scholar
Digital Library
- Evgeny Roubinchtein. 2015. IR-MetaOCaml: (re)implementing MetaOCaml. Master’s thesis. University of British Columbia. https://doi.org/10.14288/1.0166800 Google Scholar
Cross Ref
- Philipp Schuster, Jonathan Immanuel Brachthäuser, and Klaus Ostermann. 2020. Zero-cost Effect Handlers by Staging. In submission.Google Scholar
- 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 Google Scholar
Digital 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). ACM, New York, NY, USA. 1–16. isbn:1-58113-605-6 https://doi.org/10.1145/581690.581691 Google Scholar
Digital 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). Association for Computing Machinery, New York, NY, USA. 14–27. isbn:9781450360456 https://doi.org/10.1145/3278122.3278139 Google Scholar
Digital Library
- 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 Google Scholar
Digital Library
- 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 Google Scholar
Digital Library
- Walid Taha and Tim Sheard. 1998. Multi-stage programming: Axiomatization and type safety. In International Colloquium on Automata, Languages, and Programming. 918–929.Google Scholar
- Walid Mohamed Taha. 1999. Multistage programming: its theory and applications. Oregon Graduate Institute of Science and Technology.Google Scholar
- 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 Google Scholar
Digital Library
- 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 Google Scholar
Digital Library
- 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 Google Scholar
Digital Library
- 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 Scholar
Cross Ref
- 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 Google Scholar
Digital Library
- Jeremy Yallop. 2017. Staged Generic Programming. Proc. ACM Program. Lang., 1, ICFP (2017), Article 29, Aug., 29 pages. https://doi.org/10.1145/3110273 Google Scholar
Digital Library
- 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 Google Scholar
Digital Library
- Jeremy Yallop and Leo White. 2015. Modular Macros. OCaml Users and Developers Workshop.Google Scholar
Index Terms
Staging with class: a specification for typed template Haskell
Recommendations
Interactive type debugging in Haskell
Haskell '03: Proceedings of the 2003 ACM SIGPLAN workshop on HaskellIn this paper we illustrate the facilities for type debugging of Haskell programs in the Chameleon programming environment. Chameleon provides an extension to Haskell supporting advanced and programmable type extensions. Chameleon maps the typing ...
Haskell session types with (almost) no class
HASKELL '08We describe an implementation of session types in Haskell. Session types statically enforce that client-server communication proceeds according to protocols. They have been added to several concurrent calculi, but few implementations of session types ...
Type classes in Haskell
This article defines a set of type inference rules for resolving overloading introduced by type classes, as used in the functional programming language Haskell. Programs including type classes are transformed into ones which may be typed by standard ...






Comments