skip to main content
research-article

Type families with class, type classes with family

Published:30 August 2015Publication History
Skip Abstract Section

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.

References

  1. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  2. E. Brady. Idris, a general-purpose dependently typed programming language: Design and implementation. Journal of Functional Programming, 23:552–593, 9 2013.Google ScholarGoogle ScholarCross RefCross Ref
  3. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  4. I. S. Diatchki. High-level abstractions for low-level programming. PhD thesis, OGI School of Science & Engineering, May 2007.Google ScholarGoogle Scholar
  5. 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 ScholarGoogle Scholar
  6. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  7. J. Hage. DOMain Specific Type Error Diagnosis (DOMSTED). Technical Report UU-CS-2014-019, Department of Information and Computing Sciences, Utrecht University, 2014.Google ScholarGoogle Scholar
  8. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  9. 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 ScholarGoogle Scholar
  10. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  11. O. Kiselyov. Stretching type classes. Retrieved from http://okmij. org/ftp/Haskell/TypeClass.html, May 2015.Google ScholarGoogle Scholar
  12. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  13. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  14. S. Peyton Jones, M. Jones, and E. Meijer. Type classes: exploring the design space. In Haskell Workshop, 1997.Google ScholarGoogle Scholar
  15. 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 ScholarGoogle Scholar
  16. A. Serrano, J. Hage, and P. Bahr. Type Families with Class, Type Classes with Family. Technical report. To appear.Google ScholarGoogle Scholar
  17. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  18. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  19. W. Swierstra. Data types à la carte. Journal of Functional Programming, 18(4):423–436, July 2008. Google ScholarGoogle ScholarDigital LibraryDigital Library
  20. The Idris Community. Programming in Idris: A Tutorial. Available from http://eb.host.cs.st-andrews.ac.uk/writings/ idris-tutorial.pdf, 2014.Google ScholarGoogle Scholar
  21. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  22. 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 ScholarGoogle ScholarDigital LibraryDigital Library

Index Terms

  1. Type families with class, type classes with family

        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!