Bidirectional Type Class Instances (Extended Version)

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]  Bjarne Stroustrup,et al.  Concepts: linguistic support for generic programming in C++ , 2006, OOPSLA '06.

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

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

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

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

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

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

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

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

[10]  Simon L. Peyton Jones,et al.  Type Classes in Haskell , 1994, ESOP.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

[36]  Hugo Herbelin,et al.  The Coq proof assistant : reference manual, version 6.1 , 1997 .

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