Modular Type Classes

by Derek Dreyer, Robert Harper, and Manuel M. T. Chakravarty

ML modules and Haskell type classes have proven to be highly effective tools for program structuring. Modules emphasize explicit configuration of program components and the use of data abstraction. Type classes emphasize implicit program construction and ad hoc polymorphism. In this paper, we show how the implicitly-typed style of type class programming may be supported within the framework of an explicitly-typed module language by viewing type classes as a particular mode of use of modules. This view offers a harmonious integration of modules and type classes, where type class features, such as class hierarchies and associated types, arise naturally as uses of existing module-language constructs, such as module hierarchies and type components. In addition, programmers have explicit control over which type class instances are available for use by type inference in a given scope. We formalize our approach as a Harper-Stone-style elaboration relation, and provide a sound type inference algorithm as a guide to implementation.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

This is nice...

I don't have anything too specific or useful to add, but I'd like to note that this is pretty exciting work. There have always been some things about Haskell's type classes that just don't "feel right" for large-scale programming, and the limitations of ML's modules are well known. I think there is an opportunity for a very nice step forward here, and I hope a proposal like this will make it into an implementation sometime soon.

Unfortunately, the paper doesn't say anything about future work, so I'm not sure what the implementation status or plans are.

It also occurs to me to wonder about the relationship between type classes, ML-style modules and functors, this paper's modular type classes, and the abstraction mechanisms in Scala. I'm aware of these slides and a paper (An Object-Oriented Approach to Datatype-Generic Programming) by Adrian Moors, but I haven't had time to dig too deeply yet and am not sure how relevant they are.

Sometimes I feel like there's room for a new strict functional language with SML's focus on simplicity but a few new ideas (Ocaml is a bit too hairy to play this role, IMHO)... But now I'm really getting off topic.

[OTing] SML + ?

What straw-items would you put into that list of a few new ideas?

(I sure liked SML-NJ when I used it. OCAML, while impressive, has never quite worked for me. Alice is nice but there's no debugger as far as I know.)

[Further OT] SML++

There's the Successor ML Wiki for those that care to influence SML the next generation. Not real active at the moment, but given the formal nature of the ML community, it probably shouldn't come as a surprise.

Neat

I hadn't seen that. As for features, I don't really have a laundry list, but it looks like everything that had occurred to me is already on the wiki. Nice!