我试图掌握这两个概念之间的关系。
首先考虑一个抽象数据类型的例子:
data Tree a = Nil
| Node { left :: Tree a,
value :: a,
right :: Tree a }
根据 Haskell wiki:
This type is abstract because it leaves some aspects of its structure undefined, to be provided by the user of the data type. This is a weak form of abstract data type. Source
现在考虑参数多态性的概念:
Parametric polymorphism refers to when the type of a value contains one or more (unconstrained) type variables, so that the value may adopt any type that results from substituting those variables with concrete types. -- Source
这里给出了一个id::a -> a
的例子:
For example, the function
id :: a -> a
contains an unconstrained type variable a in its type
问题这两个概念之间的正式关系是什么?特别是,抽象数据类型的所有实例是否也是参数多态性的实例?反之亦然?
最佳答案
有两件事要实现。首先,您的示例 Tree
实际上是一个参数类型,这是一种特殊的抽象类型。其次,参数多态性可以是类型或函数。考虑到这两点,很明显抽象类型和参数多态性都不是彼此的超集。但是,任何参数多态性是一种类型也是一种抽象类型。换句话说,抽象类型是参数多态类型的超集(不知道它们是否真的这么叫)。
关于haskell - Haskell 中的抽象数据类型与参数多态性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36948114/