Wednesday, December 22, 2010

What are Type Classes?

I joined the Haskell-cafe mailing list out of curiosity, and looked at some posts with some type-hacks. One of the questions it immediately threw up is: "What is a type-class, exactly?"

One of the reasons Hi has simple interfaces on typed values is that there is no short direct answer to the above in plain English. Well, that and the fact that there are programs for which Haskell has, or had, problems deriving the type or acts counter-intuitive. (Don't ask me for examples, it was just stuff I found in certain papers.)

So, since I am bored for the moment, think I'll spend some time whether there is an easy neat answer to that question.

[ Sterling was so kind to start a Reddit thread on it, see the replies to this post. I don't like the current answers yet since I want a non-phenomenological explanation; I believe a construct in a programming language should have a clear description in plain English which precisely describes the operational behavior of constructs such that programmers do not experience surprises in using them. Most phenomenological explanations don't do it for me, neither does a precise mathematical explanation since you really don't want to understand code in terms of the typing algorithm, or a specification of that.]


  1. FYI: I posted this to reddit and got some nice responses:

  2. Ah nice, thanks! I will be really interested if there is a non-phenomenonological answer to that.

  3. Hi Sterling, I posted two responses on that Reddit thread. They threw up the question what Hi interfaces are, with my response below. I thought I would include it here since you might be interested.

    I am the designer of the Hi language, which you should consider to be unfinished.
    No, the Hi language doesn't have type classes a-la Haskell.
    In Hi, terms describe values. A type describes the run-time 'shape' of a value, each value being associated with a type constructor. An interface is a predicate on a type constructor and describes abstractly a set of values to be associated with values produced by that constructor. An instance describes which specific values are associated with a specific type constructor instantiating a specific interface. (At least, that it is how it is supposed to work)
    Hi interfaces are much weaker than Haskell type classes which allow to associate values with types (not values), and which have a different manner of associating values associated with specific types with other run-time values.
    The closest approximation of Hi interfaces in terms of Haskell/type terminology would be type constructor classes. (Approximation since in Hi you need to 'peel of' a method of a value explicitly a-la Java, which in type constructor classes one doesn't need to, as far as I remember.)