Haskell 的类型类和 Go 的接口

发布于 2024-09-04 00:51:21 字数 63 浏览 5 评论 0原文

Haskell 的 TypeClass 和 Go 的接口有什么相同点和不同点?这两种方法的相对优点/缺点是什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

客…行舟 2024-09-11 00:51:21

看起来仅在表面上是Go接口,就像单参数类型类(构造函数类)一样哈斯克尔。

  • 方法与接口类型相关
  • 联对象(特定类型)可能具有该接口的实现

我不清楚Go是否以任何方式通过接口支持有界多态性,这是类型类的主要目的。也就是说,在Haskell中,接口方法可能用于不同的类型,

class I a where
    put :: a -> IO ()
    get :: IO a

instance I Int where
    ...

instance I Double where
    ....

所以我的问题是Go是否支持类型多态性。如果不是,它们就根本不像类型类。而且它们实际上没有可比性。

Haskell 的类型类允许通过“泛型”(更高级的多态性)对代码进行强大的重用,这是 对这种形式的通用程序的跨语言支持就是本文

这里详细描述了通过类型类实现的临时多态性或有界多态性。这是 Haskell 中类型类的主要目的,并且没有通过 Go 接口解决,这意味着它们根本不是非常相似。接口的功能严格来说不太强大——一种零阶类型类。

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.

辞旧 2024-09-11 00:51:21

我将补充 Don Stewart 的出色答案,Haskell 类型类的令人惊讶的结果之一是您可以在编译时使用逻辑编程来生成任意多个类的实例。 (Haskell 的类型类系统包括 Prolog 的有效子集,与 Datalog 非常相似。)该系统在 QuickCheck 库中得到了很好的利用。或者对于一个非常简单的示例,您可以了解如何定义 适用于任意数量谓词的布尔补码版本(not。我怀疑这种能力是类型等级系统的意外结果,但事实证明它非常强大。

Go 没有类似的东西。

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.

白云不回头 2024-09-11 00:51:21
  1. 在 haskell 中,类型类实例化是显式的(即,您必须说 instance Foo Bar 才能使 Bar 成为 Foo 的实例),而在 go 中,实现接口是隐式的(即,当您定义一个类时,该类定义了正确的方法,它会自动实现相应的接口,而不必说诸如 implement InterfaceName 之类的东西。
  2. 接口只能描述接口实例为接收者的方法。在类型类中,实例化类型可以出现在任何参数位置或函数的返回类型处(即,您可以说,如果 Foo 是 Bar 类型的实例,则必须有一个名为 baz 的函数,该函数接受 Int 并返回 Foo -你不能用接口这么说)。
  1. In haskell typeclass instantiation is explicit (i.e. you have to say instance Foo Bar for Bar to be an instance of Foo), while in go implementing an interface is implicit (i.e. when you define a class that defines the right methods, it automatically implements the according interface without having to say something like implement InterfaceName).
  2. An interface can only describe methods where the instance of the interface is the receiver. In a typeclass the instantiating type can appear at any argument position or the return type of a function (i.e. you can say, if Foo is an instance of type Bar there must be a function named baz, which takes an Int and returns a Foo - you can't say that with interfaces).
悲歌长辞 2024-09-11 00:51:21

从表面上看,Go 的接口更像是 OCaml 中的结构子类型。

Very superficial similarities, Go's interfaces are more like structural sub-typing in OCaml.

沩ん囻菔务 2024-09-11 00:51:21

C++ 概念(没有进入 C++0x)就像Haskell 类型类。还有一些 Haskell 中根本不存在的“公理”。它们让你可以将诸如单子定律之类的东西形式化。

C++ Concepts (that didn't make it into C++0x) are like Haskell type classes. There were also "axioms" which aren't present in Haskell at all. They let you formalize things like the monad laws.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文