Bidirectional Type Class Instances (Extended Version)

Bidirectional Type Class Instances (Extended Version)

Bidirectional Type Class Instances (Extended Version) Koen Pauwels Georgios Karachalias KU Leuven KU Leuven Belgium Belgium [email protected] [email protected] Michiel Derhaeg Tom Schrijvers Guardsquare KU Leuven Belgium Belgium [email protected] [email protected] Abstract New York, NY, USA, Article 1, 17 pages. https://doi.org/10.1145/ GADTs were introduced in Haskell’s eco-system more than 3331545.3342596 a decade ago, but their interaction with several mainstream features such as type classes and functional dependencies 1 Introduction has a lot of room for improvement. More specifically, for Type classes were first introduced by Wadler and Blott[1989] some GADTs it can be surprisingly difficult to provide an as a principled way to support ad-hoc polymorphism in instance for even the simplest of type classes. Haskell, have since appeared in other declarative languages In this paper we identify the source of this shortcoming like Coq [The Coq development team 2004] and Mercury and address it by introducing a conservative extension to [Henderson et al. 1996], and have influenced the design of Haskell’s type classes: Bidirectional Type Class Instances. In similar features (e.g., concepts for C++ [Gregor et al. 2006]). essence, under our interpretation class instances correspond One of the main reasons type classes have been so suc- to logical bi-implications, in contrast to their traditional uni- cessful is that they support sound, decidable, and efficient directional interpretation. type inference [Jones 1992], while being a simple exten- We present a fully-fledged design of bidirectional instances, sion of the well-understood Hindley-Damas-Milner system covering the specification of typing and elaboration into (HM) [Damas and Milner 1982; Hindley 1969]. Furthermore, System FC, as well as an algorithm for type inference and as Wadler and Blott[1989] have shown, they can be straight- elaboration. We provide a proof-of-concept implementation forwardly translated to parametric polymorphism in inter- of our algorithm, and revisit the meta-theory of type classes mediate languages akin to System F [Girard 1972; Reynolds in the presence of our extension. 1974, 1983a]. Since the conception of type classes, instances have been CCS Concepts • Theory of computation → Type the- interpreted as logical implications, due to their straightfor- ory; • Software and its engineering → Functional lan- ward implementation as System F functions. For example, guages. the well-known equality instance for lists Keywords Haskell, type classes, type inference, elaboration instance Eqa ) Eq »a¼ ACM Reference Format: can be read as “ if a is an instance of Eq, then so is »a¼”. This Koen Pauwels, Georgios Karachalias, Michiel Derhaeg, and Tom interpretation has worked pretty well so far, but falls short arXiv:1906.12242v2 [cs.PL] 1 Jul 2019 Schrijvers. 2019. Bidirectional Type Class Instances (Extended Ver- in the presence of advanced features such as Generalized sion). In Proceedings of the 12th ACM SIGPLAN International Haskell Algebraic Data Types (GADTs) [Peyton Jones et al. 2006]. Symposium (Haskell ’19), August 22–23, 2019, Berlin, Germany. ACM, More specifically, with the current interpretation of type classes a large class of GADTs cannot be made an instance Permission to make digital or hard copies of all or part of this work for of the simplest of type classes. In this work we alleviate this personal or classroom use is granted without fee provided that copies are not problem by introducing a conservative extension1 to Haskell: made or distributed for profit or commercial advantage and that copies bear this notice and the full citation on the first page. Copyrights for components Bidirectional Instances. Under our interpretation, instances of this work owned by others than ACM must be honored. Abstracting with like the above can be read as “a is an instance of Eq if and credit is permitted. To copy otherwise, or republish, to post on servers or to only if »a¼ is an instance of Eq”.2 redistribute to lists, requires prior specific permission and/or a fee. Request permissions from [email protected]. 1By “conservative”, we mean that our system can type strictly more pro- Haskell ’19, August 22–23, 2019, Berlin, Germany grams than plain Haskell does. © 2019 Association for Computing Machinery. 2 Which we believe reflects what Haskell users already have in mind. Indeed, ACM ISBN 978-1-4503-6813-1/19/08...$15.00 most prior research on type classes—such as the work of Sulzmann et al. https://doi.org/10.1145/3331545.3342596 [2007b]—treat Eqa and Eq »a¼ as denotationally equivalent. Haskell ’19, August 22–23, 2019, Berlin, Germany K. Pauwels, G. Karachalias, M. Derhaeg, and T. Schrijvers The main problem is that, unlike basic type classes, this The GADT Term encodes a simple expression language, with extension requires a non-parametric encoding (which possi- constants (constructed by data constructor Con) and tuples bly explains its initial omission). We overcome this problem (constructed by data constructor Tup). with System FC coercions [Sulzmann et al. 2007a] and add Making (Terma ) an instance of even the simplest of type a non-parametric witness for the instance context to the classes can be challenging. For example, the following straight- otherwise parametric dictionary representation. Our specific forward instance is not typeable under the current specifica- contributions are: tion of type classes: • We present a detailed overview of the shortcomings instance Showa ) Show ¹Terma º where in the interaction between GADTs and type classes, as show ¹Conx º = showx well as other class-based extensions (Sec.2). show ¹Tupxy º = unwords »"¹"; showx ; ";"; showy ; "º"¼ • We identify the major challenges of interpreting and elaborating type class instances bidirectionally (Sec.3). Loading the above program into ghci emits the following • We describe an elaboration strategy that addresses all error message: such challenges, while making Haskell strictly more Bidirectional.hs:14:33: expressive (Sec.4). Could not deduce (Show b) arising from a use of `show' from the context (Show a) or from (a ~ (b, c)) • We formalize superclasses, an important—yet often neglected—aspect of type classes. Our formalization Bidirectional.hs:14:44: Could not deduce (Show c) arising from a use of `show' includes a specification of typing, elaboration, and an from the context (Show a) or from (a ~ (b, c)) algorithm for type inference with elaboration (Sec.5). As the message indicates, the sources of the errors are the two • We provide a formalization of typing and evidence recursive calls to show in the second clause: the instance con- translation from source terms to System F for type C text ¹Showa º and the local constraint (exposed via GADT pat- classes with bidirectional instances, as well as an algo- tern matching) a ∼ ¹b; cº are not sufficient to prove (Showb ) rithm for type inference with elaboration (Sec.6). Our and (Showc ), which are required by the recursive calls to approach reuses most of the infrastructure needed by show. In summary, the type system cannot derive the follow- superclasses; existing systems need to be minimally ing implications: extended for the additional expressive power. • We elaborate on the changes bidirectional instances b: c: Show ¹b; cº ) Showb induce to the meta-theory of type classes; notably ter- 8b: 8c: Show ¹b; cº ) Showc mination of type inference and principality of types 8 8 (Sec.7). The Problem Both implications above constitute the in- • We provide a prototype implementation of our algo- version of the implication derived by the predefined Show rithm for type inference with elaboration at https: instance for tuples: //github.com/KoenP/bidirectional-instances. instance ¹Showb ; Showc º ) Show ¹b; cº where f ::: g 2 Motivation Indeed, the interpretation of type classes in existing systems is not bidirectional: the system can only derive Show ¹b; cº 2.1 Structural Induction Over Indexed Data Types from Showb and Showc , but not the other way around. Ever since GADTs were introduced in Haskell [Peyton Jones et al. 2006], they have been put to good use by programmers 2.2 Functional Dependencies and Associated Types for dataflow analysis and optimization [Ramsey et al. 2010], Unfortunately, the lack of bidirectionality of type class in- 3 4 accelerated array processing, automatic differentiation, stances does not only affect the expressive power of simple and much more. Yet, their interaction with existing features type classes, but also the expressive power of features based such as type classes [Wadler and Blott 1989] and functional on them, such as functional dependencies [Jones 2000] and dependencies [Jones 2000] can lead to surprising problems. associated type synonyms [Chakravarty et al. 2005a]. For example, consider (a simplified version of) the Term For example, let us consider an example of type-level pro- datatype, as given by Johann and Ghani[2008]: gramming using functional dependencies.5 First, we define type-level natural numbers and length-indexed vectors: data Term :: ? ! ? where Con :: a ! Terma data Nat :: ? where data Vec :: Nat ! ? ! ? where Tup :: Termb ! Termc ! Term ¹b; cº Z :: Nat VN :: VecZa S :: Nat ! Nat VC :: a ! Vecna ! Vec ¹Sn º a 3https://hackage.haskell.org/package/accelerate 5A similar example has been presented by Hallgren[2000], who imple- 4https://hackage.haskell.org/package/ad mented insertion sort at the level of types using functional dependencies. Bidirectional Type Class Instances (Extended Version) Haskell ’19, August 22–23, 2019, Berlin, Germany On the left, we define type-level natural numbers Nat. Type most part—the same semantics with functional dependen- Nat is automatically promoted into a kind and data construc- cies. Thus, the problem we are presenting here applies to tors Z and S into type constructors of the same name, using associated type families as well; shortcomings of type classes the GHC extension DataKinds [Yorgey et al.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    17 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us