T243 : An algebraic model of Haskell type class resolution

Supervision:
Contact person: Tom Schrijvers
Supervisor(s): Tom Schrijvers
Daily advisor(s): Tom Schrijvers
Research group:
Declarative Languages and Artificial Intelligence
Context:

Type classes are a unique feature of the Haskell programming language to make add-hoc overloading less ad-hoc. The feature was not only a big success in Haskell, but was also adopted by other languages like Coq and Mercury, and influenced the design of similar features in C++ (concepts) and Rust (traits).

Type classes add considerable complexity to the job of the type checker, who has to make sure that the necessary type class instances are available to discharge the type class constraints that emerge from the uses of type class methods. For instance, the expression [1,2] == [3] gives rise to the type class constraint Eq [Int]. This constraint can be satisfied by using the instance Eq a => Eq [a]. However, this instance in turn gives rise to the constraint Eq Int, which can be fully discharged with the instance Eq Int. This (potentially) multi-step process is known as type class resolution and is interleaved with regular type checking.

The resolution process is further complicated by the need to produce a runtime witness. It is after all not enough to check whether an instance is available that satisfies a given constraint. The type checker also produces code that explicitly passes the implemention to be used, which comes in the form of a so-called (function) dictionary with one function field per type class method. The process of turning implicit type class constraints into explicitly passed dictionaries is known as elaboration. It takes place as a (crucial) by-product of type class resolution.

The control flow of type checking with type class resolution and elaboration is rather involved: different steps are interleaved, while the top-down generation of dictionary witnesses takes place. Due to the frequent interruption of the latter, the GHC Haskell compiler resorts to ugly (non-functional) mutable variables to construct the type class dictionaries. 

Aim:

The goal of this work is to come up with a cleaner, more high-level and more functional approach to type class resolution. We have in mind an approach based on algebraic effects. This allows us to model the type checking process based on a number of high-level effectful operations, in the style of Atkey (see literature). In particular, we consider the use of a cooperative multi-threading effect to model the switching between the different type checker tasks. This way we can use functional continuations to replace imperative mutable references.

Possible research questions:
  • Can we use algebraic effects to model type checking with type class resolution?
     
  • Which functional control flow pattern is most suitable for the problem: monads, applicative functors, arrows, ...?
Approach:
  • Study the literature on type class resolution and algebraic effects.
     
  • Develop a simple executable model. Proceed in phases: first only checking, then with elaboration, finally with cooperative multi-threading instead of mutable references.
     
  • Given time, consider extensions like functional dependencies.
Relevant literature:
Student profile:
  • Passion for Functional Programming.
  • Good at theory and practice.
  • Potentially interested in a PhD.

This topic is for 1 student(s).

This topic can be selected by

  • all Master in de Ingenieurswetenschappen: Computerwetenschappen (ir) students
  • all Master of Engineering: Computer Science students

Clusters, in which this topic appears:
  • Programming Languages
18-07-2008