skip to main content
research-article

Injective type families for Haskell

Published:30 August 2015Publication History
Skip Abstract Section

Abstract

Haskell, as implemented by the Glasgow Haskell Compiler (GHC), allows expressive type-level programming. The most popular type-level programming extension is TypeFamilies, which allows users to write functions on types. Yet, using type functions can cripple type inference in certain situations. In particular, lack of injectivity in type functions means that GHC can never infer an instantiation of a type variable appearing only under type functions. In this paper, we describe a small modification to GHC that allows type functions to be annotated as injective. GHC naturally must check validity of the injectivity annotations. The algorithm to do so is surprisingly subtle. We prove soundness for a simplification of our algorithm, and state and prove a completeness property, though the algorithm is not fully complete. As much of our reasoning surrounds functions defined by a simple pattern-matching structure, we believe our results extend beyond just Haskell. We have implemented our solution on a branch of GHC and plan to make it available to regular users with the next stable release of the compiler.

References

  1. F. Baader and T. Nipkow. Term Rewriting and All That. Cambridge University Press, New York, NY, USA, 1998. ISBN 0-521-45520-0. E. Brady. Idris, a general-purpose dependently typed programming language: Design and implementation. Journal of Functional Programming, 23(5): 552–593, Sept. 2013. Google ScholarGoogle ScholarDigital LibraryDigital Library
  2. J. Breitner, R. A. Eisenberg, S. Peyton Jones, and S. Weirich. Safe zero-cost coercions for Haskell. In ACM SIGPLAN International Conference on Functional Programming, pages 189–202, Sept. 2014. Google ScholarGoogle ScholarDigital LibraryDigital Library
  3. M. M. T. Chakravarty, G. Keller, and S. Peyton Jones. Associated type synonyms. In ACM SIGPLAN International Conference on Functional Programming, 2005a. M. M. T. Chakravarty, G. Keller, S. Peyton Jones, and S. Marlow. Associated types with class. In ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages, 2005b. J. Cheney and R. Hinze. First-class phantom types. Technical report, Cornell University, 2003. Google ScholarGoogle ScholarDigital LibraryDigital Library
  4. R. A. Eisenberg, D. Vytiniotis, S. Peyton Jones, and S. Weirich. Closed type families with overlapping equations. In ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages, 2014. Google ScholarGoogle ScholarDigital LibraryDigital Library
  5. M. P. Jones. Simplifying and improving qualified types. In ACM Conference on Functional Programming and Computer Architecture, 1995. Google ScholarGoogle ScholarDigital LibraryDigital Library
  6. M. P. Jones. Type classes with functional dependencies. In European Symposium on Programming, 2000. Google ScholarGoogle ScholarDigital LibraryDigital Library
  7. M. P. Jones. Language and program design for functional dependencies. In ACM Haskell Symposium. ACM, 2008. Google ScholarGoogle ScholarDigital LibraryDigital Library
  8. S. Lindley and C. McBride. Hasochism: the pleasure and pain of dependently typed Haskell programming. In ACM SIGPLAN Haskell Symposium, pages 81–92, 2013.. N. Nishida and M. Sakai. Proving injectivity of functions via program inversion in term rewriting. In M. Blume, N. Kobayashi, and G. Vidal, editors, Functional and Logic Programming, volume 6009 of Lecture Notes in Computer Science, pages 288–303. Springer, 2010. Google ScholarGoogle ScholarDigital LibraryDigital Library
  9. U. Norell. Towards a practical programming language based on dependent type theory. PhD thesis, Chalmers University of Technology, 2007.Google ScholarGoogle Scholar
  10. S. Peyton Jones, D. Vytiniotis, S. Weirich, and G. Washburn. Simple unification-based type inference for GADTs. In ACM SIGPLAN International Conference on Functional Programming, 2006. Google ScholarGoogle ScholarDigital LibraryDigital Library
  11. A. Serrano Mena. Branching, disjointness and injectivity for OutsideIn(X). ICFP 2014 poster, Sept. 2014.Google ScholarGoogle Scholar
  12. J. Stolarek, S. Peyton Jones, and R. A. Eisenberg. Injective type families for Haskell (extended version). Technical report, Politechnika Łódzka, 2015. URL http://ics.p.lodz.pl/~stolarek/_media/pl:research: stolarek_peyton-jones_eisenberg_injectivity_extended. pdf. M. Sulzmann, G. Duck, S. Peyton Jones, and P. Stuckey. Understanding functional dependencies via constraint handling rules. Journal of Functional Programming, 17:83–129, 2007. Google ScholarGoogle ScholarDigital LibraryDigital Library
  13. The Coq development team. The Coq Proof Assistant reference manual, version 8.4. Technical report, TypiCal Project (formerly LogiCal), Nov. 2014.Google ScholarGoogle Scholar
  14. 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
  15. B. Yorgey, S. Weirich, J. Cretin, S. Peyton Jones, D. Vytiniotis, and J. P. Magalhães. Giving Haskell a promotion. In ACM SIGPLAN Workshop on Types in Language Design and Implementation, 2012. Google ScholarGoogle ScholarDigital LibraryDigital Library

Index Terms

  1. Injective type families for Haskell

            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!