One Vote for Type Families in Haskell!

Generalized Algebraic DataTypes (GADTs) allow programmers to capture important invariants of their data structures through type annotations on data constructors. However when working with GADTs, it is often difficult to concisely and precisely express the way complex data manipulations maintain those invariants. One can approach the problem in a few different ways, given the arsenal of GHC’s current type extensions. One approach is to use type classes with functional dependencies. Another approach is to introduce yet more GADTs to capture the behavior of individual functions. A third alternative is to use type families, a recent introduction of GHC by which functions over types can be defined directly, much like term-level functions, and appear in type signatures. In this paper we illustrate the use of type families in the context of a typepreserving compiler written in Haskell. We compare the results with an ad-hoc, all-GADT solution. We argue that type families promote a more direct programming style that eliminates much code bloat and translates into increased run-time performance. They offer better modularity and require fewer type annotations than type classes, which require that class constraints be propagated between compilation phases. We also describe a use of type families to capture more complex data structure invariants. We mention current limitations that we face with these more advanced uses, in which we need to convince the type checker that the type families we define satisfy certain properties. We sketch a proposal of a language extension to directly support such properties. ?Universite de Montreal, C.P. 6128, succ. Centre-Ville, Montreal, Quebec, Canada. ++1 (514) 343-6111 x3545; {guillelj,monnier}@iro.umontreal.ca

[1]  Conor McBride Faking it: Simulating dependent types in Haskell , 2002, J. Funct. Program..

[2]  Fairouz Kamareddine,et al.  Reviewing the Classical and the de Bruijn Notation for [lambda]-calculus and Pure Type Systems , 2001, J. Log. Comput..

[3]  Zhong Shao,et al.  A type system for certi .ed binaries , 2002, Foundations of Intrusion Tolerant Systems, 2003 [Organically Assured and Survivable Information Systems].

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

[5]  James Cheney,et al.  First-Class Phantom Types , 2003 .

[6]  Tim Sheard,et al.  WHAT IS A PROOF , 2005 .

[7]  Stefan Monnier,et al.  Type-Safe Code Transformations in Haskell , 2007, Electron. Notes Theor. Comput. Sci..

[8]  Gang Chen,et al.  Guarded recursive datatype constructors , 2003, POPL '03.

[9]  Andrew W. Appel,et al.  Compiling with Continuations , 1991 .

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

[11]  Emir Pasalic,et al.  Meta-programming With Built-in Type Equality , 2008, Electron. Notes Theor. Comput. Sci..

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

[13]  Chiyan Chen,et al.  Implementing Cut Elimination: A Case Study of Simulating Dependent Types in Haskell , 2004, PADL.

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

[15]  Simon Peyton Jones,et al.  Towards open type functions for Haskell , 2007 .

[16]  Stefan Monnier,et al.  A type-preserving closure conversion in haskell , 2007, Haskell '07.

[17]  Frank Pfenning,et al.  A Coverage Checking Algorithm for LF , 2003, TPHOLs.

[18]  Stefan Monnier,et al.  A type-preserving compiler in Haskell , 2008, ICFP.

[19]  Carsten Sch A Meta Logical Framework Based on Realizability , 2000 .

[20]  Stefan Monnier The swiss coercion , 2007, PLPV '07.

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