Haskell Typeclasses (was Re: OO in Python? ^^)

Donn Cave donn at u.washington.edu
Thu Dec 15 12:59:44 EST 2005


In article <dns0vr$9jg$1 at panix1.panix.com>, aahz at pythoncraft.com (Aahz) 
wrote:

> In article <1h7kymc.1n6gss7to9krnN%aleax at mail.comcast.net>,
> Alex Martelli <aleax at mail.comcast.net> wrote:
> >
> >Right, you can get good genericity with Haskell's typeclasses (I've
> >posted about that often in the past, and desperately and so far
> >unsuccessfully tried to convince Guido to use something close to
> >typeclasses rather than "interfaces" for such purposes as PEP 246
> >[protocol adaptation]); it's the state of _templates_ in Haskell,
> >specifically, which I was rather dubious about (it may be that I just
> >haven't dug into them deep enough yet, but they do seem not a little
> >"convoluted" to me, so far).
> 
> Hrm.  I don't recall anything about typeclasses, so my suspicion is that
> you were writing something lengthy and above my head.  Can you write
> something reasonably short about it?  (I'm asking partly for your
> benefit, because if it makes sense to me, that will likely go a long way
> toward making sense to Guido -- we seem to think similarly in certain
> ways.)

Couple of points to hopefully augment Alex's response

- A type class may very well provide no implementation at all.
They certainly can, but they are still useful when they don't, and
indeed I think it's fair to say that isn't their main purpose.

- Doesn't hurt to recall that Haskell is a functional language.
Type classes have a superficial and maybe useful similarity to
object classes, but in Haskell, if data objects have methods,
they aren't aware of them, so to speak.  If I define a new
type class for some reason, I may make instances for every
data type I can think of -- Int, Bool, (), whatever.  Now do
these types suddenly sprout new methods?  Not really!  I just
have a set of functions that are now defined over all these
types.  Outside the scope of these declarations, of course these
functions are unknown, and the data types are the same in either
case.

- If type classes are very commonly written in Haskell applications,
it's news to me.  The need to define functions over a set of
data types in this way is relatively unusual - extremely useful
in the core language where you get Eq, Ord, Show and such basic
properties, but I think less needed higher up in the application
hierarchy.  That might be an interesting philosophical question,
as a contrast between the basic world views of FP versus OOP, but
of course you'd want to check it with someone with a lot more
Haskell than I have.

   Donn Cave, donn at u.washington.edu



More information about the Python-list mailing list