[Haskell-beginners] type classes, java interfaces in type systems space

John Li jli at circularly.org
Fri Nov 6 15:22:24 EST 2009


Hi all,

I've been trying to get a feel for the space of programming languages,
and in particular, different sorts of type systems. In hopes of
clarifying and correcting my thoughts, please bear with me while I
ramble for a while, and correct and direct me towards enlightening
resources.


Abstraction is good, boilerplate is bad, and we'd like to write
general code that works for different "sorts" of things. Parametric
polymorphism and interfaces are 2 common ways to do this, and fit
together well with current strong, static type systems.


Parametric polymorphism enables abstraction by allowing functions to
operate on a particular "containing" type, but arbitrary "contained"
types, exemplified by the many well-known list functions. Abstraction
is achieved because the desired functionality only depends on the
specific structure (interface?) of the "outer" type. This is
essentially exactly what generics in Java are.


Interfaces (I use the term in the Java sense, though I'm not fully
comfortable with the details) allow one to write code that works with
any type, as long as that type provides the functionality specified in
an interface. So, type classes in Haskell provide similar
functionality to interfaces in Java.

It seems like parametric polymorphism is a more restricted case of
type classes. Map could perhaps be something like:

map :: (Consable xs x, Consable ys y) => (x -> y) -> xs -> ys

class Consable xs x where
  cons :: (x, xs) -> xs
  decons :: xs -> (x, xs)

(I think functional dependencies could be used to link xs and x in the
type system?) The Functor class seems similar, but I'm not sure if I
see that it's exactly the same.

Clojure's "seq" abstraction (implemented for most (all?) of its
built-in collection types as well as Java Iterables, etc.) requires
"first", "rest", and "cons". Through this interface, Clojure
implements map, filter, reverse, etc. once, but they work for all its
collections.


Thanks for your patience!
-John


More information about the Beginners mailing list