How are c++ concepts different to Haskell typeclasses?

jbcoe

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?

Andrew Sutton

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

What are some of the different names of terms and concepts in Objective C as compared with Java?

how can a c++ concept combine concepts?

What is the difference between traits in Rust and typeclasses in Haskell?

Why is Haskell missing "obvious" Typeclasses

Is it possible to define different behaviour for generic types depending on which typeclasses are implemented?

How to shapeless case classes with attributes and typeclasses?

How do I export typeclasses?

Can two different typeclasses have the same method names?

Scala vs Haskell typeclasses: "catchall" instances

Different ways of building typeclasses in Scala?

Can "overloading" via FlexibleInstances return different types, or match on typeclasses?

Haskell style & typeclass design (should typeclasses be minimized?)

Haskell Typeclasses with parametric types

Creating Generic Typeclasses in Haskell

Checking Haskell typeclasses in a function

A concise way to factor out multiple typeclasses in Haskell?

How do I use typeclasses in Haskell functions?

Types and Typeclasses in Haskell: Missing field in record construction

Typeclasses and type inference in Haskell

How to return different types in the same function in haskell?

How to make same types with different behavior in Haskell?

Can Haskell libraries documentation be ordered by typeclasses and types?

How are Arrows and Functions different in haskell?

Why Haskell typeclasses in Agda are started with `Raw`?

How do I avoid ambiguous type errors in generic functions on typeclasses, in Haskell?

How to use conversion typeclasses

C++ different using declarations for different concepts

How to refer to data declarations in different modules with haskell

How to use C++20 concepts to do different things based on return type of a function?