skip to main content

Type-level programming with match types

Published:12 January 2022Publication History
Skip Abstract Section

Abstract

Type-level programming is becoming more and more popular in the realm of functional programming. However, the combination of type-level programming and subtyping remains largely unexplored in practical programming languages. This paper presents match types, a type-level equivalent of pattern matching. Match types integrate seamlessly into programming languages with subtyping and, despite their simplicity, offer significant additional expressiveness. We formalize the feature of match types in a calculus based on System F sub and prove its soundness. We practically evaluate our system by implementing match types in the Scala 3 reference compiler, thus making type-level programming readily available to a broad audience of programmers.

Skip Supplemental Material Section

Supplemental Material

Auxiliary Presentation Video

Type-level programming is becoming more and more popular in the realm of functional programming. However, the combination of type-level programming and subtyping remains largely unexplored in practical programming languages. This paper presents \emph{match types}, a type-level equivalent of pattern matching. Match types integrate seamlessly into programming languages with subtyping and, despite their simplicity, offer significant additional expressiveness. We formalize the feature of match types in a calculus based on System F sub and prove its soundness. We practically evaluate our system by implementing match types in the Scala 3 reference compiler, thus making type-level programming readily available to a broad audience of programmers.

References

  1. Martín Abadi, Luca Cardelli, Benjamin Pierce, and Gordon Plotkin. 1991. Dynamic Typing in a Statically Typed Language. ACM Trans. Program. Lang. Syst., 13, 2 (1991), April, 237–268. issn:0164-0925 https://doi.org/10.1145/103135.103138 Google ScholarGoogle ScholarDigital LibraryDigital Library
  2. Nada Amin, Samuel Grütter, Martin Odersky, Tiark Rompf, and Sandro Stucki. 2016. The Essence of Dependent Object Types. Springer International Publishing, Cham. 249–272. isbn:978-3-319-30936-1 https://doi.org/10.1007/978-3-319-30936-1_14 Google ScholarGoogle ScholarCross RefCross Ref
  3. David Aspinall. 1995. Subtyping with singleton types. In International Workshop on Computer Science Logic. Springer Berlin Heidelberg, Berlin, Heidelberg. 1–15. https://doi.org/10.1007/BFb0022243 Google ScholarGoogle ScholarCross RefCross Ref
  4. Brian Aydemir, Arthur Charguéraud, Benjamin C. Pierce, Randy Pollack, and Stephanie Weirich. 2008. Engineering Formal Metatheory. In Proceedings of the 35th Annual ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages (POPL’08). Association for Computing Machinery, New York, NY, USA. 3–15. isbn:9781595936899 https://doi.org/10.1145/1328438.1328443 Google ScholarGoogle ScholarDigital LibraryDigital Library
  5. Paul Barham and Michael Isard. 2019. Machine Learning Systems Are Stuck in a Rut. In Proceedings of the Workshop on Hot Topics in Operating Systems (HotOS ’19). ACM, New York, NY, USA. 177–183. isbn:978-1-4503-6727-1 https://doi.org/10.1145/3317550.3321441 Google ScholarGoogle ScholarDigital LibraryDigital Library
  6. Gavin Bierman, Martín Abadi, and Mads Torgersen. 2014. Understanding TypeScript. In Proceedings of the 28th European Conference on ECOOP 2014 — Object-Oriented Programming - Volume 8586. Springer-Verlag, Berlin, Heidelberg. 257–281. isbn:9783662442012 https://doi.org/10.1007/978-3-662-44202-9_11 Google ScholarGoogle ScholarDigital LibraryDigital Library
  7. Olivier Blanvillain, Jonathan Brachthäuser, Maxime Kjaer, and Martin Odersky. 2021. Type-Level Programming with Match Types. 70. http://infoscience.epfl.ch/record/290019Google ScholarGoogle Scholar
  8. Olivier Blanvillain, Jonathan Brachthäuser, Maxime Kjaer, and Martin Odersky. 2021. Type-Level Programming with Match Types Artifact. https://doi.org/10.5281/zenodo.5568850 Google ScholarGoogle ScholarDigital LibraryDigital Library
  9. Olivier Blanvillain, Marios Iliofotou, Adelbert Chang, Gleb Kanterov, and other open-source contributors. 2016–2021. Frameless. https://github.com/typelevel/framelessGoogle ScholarGoogle Scholar
  10. Luca Cardelli, Simone Martini, John C Mitchell, and Andre Scedrov. 1994. An extension of system F with subtyping. Information and computation, 109, 1-2 (1994), 4–56. https://doi.org/10.1006/inco.1994.1013 Google ScholarGoogle ScholarDigital LibraryDigital Library
  11. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  12. Tongfei Chen. 2017. Typesafe Abstractions for Tensor Operations (Short Paper). In Proceedings of the 8th ACM SIGPLAN International Symposium on Scala (SCALA 2017). Association for Computing Machinery, New York, NY, USA. 45–50. isbn:9781450355292 https://doi.org/10.1145/3136000.3136001 Google ScholarGoogle ScholarDigital LibraryDigital Library
  13. Judicaël Courant. 2003. Strong normalization with singleton types. Electronic Notes in Theoretical Computer Science, 70, 1 (2003), 53–71. https://doi.org/10.1016/S1571-0661(04)80490-0 Google ScholarGoogle ScholarCross RefCross Ref
  14. Richard A Eisenberg. 2016. Dependent types in Haskell: Theory and practice. University of Pennsylvania.Google ScholarGoogle Scholar
  15. Richard A. Eisenberg, Dimitrios Vytiniotis, Simon Peyton Jones, and Stephanie Weirich. 2014. Closed Type Families with Overlapping Equations. In Proceedings of the 41st ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages (POPL’14). Association for Computing Machinery, New York, NY, USA. 671–683. isbn:9781450325448 https://doi.org/10.1145/2535838.2535856 Google ScholarGoogle ScholarDigital LibraryDigital Library
  16. Burak Emir, Martin Odersky, and John Williams. 2007. Matching Objects with Patterns. In Proceedings of the 21st European Conference on Object-Oriented Programming (ECOOP’07). Springer-Verlag, Berlin, Heidelberg. 273–298. isbn:3540735887 https://doi.org/10.1007/978-3-540-73589-2_14 Google ScholarGoogle ScholarCross RefCross Ref
  17. Paolo G. Giarrusso, Léo Stefanesco, Amin Timany, Lars Birkedal, and Robbert Krebbers. 2020. Scala Step-by-Step: Soundness for DOT with Step-Indexed Logical Relations in Iris. Proc. ACM Program. Lang., 4, ICFP (2020), Article 114, 29 pages. https://doi.org/10.1145/3408996 Google ScholarGoogle ScholarDigital LibraryDigital Library
  18. Robert Harper and Greg Morrisett. 1995. Compiling Polymorphism Using Intensional Type Analysis. In Proceedings of the 22nd ACM SIGPLAN-SIGACT Symposium on Principle s of Programming Languages (POPL ’95). Association for Computing Machinery, New York, NY, USA. 130–141. isbn:0897916921 https://doi.org/10.1145/199448.199475 Google ScholarGoogle ScholarDigital LibraryDigital Library
  19. Charles R. Harris, K. Jarrod Millman, Stéfan J. van der Walt, Ralf Gommers, Pauli Virtanen, David Cournapeau, Eric Wieser, Julian Taylor, Sebastian Berg, Nathaniel J. Smith, Robert Kern, Matti Picus, Stephan Hoyer, Marten H. van Kerkwijk, Matthew Brett, Allan Haldane, Jaime Fernández del Río, Mark Wiebe, Pearu Peterson, Pierre Gérard-Marchant, Kevin Sheppard, Tyler Reddy, Warren Weckesser, Hameer Abbasi, Christoph Gohlke, and Travis E. Oliphant. 2020. Array programming with NumPy. Nature, 585, 7825 (2020), Sept., 357–362. https://doi.org/10.1038/s41586-020-2649-2 Google ScholarGoogle Scholar
  20. Austin Huang, Sam Stites, and Torsten Scholak. 2017–2021. HaskTorch. https://github.com/hasktorch/hasktorchGoogle ScholarGoogle Scholar
  21. DeLesley S. Hutchins. 2010. Pure Subtype Systems. In Proceedings of the 37th Annual ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages (POPL’10). Association for Computing Machinery, New York, NY, USA. 287–298. isbn:9781605584799 https://doi.org/10.1145/1706299.1706334 Google ScholarGoogle ScholarDigital LibraryDigital Library
  22. Oleg Kiselyov, Ralf Lämmel, and Keean Schupke. 2004. Strongly Typed Heterogeneous Collections. In Proceedings of the 2004 ACM SIGPLAN Workshop on Haskell (Haskell ’04). Association for Computing Machinery, New York, NY, USA. 96–107. isbn:1581138504 https://doi.org/10.1145/1017472.1017488 Google ScholarGoogle ScholarDigital LibraryDigital Library
  23. George Leontiev, Eugene Burmako, Jason Zaugg, Adriaan Moors, Paul Phillips, Oron Port, and Miles Sabin. 2014. SIP-23 - Literal-Based Singleton Types. https://docs.scala-lang.org/sips/42.type.htmlGoogle ScholarGoogle Scholar
  24. Fengyun Liu. 2016. A Generic Algorithm for Checking Exhaustivity of Pattern Matching (Short Paper). In Proceedings of the 2016 7th ACM SIGPLAN Symposium on Scala (SCALA 2016). Association for Computing Machinery, New York, NY, USA. 61–64. isbn:9781450346481 https://doi.org/10.1145/2998392.2998401 Google ScholarGoogle ScholarDigital LibraryDigital Library
  25. Abel Nieto, Yaoyu Zhao, Ondřej Lhoták, Angela Chang, and Justin Pu. 2020. Scala with Explicit Nulls. In 34th European Conference on Object-Oriented Programming (ECOOP 2020), Robert Hirschfeld and Tobias Pape (Eds.) (Leibniz International Proceedings in Informatics (LIPIcs), Vol. 166). Schloss Dagstuhl–Leibniz-Zentrum für Informatik, Dagstuhl, Germany. 25:1–25:26. isbn:978-3-95977-154-2 issn:1868-8969 https://doi.org/10.4230/LIPIcs.ECOOP.2020.25 Google ScholarGoogle ScholarCross RefCross Ref
  26. Benjamin C. Pierce. 2002. Types and programming languages. MIT press. isbn:0262162091Google ScholarGoogle Scholar
  27. Michael Pilquist and Scodec open-source contributors. 2013–2021. Scodec. https://github.com/scodec/scodecGoogle ScholarGoogle Scholar
  28. Marianna Rapoport, Ifaz Kabir, Paul He, and Ondřej Lhoták. 2017. A Simple Soundness Proof for Dependent Object Types. Proc. ACM Program. Lang., 1, OOPSLA (2017), Article 46, Oct., 27 pages. https://doi.org/10.1145/3133870 Google ScholarGoogle ScholarDigital LibraryDigital Library
  29. Alexander Rush. 2019. Tensor Considered Harmful. https://nlp.seas.harvard.edu/NamedTensorGoogle ScholarGoogle Scholar
  30. Miles Sabin and Shapeless open-source contributors. 2011–2021. Shapeless. https://github.com/milessabin/shapelessGoogle ScholarGoogle Scholar
  31. Michel Schinz. 2005. Compiling Scala for the Java virtual machine. Ph.D. Dissertation. EPFL. Lausanne. https://doi.org/10.5075/epfl-thesis-3302 Google ScholarGoogle ScholarCross RefCross Ref
  32. Tom Schrijvers, Simon Peyton Jones, Manuel Chakravarty, and Martin Sulzmann. 2008. Type Checking with Open Type Functions. In Proceedings of the 13th ACM SIGPLAN International Conference on Functional Programming (ICFP’08). Association for Computing Machinery, New York, NY, USA. 51–62. isbn:9781595939197 https://doi.org/10.1145/1411204.1411215 Google ScholarGoogle ScholarDigital LibraryDigital Library
  33. Vilhelm Sjoberg. 2015. A Dependently Typed Language with Nontermination. Ph.D. Dissertation. University of Pennsylvania.Google ScholarGoogle Scholar
  34. Christopher A. Stone and Robert Harper. 2000. Deciding Type Equivalence in a Language with Singleton Kinds. In Proceedings of the 27th ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages (POPL’00). Association for Computing Machinery, New York, NY, USA. 214–227. isbn:1581131259 https://doi.org/10.1145/325694.325724 Google ScholarGoogle ScholarDigital LibraryDigital Library
  35. TypeScript development team. 2020. The TypeScript Handbook. https://www.typescriptlang.org/Google ScholarGoogle Scholar
  36. Stephanie Weirich, Dimitrios Vytiniotis, Simon Peyton Jones, and Steve Zdancewic. 2011. Generative Type Abstraction and Type-Level Computation. In Proceedings of the 38th Annual ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages (POPL’11). Association for Computing Machinery, New York, NY, USA. 227–240. isbn:9781450304900 https://doi.org/10.1145/1926385.1926411 Google ScholarGoogle ScholarDigital LibraryDigital Library
  37. Yanpeng Yang and Bruno C. d. S. Oliveira. 2017. Unifying Typing and Subtyping. Proc. ACM Program. Lang., 1, OOPSLA (2017), Article 47, 26 pages. https://doi.org/10.1145/3133871 Google ScholarGoogle ScholarDigital LibraryDigital Library
  38. Jan Zwanenburg. 1999. Pure type systems with subtyping. In International Conference on Typed Lambda Calculi and Applications. Springer Berlin Heidelberg, Berlin, Heidelberg. 381–396. https://doi.org/10.1007/3-540-48959-2_27 Google ScholarGoogle ScholarCross RefCross Ref

Index Terms

  1. Type-level programming with match types

            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

            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!