Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell's TypeClasses and Go's Interfaces

What are the similarities and the differences between Haskell's TypeClasses and Go's Interfaces? What are the relative merits / demerits of the two approaches?

like image 257
Jeremy Jose Avatar asked Jun 05 '10 20:06

Jeremy Jose


People also ask

What are type classes in Haskell?

Type Classes are a language mechanism in Haskell designed to support general overloading in a principled way. They address each of the concerns raised above. They provide concise types to describe overloaded functions, so there is no expo- nential blow-up in the number of versions of an overloaded function.

How are Typeclasses different from interfaces?

An interface in the Java programming language is an abstract type that is used to specify an interface (in the generic sense of the term) that classes must implement. These two looks rather similar: type class limit a type's behavior, while interface limit a class' behavior.

How are types used in Haskell?

In Haskell, every statement is considered as a mathematical expression and the category of this expression is called as a Type. You can say that "Type" is the data type of the expression used at compile time. To learn more about the Type, we will use the ":t" command.

Is Haskell an OOP?

Haskell isn't an object-oriented language. All of the functionality built here from scratch already exists in a much more powerful form, using Haskell's type system.


2 Answers

Looks like only in superficial ways are Go interfaces like single parameter type classes (constructor classes) in Haskell.

  • Methods are associated with an interface type
  • Objects (particular types) may have implementations of that interface

It is unclear to me whether Go in any way supports bounded polymorphism via interfaces, which is the primary purpose of type classes. That is, in Haskell, the interface methods may be used at different types,

class I a where     put :: a -> IO ()     get :: IO a  instance I Int where     ...  instance I Double where     .... 

So my question is whether Go supports type polymorphism. If not, they're not really like type classes at all. And they're not really comparable.

Haskell's type classes allow powerful reuse of code via "generics" -- higher kinded polymorphism -- a good reference for cross-language support for such forms of generic program is this paper.

Ad hoc, or bounded polymorphism, via type classes, is well described here. This is the primary purpose of type classes in Haskell, and one not addressed via Go interfaces, meaning they're not really very similar at all. Interfaces are strictly less powerful - a kind of zeroth-order type class.

like image 150
Don Stewart Avatar answered Sep 20 '22 15:09

Don Stewart


I will add to Don Stewart's excellent answer that one of the surprising consquences of Haskell's type classes is that you can use logic programming at compile time to generate arbitrarily many instances of a class. (Haskell's type-class system includes what is effectively a cut-free subset of Prolog, very similar to Datalog.) This system is exploited to great effect in the QuickCheck library. Or for a very simple example, you can see how to define a version of Boolean complement (not) that works on predicates of arbitrary arity. I suspect this ability was an unintended consequence of the type-class system, but it has proven incredibly powerful.

Go has nothing like it.

like image 42
Norman Ramsey Avatar answered Sep 18 '22 15:09

Norman Ramsey