Bidirectional type class instances

GADTs 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 surprisingly difficult to provide an instance for even the simplest of type classes. In this paper we identify the source of this shortcoming and address it by introducing a conservative extension to Haskell’s type classes: Bidirectional Type Class Instances. In essence, under our interpretation class instances correspond to logical bi-implications, in contrast to their traditional unidirectional interpretation. We present a fully-fledged design of bidirectional instances, covering the specification of typing and elaboration into System FC, as well as an algorithm for type inference and elaboration. We provide a proof-of-concept implementation of our algorithm, and revisit the meta-theory of type classes in the presence of our extension.

[1]  Simon L. Peyton Jones,et al.  Type checking with open type functions , 2008, ICFP.

[2]  Patricia Johann,et al.  Foundations for structured programming with GADTs , 2008, POPL '08.

[3]  Richard A. Eisenberg,et al.  Constrained type families , 2017, Proc. ACM Program. Lang..

[4]  Mark P. Jones Qualified Types: Theory and Practice , 1994 .

[5]  Giuseppe Peano,et al.  Arithmetices Principia Novo Methodo Exposita , 1889 .

[6]  Peyton Jones,et al.  Haskell 98 language and libraries : the revised report , 2003 .

[7]  Mark P. Jones,et al.  Instance chains: type class programming without overlapping instances , 2010, ICFP '10.

[8]  Tom Schrijvers,et al.  Coherence of type class resolution , 2019, Proc. ACM Program. Lang..

[9]  Philip Wadler,et al.  How to make ad-hoc polymorphism less ad hoc , 1989, POPL '89.

[10]  Bjarne Stroustrup,et al.  Concepts: linguistic support for generic programming in C++ , 2006, OOPSLA '06.

[11]  Mark P. Jones,et al.  Type Classes with Functional Dependencies , 2000, ESOP.

[12]  John C. Reynolds,et al.  Towards a theory of type structure , 1974, Symposium on Programming.

[13]  Mark P. Jones A Theory of Qualified Types , 1992, ESOP.

[14]  Fritz Henglein,et al.  Type inference with polymorphic recursion , 1993, TOPL.

[15]  Bruno C. d. S. Oliveira,et al.  Quantified class constraints , 2017, Haskell.

[16]  Simon L. Peyton Jones,et al.  OutsideIn(X) Modular type inference with local assumptions , 2011, J. Funct. Program..

[17]  Robin Milner,et al.  Principal type-schemes for functional programs , 1982, POPL '82.

[18]  Simon L. Peyton Jones,et al.  Understanding functional dependencies via constraint handling rules , 2007, J. Funct. Program..

[19]  Simon L. Peyton Jones,et al.  Closed type families with overlapping equations , 2014, POPL.

[20]  Henk Barendregt,et al.  The Lambda Calculus: Its Syntax and Semantics , 1985 .

[21]  Mark P. Jones Simplifying and improving qualified types , 1995, FPCA '95.

[22]  Simon L. Peyton Jones,et al.  Associated types with class , 2005, POPL '05.

[23]  Simon L. Peyton Jones,et al.  Simple unification-based type inference for GADTs , 2006, ICFP '06.

[24]  R. Hindley The Principal Type-Scheme of an Object in Combinatory Logic , 1969 .

[25]  Simon L. Peyton Jones,et al.  Hoopl: a modular, reusable library for dataflow analysis and transformation , 2010, Haskell.

[26]  J. Y. Girard,et al.  Interpretation fonctionelle et elimination des coupures dans l'aritmetique d'ordre superieur , 1972 .

[27]  John C. Reynolds,et al.  Types, Abstraction and Parametric Polymorphism , 1983, IFIP Congress.

[28]  Simon J. E. Taylor,et al.  The Mercury Language Reference Manual , 2016 .

[29]  Robert A. Kowalski,et al.  Predicate Logic as Programming Language , 1974, IFIP Congress.

[30]  Simon L. Peyton Jones,et al.  System F with type equality coercions , 2007, TLDI '07.

[31]  J. Garrett Morris A simple semantics for Haskell overloading , 2014, Haskell '14.

[32]  Simon L. Peyton Jones,et al.  Associated type synonyms , 2005, ICFP '05.

[33]  Simon L. Peyton Jones,et al.  Giving Haskell a promotion , 2012, TLDI '12.

[34]  Christine Paulin-Mohring,et al.  The coq proof assistant reference manual , 2000 .

[35]  Stephanie Weirich,et al.  Closed Type Families with Overlapping Equations (extended version) , 2013 .

[36]  Tom Schrijvers,et al.  Elaboration on functional dependencies: functional dependencies are dead, long live functional dependencies! , 2017, Haskell.

[37]  Simon L. Peyton Jones,et al.  Type classes in Haskell , 1994, TOPL.