Haskell类型类如何与C ++概念不同? [英] How are c++ concepts different to Haskell typeclasses?

查看:152
本文介绍了Haskell类型类如何与C ++概念不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

概念TS的C ++概念最近已被合并到GCC主干中。概念允许我们通过要求类型满足概念的条件来约束泛型代码(比如'Comparable')。

Haskell有类型类。我对Haskell不太熟悉。概念和类型类是如何相关的?概念(由Concepts TS定义)和类型类只有在意识到它们限制了可以与通用函数一起使用的类型集合。除此之外,我只能想到两种功能的不同之处。



我应该注意到,我不是Haskell的专家。离得很远。然而,我是Concepts TS的专家(我写了它,并且我为GCC实现了它)。


  • 概念(和约束)是判断一个类型是否是一个集合的成员的谓词。您不需要显式声明某个类型是否是概念模型(类型类的一个实例)。这是由一组要求确定的,并由编译器检查。事实上,概念不允许你写 T C 的模型,尽管这是很容易用各种元编程技术支持。

  • 概念可以用来约束非类型参数,并且由于 constexpr 函数和模板元编程,几乎可以表达任何您希望编写的约束(例如,其范围必须是素数的哈希数组)。我不相信类型类是这样。


  • 概念不是类型系统的一部分。它们限制了声明的使用,并在某些情况下限制了模板参数的推导。类型类是类型系统的一部分,并参与类型检查。

  • 概念不支持模块类型检查或编译。模板定义不是针对概念进行检查的,所以在实例化过程中仍然可以获得延迟捕获的类型错误,但是这对于库编写者来说增加了一定程度的灵活性(例如,向算法添加调试代码不会改变接口)。因为类型类是类型系统的一部分,所以可以模块化地检查和编译通用算法。
    概念TS支持通用算法和基于数据结构的专业化基于约束的顺序。我根本不是哈斯克尔的专家,所以我不知道这里是否有等价物。我无法找到它。

  • 使用概念永远不会增加运行成本。我最后一次看到,类型类可能会给虚拟函数调用带来相同的运行时间开销,但我明白Haskell非常擅长优化那些。




我认为这些是比较特征(Concepts TS)和特征(Haskell类型类别)时的主要区别。

但是,两种语言在哲学上存在根本差异,而且它与您编写的C ++的风格无关。哈斯克尔希望成为模块化:因为它有很多不错的属性。 C ++模板拒绝模块化:实例化时间查找允许基于类型的优化,而无需运行时间开销。这就是为什么C ++泛型库提供广泛的重用和无与伦比的性能。

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?

解决方案

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.

这篇关于Haskell类型类如何与C ++概念不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆