skip to main content

Staged compilation with two-level type theory

Published:31 August 2022Publication History
Skip Abstract Section

Abstract

The aim of staged compilation is to enable metaprogramming in a way such that we have guarantees about the well-formedness of code output, and we can also mix together object-level and meta-level code in a concise and convenient manner. In this work, we observe that two-level type theory (2LTT), a system originally devised for the purpose of developing synthetic homotopy theory, also serves as a system for staged compilation with dependent types. 2LTT has numerous good properties for this use case: it has a concise specification, well-behaved model theory, and it supports a wide range of language features both at the object and the meta level. First, we give an overview of 2LTT's features and applications in staging. Then, we present a staging algorithm and prove its correctness. Our algorithm is "staging-by-evaluation", analogously to the technique of normalization-by-evaluation, in that staging is given by the evaluation of 2LTT syntax in a semantic domain. The staging algorithm together with its correctness constitutes a proof of strong conservativity of 2LLT over the object theory. To our knowledge, this is the first description of staged compilation which supports full dependent types and unrestricted staging for types.

References

  1. Andreas Abel and Thierry Coquand. 2007. Untyped Algorithmic Equality for Martin-Löf’s Logical Framework with Surjective Pairs. Fundam. Informaticae, 77, 4 (2007), 345–395. http://content.iospress.com/articles/fundamenta-informaticae/fi77-4-05 Google ScholarGoogle ScholarDigital LibraryDigital Library
  2. Andreas Abel, Thierry Coquand, and Miguel Pagano. 2011. A Modular Type-checking algorithm for Type Theory with Singleton Types and Proof Irrelevance. Log. Methods Comput. Sci., 7, 2 (2011), https://doi.org/10.2168/LMCS-7(2:4)2011 Google ScholarGoogle ScholarCross RefCross Ref
  3. Andreas Abel, Joakim Öhman, and Andrea Vezzosi. 2018. Decidability of conversion for type theory in type theory. Proc. ACM Program. Lang., 2, POPL (2018), 23:1–23:29. https://doi.org/10.1145/3158111 Google ScholarGoogle ScholarDigital LibraryDigital Library
  4. Agda developers. 2022. Agda documentation. https://agda.readthedocs.io/en/v2.6.2.1/ Google ScholarGoogle Scholar
  5. Thorsten Altenkirch and Ambrus Kaposi. 2016. Type theory in type theory using quotient inductive types. In Proceedings of the 43rd Annual ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages, POPL 2016, St. Petersburg, FL, USA, January 20 - 22, 2016, Rastislav Bodik and Rupak Majumdar (Eds.). ACM, 18–29. isbn:978-1-4503-3549-2 https://doi.org/10.1145/2837614.2837638 Google ScholarGoogle ScholarDigital LibraryDigital Library
  6. Thorsten Altenkirch and Ambrus Kaposi. 2017. Normalisation by Evaluation for Type Theory, in Type Theory. Logical Methods in Computer Science, Volume 13, Issue 4 (2017), Oct., https://doi.org/10.23638/LMCS-13(4:1)2017 Google ScholarGoogle ScholarCross RefCross Ref
  7. Danil Annenkov, Paolo Capriotti, Nicolai Kraus, and Christian Sattler. 2019. Two-Level Type Theory and Applications. ArXiv e-prints, may, arxiv:1705.03307 Google ScholarGoogle Scholar
  8. Rafaël Bocquet, Ambrus Kaposi, and Christian Sattler. 2021. Relative induction principles for type theories. arXiv preprint arXiv:2102.11649. Google ScholarGoogle Scholar
  9. Corrado Böhm and Alessandro Berarducci. 1985. Automatic Synthesis of Typed Lambda-Programs on Term Algebras. Theor. Comput. Sci., 39 (1985), 135–154. https://doi.org/10.1016/0304-3975(85)90135-5 Google ScholarGoogle ScholarCross RefCross Ref
  10. Edwin C. Brady and Kevin Hammond. 2010. Scrapping your inefficient engine: using partial evaluation to improve domain-specific language implementation. In Proceeding of the 15th ACM SIGPLAN international conference on Functional programming, ICFP 2010, Baltimore, Maryland, USA, September 27-29, 2010, Paul Hudak and Stephanie Weirich (Eds.). ACM, 297–308. https://doi.org/10.1145/1863543.1863587 Google ScholarGoogle ScholarDigital LibraryDigital Library
  11. Paolo Capriotti. 2017. Models of type theory with strict equality. arXiv preprint arXiv:1702.04912. Google ScholarGoogle Scholar
  12. Simon Castellan, Pierre Clairambault, and Peter Dybjer. 2019. Categories with Families: Unityped, Simply Typed, and Dependently Typed. CoRR, abs/1904.00827 (2019), arXiv:1904.00827. arxiv:1904.00827 Google ScholarGoogle Scholar
  13. Evan Cavallo, Anders Mörtberg, and Andrew W. Swan. 2020. Unifying Cubical Models of Univalent Type Theory. In 28th EACSL Annual Conference on Computer Science Logic, CSL 2020, January 13-16, 2020, Barcelona, Spain, Maribel Fernández and Anca Muscholl (Eds.) (LIPIcs, Vol. 152). Schloss Dagstuhl - Leibniz-Zentrum für Informatik, 14:1–14:17. https://doi.org/10.4230/LIPIcs.CSL.2020.14 Google ScholarGoogle ScholarCross RefCross Ref
  14. James Chapman, Pierre-Évariste Dagand, Conor McBride, and Peter Morris. 2010. The gentle art of levitation. ACM Sigplan Notices, 45, 9 (2010), 3–14. Google ScholarGoogle ScholarDigital LibraryDigital Library
  15. Thierry Coquand. 1996. An Algorithm for Type-Checking Dependent Types. Sci. Comput. Program., 26, 1-3 (1996), 167–177. https://doi.org/10.1016/0167-6423(95)00021-6 Google ScholarGoogle ScholarDigital LibraryDigital Library
  16. Thierry Coquand. 2019. Canonicity and normalization for dependent type theory. Theor. Comput. Sci., 777 (2019), 184–191. https://doi.org/10.1016/j.tcs.2019.01.015 Google ScholarGoogle ScholarDigital LibraryDigital Library
  17. Duncan Coutts, Roman Leshchinskiy, and Don Stewart. 2007. Stream fusion: from lists to streams to nothing at all. In Proceedings of the 12th ACM SIGPLAN International Conference on Functional Programming, ICFP 2007, Freiburg, Germany, October 1-3, 2007, Ralf Hinze and Norman Ramsey (Eds.). ACM, 315–326. https://doi.org/10.1145/1291151.1291199 Google ScholarGoogle ScholarDigital LibraryDigital Library
  18. Pierre-Évariste Dagand. 2017. The essence of ornaments. J. Funct. Program., 27 (2017), e9. https://doi.org/10.1017/S0956796816000356 Google ScholarGoogle ScholarCross RefCross Ref
  19. Olivier Danvy, Karoline Malmkjæ r, and Jens Palsberg. 1996. Eta-Expansion Does The Trick. ACM Trans. Program. Lang. Syst., 18, 6 (1996), 730–751. https://doi.org/10.1145/236114.236119 Google ScholarGoogle ScholarDigital LibraryDigital Library
  20. 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
  21. Larry Diehl. 2017. Fully Generic Programming over Closed Universes of Inductive-Recursive Types. Ph. D. Dissertation. Portland State University. Google ScholarGoogle ScholarCross RefCross Ref
  22. Jana Dunfield and Neel Krishnaswami. 2021. Bidirectional Typing. ACM Comput. Surv., 54, 5 (2021), 98:1–98:38. https://doi.org/10.1145/3450952 Google ScholarGoogle ScholarDigital LibraryDigital Library
  23. Andrew John Gill, John Launchbury, and Simon L. Peyton Jones. 1993. A Short Cut to Deforestation. In Proceedings of the conference on Functional programming languages and computer architecture, FPCA 1993, Copenhagen, Denmark, June 9-11, 1993, John Williams (Ed.). ACM, 223–232. https://doi.org/10.1145/165180.165214 Google ScholarGoogle ScholarDigital LibraryDigital Library
  24. Daniel Gratzer, G. A. Kavvos, Andreas Nuyts, and Lars Birkedal. 2020. Multimodal Dependent Type Theory. In LICS ’20: 35th Annual ACM/IEEE Symposium on Logic in Computer Science, Saarbrücken, Germany, July 8-11, 2020, Holger Hermanns, Lijun Zhang, Naoki Kobayashi, and Dale Miller (Eds.). ACM, 492–506. https://doi.org/10.1145/3373718.3394736 Google ScholarGoogle ScholarDigital LibraryDigital Library
  25. Martin Hofmann. 1995. Extensional concepts in intensional type theory. University of Edinburgh, Department of Computer Science. Google ScholarGoogle Scholar
  26. Martin Hofmann. 1997. Syntax and Semantics of Dependent Types. In Semantics and Logics of Computation. Cambridge University Press, 79–130. Google ScholarGoogle Scholar
  27. Martin Hofmann. 1999. Semantical Analysis of Higher-Order Abstract Syntax. In 14th Annual IEEE Symposium on Logic in Computer Science, Trento, Italy, July 2-5, 1999. IEEE Computer Society, 204–213. https://doi.org/10.1109/LICS.1999.782616 Google ScholarGoogle Scholar
  28. Jason Z. S. Hu, Brigitte Pientka, and Ulrich Schöpp. 2022. A Category Theoretic View of Contextual Types: from Simple Types to Dependent Types. CoRR, abs/2206.02831 (2022), https://doi.org/10.48550/arXiv.2206.02831 arXiv:2206.02831. Google ScholarGoogle Scholar
  29. Simon Huber. 2016. Cubical Interpretations of Type Theory. Ph. D. Dissertation. University of Gothenburg. Google ScholarGoogle Scholar
  30. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  31. Neil D. Jones, Carsten K. Gomard, and Peter Sestoft. 1993. Partial evaluation and automatic program generation. Prentice Hall. isbn:978-0-13-020249-9 Google ScholarGoogle Scholar
  32. Yukiyoshi Kameyama, Oleg Kiselyov, and Chung-chieh Shan. 2011. Shifting the stage - Staging with delimited control. J. Funct. Program., 21, 6 (2011), 617–662. https://doi.org/10.1017/S0956796811000256 Google ScholarGoogle ScholarDigital LibraryDigital Library
  33. Ambrus Kaposi, Simon Huber, and Christian Sattler. 2019. Gluing for Type Theory. In 4th International Conference on Formal Structures for Computation and Deduction, FSCD 2019, June 24-30, 2019, Dortmund, Germany, Herman Geuvers (Ed.) (LIPIcs, Vol. 131). Schloss Dagstuhl - Leibniz-Zentrum für Informatik, 25:1–25:19. https://doi.org/10.4230/LIPIcs.FSCD.2019.25 Google ScholarGoogle ScholarCross RefCross Ref
  34. Ambrus Kaposi, András Kovács, and Thorsten Altenkirch. 2019. Constructing quotient inductive-inductive types. Proc. ACM Program. Lang., 3, POPL (2019), 2:1–2:24. https://doi.org/10.1145/3290315 Google ScholarGoogle ScholarDigital LibraryDigital Library
  35. Akira Kawata and Atsushi Igarashi. 2019. A Dependently Typed Multi-stage Calculus. In Programming Languages and Systems - 17th Asian Symposium, APLAS 2019, Nusa Dua, Bali, Indonesia, December 1-4, 2019, Proceedings, Anthony Widjaja Lin (Ed.) (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
  36. Oleg Kiselyov. 2014. The Design and Implementation of BER MetaOCaml - System Description. 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, 86–102. https://doi.org/10.1007/978-3-319-07151-0_6 Google ScholarGoogle ScholarCross RefCross Ref
  37. Oleg Kiselyov and Jeremy Yallop. 2022. let (rec) insertion without Effects, Lights or Magic. CoRR, abs/2201.00495 (2022), arXiv:2201.00495. arxiv:2201.00495 Google ScholarGoogle Scholar
  38. András Kovács. 2022. Demo implementation for the paper "Staged Compilation With Two-Level Type Theory". https://doi.org/10.5281/zenodo.6757373 Google ScholarGoogle ScholarDigital LibraryDigital Library
  39. Daniel R. Licata, Ian Orton, Andrew M. Pitts, and Bas Spitters. 2018. Internal Universes in Models of Homotopy Type Theory. In 3rd International Conference on Formal Structures for Computation and Deduction, FSCD 2018, July 9-12, 2018, Oxford, UK, Hélène Kirchner (Ed.) (LIPIcs, Vol. 108). Schloss Dagstuhl - Leibniz-Zentrum für Informatik, 22:1–22:17. https://doi.org/10.4230/LIPIcs.FSCD.2018.22 Google ScholarGoogle ScholarCross RefCross Ref
  40. Andres Löh and José Pedro Magalhães. 2011. Generic programming with indexed functors. In Proceedings of the seventh ACM SIGPLAN workshop on Generic programming, [email protected] 2011, Tokyo, Japan, September 19-21, 2011, Jaakko Järvi and Shin-Cheng Mu (Eds.). ACM, 1–12. https://doi.org/10.1145/2036918.2036920 Google ScholarGoogle ScholarDigital LibraryDigital Library
  41. Saunders Mac Lane. 1998. Categories for the Working Mathematician (2nd ed.). Springer. isbn:0387984038 http://www.amazon.com/exec/obidos/redirect?tag=citeulike07-20&path=ASIN/0387984038 Google ScholarGoogle Scholar
  42. Ian Orton and Andrew M. Pitts. 2016. Axioms for Modelling Cubical Type Theory in a Topos. In 25th EACSL Annual Conference on Computer Science Logic (CSL 2016), Jean-Marc Talbot and Laurent Regnier (Eds.) (Leibniz International Proceedings in Informatics (LIPIcs), Vol. 62). Schloss Dagstuhl–Leibniz-Zentrum fuer Informatik, Dagstuhl, Germany. 24:1–24:19. isbn:978-3-95977-022-4 issn:1868-8969 https://doi.org/10.4230/LIPIcs.CSL.2016.24 Google ScholarGoogle ScholarCross RefCross Ref
  43. Pierre-Marie Pédrot and Nicolas Tabareau. 2020. The fire triangle: how to mix substitution, dependent elimination, and effects. Proc. ACM Program. Lang., 4, POPL (2020), 58:1–58:28. https://doi.org/10.1145/3371126 Google ScholarGoogle ScholarDigital LibraryDigital Library
  44. Loïc Pujet and Nicolas Tabareau. 2022. Observational equality: now for good. Proc. ACM Program. Lang., 6, POPL (2022), 1–27. https://doi.org/10.1145/3498693 Google ScholarGoogle ScholarDigital LibraryDigital Library
  45. Tim Sheard and Simon L. Peyton Jones. 2002. Template meta-programming for Haskell. ACM SIGPLAN Notices, 37, 12 (2002), 60–75. https://doi.org/10.1145/636517.636528 Google ScholarGoogle ScholarDigital LibraryDigital Library
  46. Jonathan Sterling. 2021. First Steps in Synthetic Tait Computability. Ph. D. Dissertation. Carnegie Mellon University Pittsburgh, PA. Google ScholarGoogle Scholar
  47. Jonathan Sterling and Carlo Angiuli. 2021. Normalization for Cubical Type Theory. In 36th Annual ACM/IEEE Symposium on Logic in Computer Science, LICS 2021, Rome, Italy, June 29 - July 2, 2021. IEEE, 1–15. https://doi.org/10.1109/LICS52264.2021.9470719 Google ScholarGoogle ScholarDigital LibraryDigital Library
  48. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  49. Vladimir Voevodsky. 2013. A simple type system with two identity types. Unpublished note Google ScholarGoogle Scholar
  50. Théo Winterhalter, Matthieu Sozeau, and Nicolas Tabareau. 2019. Eliminating reflection from type theory. In Proceedings of the 8th ACM SIGPLAN International Conference on Certified Programs and Proofs, CPP 2019, Cascais, Portugal, January 14-15, 2019, Assia Mahboubi and Magnus O. Myreen (Eds.). ACM, 91–103. https://doi.org/10.1145/3293880.3294095 Google ScholarGoogle ScholarDigital LibraryDigital Library
  51. 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), 1–30. https://doi.org/10.1145/3498723 Google ScholarGoogle ScholarDigital LibraryDigital Library
  52. Jeremy Yallop, Tamara von Glehn, and Ohad Kammar. 2018. Partially-static data as free extension of algebras. Proc. ACM Program. Lang., 2, ICFP (2018), 100:1–100:30. https://doi.org/10.1145/3236795 Google ScholarGoogle ScholarDigital LibraryDigital Library

Index Terms

  1. Staged compilation with two-level type theory

      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

      • Article Metrics

        • Downloads (Last 12 months)425
        • Downloads (Last 6 weeks)50

        Other Metrics

      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!