41

On slide 30/78 of this presentation, Simon suggests that implementation of type classes was a "despair" at the beginning. Is anybody aware why that was?

Matt Fenwick
  • 48,199
  • 22
  • 128
  • 192
Łukasz Lew
  • 48,526
  • 41
  • 139
  • 208
  • 1
    Not an expert in Haskell lore, but probably because it had never been done before, and nobody knew how to do it yet. – Daniel Wagner Mar 07 '12 at 13:18
  • 1
    That is for sure, it is always the case :) But in case of type classes, what exactly caused problems? – Łukasz Lew Mar 07 '12 at 13:26
  • @ŁukaszLew - no, as far as I understand it this was a completely new idea of Wadler and Blott. So you couldn't go and adapt other solutions, for example. – Ingo Mar 07 '12 at 14:29
  • Weird, I thought type classes weren't very hard to implement at all. They can be almost trivially converted to simple data-types that are explicitly passed around. I've seen this graph before, applied to Data Parallel Haskell iirc. – Sjoerd Visscher Mar 07 '12 at 14:45
  • 2
    @SjoerdVisscher Type classes can not be transformed to data types if you want the translation to remain Haskell (1.0 - 98) since method can have additional type variables in addition to the class type variable. – augustss Mar 07 '12 at 15:37
  • 1
    @augustss: Does the same not apply to Haskell 2010? – ehird Mar 07 '12 at 23:02
  • 1
    @ehird Yes, it does. You need more extensions to do the translation. – augustss Mar 07 '12 at 23:41

1 Answers1

76

I guess I'm one of the few people who have first hand experience of why it was hard, since I implemented it in hbc when there was no prior art.

So what was clear from the Wadler&Blott paper was that type checking was an extension of Hindley-Milner type checking and that at runtime you should be passing dictionaries around. From that to an actual implementation is a rather big step. A good way to understand the difficulty is to actually implement it starting from just the Wadler-Blott paper.

First, you need to come up with the idea of a type checker that not only checks types but also transforms the program; inserting evidence (dictionaries) while type checking. You also need to figure out how to construct new dictionaries from old ones using the instance declarations as an inference system.

It might all seem obvious in retrospect, but remember that since that time a lot of papers with explanations have been written. Understanding how to do something from a paper is very different from coming up with it in the first place.

Furthermore, you want type classes to be reasonably efficient which leads to its own set of problems.

augustss
  • 22,884
  • 5
  • 56
  • 93