Concept-Controlled Polymorphism

Jaakko Järvi, Jeremiah Willcock, Andrew LumsdaineGPCE 2003 · DOI

TL;DR

This paper introduces a C++ framework for "concept-controlled polymorphism," allowing generic functions to be more precisely controlled by abstract properties of types rather than just their concrete types. They achieve this by emulating Haskell's type classes in C++ using a mechanism called enable_if, enabling safer and more flexible function overloading and specialization.

What Problem Does This Paper Address?

Generic programming aims to write adaptable and interoperable code by specifying algorithms in terms of abstract properties of types (concepts), not specific types. However, C++'s mechanisms for controlling how functions behave based on these concepts are limited. While C++ has ad-hoc polymorphism (function overloading), it can lead to ambiguous calls or unintended behavior, especially when dealing with complex type hierarchies or when trying to define generic functions that work across a set of types with shared abstract properties. The paper addresses the challenge of precisely constraining function overloading and specialization in C++ based on these abstract properties, moving closer to the ideal of "constrained polymorphism."

Methods

The core contribution is a C++ mechanism called enable_if. This is a template that conditionally includes or excludes a member type based on a boolean condition. By wrapping function return types or template parameters with enable_if, developers can create compile-time predicates that enable or disable specific function template instantiations or class template specializations.

The paper then demonstrates how this enable_if mechanism can be used to emulate Haskell's "type classes" in C++. A type class in Haskell defines a set of function signatures for a collection of types. In C++, this is achieved using two class templates:

  1. A traits template (e.g., showable_traits<T>) that encodes whether a type T conforms to the type class (e.g., by having a conforms boolean member).
  2. A wrapper template (e.g., showable<T>) that provides the actual function implementations, forwarding calls to the type-specific implementations defined in the traits specialization.

This emulation allows for:

  • Constrained Parametric Polymorphism: Functions can be defined to work only for types that model a specific concept.
  • Fine-grained Control of Overloading: Functions can be overloaded based on concept conformance, not just concrete types.
  • Concept-Based Specialization: Generic functions can be specialized based on whether a type meets certain conceptual requirements.

The paper also revisits the "Barton-Nackman trick" and shows how their type class emulation provides a simpler and safer alternative for achieving similar goals, particularly in scenarios like defining operators for matrix types.

Datasets

Not discussed in the paper.

Key Insights and Ideas

  • enable_if as a powerful compile-time guard: This mechanism is fundamental for conditional compilation and enabling/disabling code based on type properties, going beyond simple type checks.
  • Haskell Type Classes as a Model for C++ Concepts: The paper effectively demonstrates that the core ideas of Haskell's type classes (abstracting over sets of types with shared behaviors) can be emulated in C++ to achieve more robust generic programming.
  • Concept-Controlled Overloading is Key: The ability to select function overloads based on abstract properties (concepts) rather than just concrete types is crucial for writing more expressive and less error-prone generic code.
  • Avoiding Ambiguity: The proposed approach helps resolve ambiguities that can arise from traditional C++ overloading, especially when dealing with multiple inheritance or complex type relationships.
  • Non-intrusive Type Class Membership: Types can be made instances of a type class without modifying their original definitions, promoting modularity and extensibility.

Key Results

The paper presents a framework that enables concept-controlled polymorphism in C++. While not providing quantitative performance metrics, the key results are qualitative:

  • A mechanism (enable_if) for conditional compilation based on type properties.
  • A robust emulation of Haskell's type classes in C++.
  • Demonstrated solutions for problems like the "Barton-Nackman trick" and ambiguous function overloading, leading to safer and more direct code.
  • The ability to define generic functions that are precisely enabled for types modeling specific concepts, such as print functions that only work for Showable types.

Limitations and Future Work

The authors acknowledge that the C++ type class emulation is "partial." * Structural Conformance Checking: The current implementation does not enforce structural conformance (i.e., checking if required functions exist and have correct signatures) at the point of instance declaration. Errors are caught later, at call sites, which can lead to less informative diagnostics. They suggest that existing libraries for structural conformance checking could be integrated. * Conditional Instance Declarations: While enable_if handles conditional function enabling, directly expressing conditional instance declarations (e.g., "a list of T is Showable if T is Showable") is more complex and not fully addressed. * Syntactic Heaviness: C++ type class emulation is syntactically heavier than Haskell, requiring explicit specification of type classes for generic functions. Haskell can often infer these constraints. * Overlapping Type Classes: C++ allows function names to appear in multiple type classes, which can be flexible but also requires careful management to avoid ambiguity, unlike Haskell's single-type-class-per-function rule.

Future work could involve integrating structural conformance checking and exploring more sophisticated ways to handle conditional instance declarations.

Summary updated

BibTeX
@inproceedings{jarvi2003concept,
  author = {Jaakko Järvi and Jeremiah Willcock and Andrew Lumsdaine},
  title = {{Concept-Controlled Polymorphism}},
  booktitle = {GPCE},
  year = {2003},
  doi = {10.1007/978-3-540-39815-8\_14},
}

Loading related content...

Figure