40

Concepts for C++ from the Concepts TS have been recently merged into GCC trunk. Concepts allow one to constrain generic code by requiring types to satisfy the conditions of the concept ('Comparable' for instance).

Haskell has type classes. I'm not so familiar with Haskell. How are concepts and type classes related?

jbcoe
  • 3,611
  • 1
  • 30
  • 45
  • Concepts are a form of type system for the currently untyped metaprogramming facilities (everything is a `typename`, not looking at value parameters). Type classes allow overloading / selecting functions dependent of the type of their arguments. https://m.reddit.com/r/haskell/comments/1e9f49/concepts_in_c_template_programming_and_type/ This may be a relevant discussion. – Daniel Jour Aug 20 '15 at 17:45

2 Answers2

57

Concepts (as defined by the Concepts TS) and type classes are related only in the sense that they restrict the sets of types that can be used with a generic function. Beyond that, I can only think of ways in which the two features differ.

I should note that I am not a Haskell expert. Far from it. However, I am an expert on the Concepts TS (I wrote it, and I implemented it for GCC).

  • Concepts (and constraints) are predicates that determine whether a type is a member of a set. You do not need to explicitly declare whether a type is a model of concept (an instance of a type class). That's determined by a set of requirements and checked by the compiler. In fact, concepts do not allow you to write "T is a model of C" at all, although this is readily supported using various metaprogramming techniques.

  • Concepts can be used to constrain non-type arguments, and because of constexpr functions and template metaprogramming, express pretty much any constraint you could ever hope to write (e.g., a hash array whose extent must be a prime number). I don't believe this is true for type classes.

  • Concepts are not part of the type system. They constrain the use of declarations and, in some cases template argument deduction. Type classes are part of the type system and participate in type checking.

  • Concepts do not support modular type checking or compilation. Template definitions are not checked against concepts, so you can still get late caught type errors during instantiation, but this does add a certain degree of flexibility for library writers (e.g., adding debugging code to an algorithm won't change the interface). Because type classes are part of the type system, generic algorithms can be checked and compiled modularly.

  • The Concepts TS supports the specialization of generic algorithms and data structures based based on the ordering of constraints. I am not at all an expert in Haskell, so I don't know if there is an equivalent here or not. I can't find one.

  • The use of concepts will never add runtime costs. The last time I looked, type classes could impose the same runtime overhead as a virtual function call, although I understand that Haskell is very good at optimizing those away.

I think that those are the major differences when comparing feature (Concepts TS) to feature (Haskell type classes).

But there's an underlying philosophical difference in two languages -- and it isn't functional vs. whatever flavor of C++ you're writing. Haskell wants to be modular: being so has many nice properties. C++ templates refuse to be modular: instantiation-time lookup allows for type-based optimization without runtime overhead. This is why C++ generic libraries offer both broad reuse and unparalleled performance.

Andrew Sutton
  • 959
  • 6
  • 5
  • In practice, Haskell's modularity is somewhat limited because its compilation relies heavily on inlining and specialization for performance. If a module is modified in any way, all modules that import it will need to be recompiled. There are techniques for performing type-driven optimizations off various sorts (such as unrolling loops over data structures whose size is fixed by their types), and also a somewhat wild system for user-defined compiler rewrite rules. – dfeuer Aug 21 '15 at 23:35
  • Do you have a good reference on the differences between C++0x concepts and Concepts TS? I was under the impression that the former did achieve modular type-checking, so is it correct to assume that this was then dropped for Concepts TS? – Dominique Devriese Aug 28 '15 at 14:00
  • No good reference yet. C++0x concepts are not too different than type classes. The Concepts TS uses a predicates to define constraints, and does not include modular type checking. – Andrew Sutton Aug 29 '15 at 12:55
  • 1
    Late reply here, but a small correction: as far as I know, Haskell typeclasses are in compile-time constructs, as long as you don't use existential quantification. See http://stackoverflow.com/questions/28169119/is-the-dispatch-of-a-haskell-typeclass-dynamic for more details. – iustin Sep 14 '16 at 13:13
  • Being a practical guy, if it comes to Haskell type classes -> C++ concepts boils down to 1 question: Do concepts support the statement: "There must be a function ``std::string foo(const T& arg)`` with a concept related argument type list."? Point is: The first idea I had to test concepts was to have a "concept Show", which would act like Haskells ``Show`` type class....and the reading and googling started. Also, I remember when I started learning haskell, that no one in IRC made the connection, that the old style C++ interfaces can be found in Haskell in the form of type classes.. – BitTickler Jun 05 '19 at 09:10
19

You might be interested in the following research paper:

"A comparison of C++ concepts and Haskell type classes", Bernardy et al., WGP 2008. Pdf More details.

Update: as a short summary of the paper: the paper defines a precise mapping between terminology for C++ concepts and terminology for Haskell type classes and uses this mapping to provide a detailed feature comparison between the two.

Their conclusion says:

Out of our 27 criteria, summarised in table 2, 16 are equally supported in both languages, and only one or two are not portable. So, we can safely conclude as we started — C++ concepts and Haskell type classes are very similar.

As noted by T.C. below, it is worth pointing out that the paper is comparing C++0x concepts, not Concepts TS. I am not aware of a good reference describing the differences.

Dominique Devriese
  • 2,998
  • 1
  • 15
  • 21