Abstract
Type classes and type families are key ingredients in Haskell programming. Type classes were introduced to deal with ad-hoc polymorphism, although with the introduction of functional dependencies, their use expanded to type-level programming. Type families also allow encoding type-level functions, but more directly in the form of rewrite rules. In this paper we show that type families are powerful enough to simulate type classes (without overlapping instances), and we provide a formal proof of the soundness and completeness of this simulation. Encoding instance constraints as type families eases the path to proposed extensions to type classes, like closed sets of instances, instance chains, and control over the search procedure. The only feature which type families cannot simulate is elaboration, that is, generating code from the derivation of a rewriting. We look at ways to solve this problem in current Haskell, and propose an extension to allow elaboration during the rewriting phase.
- P. Bahr. Composing and Decomposing Data Types: A Closed Type Families Implementation of Data Types à la Carte. In Proceedings of the 10th ACM SIGPLAN Workshop on Generic Programming, pages 71–82, New York, NY, USA, Aug. 2014. ACM. Google Scholar
Digital Library
- E. Brady. Idris, a general-purpose dependently typed programming language: Design and implementation. Journal of Functional Programming, 23:552–593, 9 2013.Google Scholar
Cross Ref
- D. Devriese and F. Piessens. On the Bright Side of Type Classes: Instance Arguments in Agda. In Proceedings of the 16th ACM SIGPLAN International Conference on Functional Programming, pages 143–155, New York, NY, USA, 2011. ACM. Google Scholar
Digital Library
- I. S. Diatchki. High-level abstractions for low-level programming. PhD thesis, OGI School of Science & Engineering, May 2007.Google Scholar
- A. Dijkstra, G. van den Geest, B. Heeren, and S. D. Swierstra. Modelling Scoped Instances with Constraint Handling Rules. Technical report, Department of Information and Computing Sciences, Utrecht University, 2007.Google Scholar
- R. A. Eisenberg, D. Vytiniotis, S. Peyton Jones, and S. Weirich. Closed Type Families with Overlapping Equations. In Proceedings of the 41st ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages, pages 671–683, New York, NY, USA, 2014. ACM. Google Scholar
Digital Library
- J. Hage. DOMain Specific Type Error Diagnosis (DOMSTED). Technical Report UU-CS-2014-019, Department of Information and Computing Sciences, Utrecht University, 2014.Google Scholar
- B. Heeren and J. Hage. Type class directives. In Proceedings of the 7th International Conference on Practical Aspects of Declarative Languages, pages 253–267, Berlin, Heidelberg, 2005. Springer-Verlag. Google Scholar
Digital Library
- M. Izbicki. A neat trick for partially closed type families. Available at http://izbicki.me/blog/ a-neat-trick-for-partially-closed-type-families, 2014.Google Scholar
- M. Jones. Type classes with functional dependencies. In G. Smolka, editor, Programming Languages and Systems, volume 1782 of Lecture Notes in Computer Science, pages 230–244. Springer Berlin Heidelberg, 2000. Google Scholar
Digital Library
- O. Kiselyov. Stretching type classes. Retrieved from http://okmij. org/ftp/Haskell/TypeClass.html, May 2015.Google Scholar
- O. Kiselyov, R. Lämmel, and K. Schupke. Strongly typed heterogeneous collections. In Proceedings of the 2004 ACM SIGPLAN Workshop on Haskell, pages 96–107, New York, NY, USA, 2004. ACM. Google Scholar
Digital Library
- J. G. Morris and M. P. Jones. Instance chains: type class programming without overlapping instances. In Proceedings of the 15th ACM SIGPLAN International Conference on Functional Programming, pages 375–386, New York, NY, USA, 2010. ACM. Google Scholar
Digital Library
- S. Peyton Jones, M. Jones, and E. Meijer. Type classes: exploring the design space. In Haskell Workshop, 1997.Google Scholar
- T. Schrijvers, M. Sulzmann, S. Peyton Jones, and M. Chakravarty. Towards open type functions for Haskell. In O. Chitil, editor, 19th International Symposium on Implementation and Application of Functional Languages, pages 233–251. Computing Laboratory, University of Kent, 2007.Google Scholar
- A. Serrano, J. Hage, and P. Bahr. Type Families with Class, Type Classes with Family. Technical report. To appear.Google Scholar
- P. J. Stuckey and M. Sulzmann. A theory of overloading. ACM Trans. Program. Lang. Syst., 27(6):1216–1269, Nov. 2005. ISSN 0164-0925. Google Scholar
Digital Library
- M. Sulzmann, G. J. Duck, S. Peyton-Jones, and P. J. Stuckey. Understanding functional dependencies via constraint handling rules. J. Funct. Program., 17(1):83–129, Jan. 2007. ISSN 0956-7968. Google Scholar
Digital Library
- W. Swierstra. Data types à la carte. Journal of Functional Programming, 18(4):423–436, July 2008. Google Scholar
Digital Library
- The Idris Community. Programming in Idris: A Tutorial. Available from http://eb.host.cs.st-andrews.ac.uk/writings/ idris-tutorial.pdf, 2014.Google Scholar
- D. Vytiniotis, S. Peyton Jones, T. Schrijvers, and M. Sulzmann. OutsideIn(X): Modular Type Inference with Local Assumptions. Journal of Functional Programming, 21(4-5):333–412, Sept. 2011. Google Scholar
Digital Library
- P. Wadler and S. Blott. How to Make Ad-hoc Polymorphism Less Ad Hoc. In Proceedings of the 16th ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages, pages 60–76, New York, NY, USA, 1989. ACM. Google Scholar
Digital Library
Index Terms
Type families with class, type classes with family
Recommendations
Type families with class, type classes with family
Haskell '15: Proceedings of the 2015 ACM SIGPLAN Symposium on HaskellType classes and type families are key ingredients in Haskell programming. Type classes were introduced to deal with ad-hoc polymorphism, although with the introduction of functional dependencies, their use expanded to type-level programming. Type ...
Constrained type families
We present an approach to support partiality in type-level computation without compromising expressiveness or type safety. Existing frameworks for type-level computation either require totality or implicitly assume it. For example, type families in ...
Bidirectional type class instances
Haskell 2019: Proceedings of the 12th ACM SIGPLAN International Symposium on HaskellGADTs were introduced in Haskell’s eco-system more than a decade ago, but their interaction with several mainstream features such as type classes and functional dependencies has a lot of room for improvement. More specifically, for some GADTs it can be ...






Comments