data-structures - "sums-and-products"数据结构是什么?

标签 data-structures programming-languages types type-systems algebraic-data-types

一个recent blog post on William Cook's Fusings提及:

The key point is that structures in Ensō are viewed holistically as graphs, not as individual values or traditional sums-and-products data structures.

他指的传统的和与积数据结构是什么?

最佳答案

What are the traditional sums-and-products data structures he is referring to?

在类型论中,常规数据结构可以用和、乘积和递归类型来描述。这导致了用于描述数据结构的代数(以及所谓的代数数据类型)。此类数据类型在静态类型函数语言(例如 ML 或 Haskell)中很常见。

产品

乘积可以被认为是“结构”或“元组”的类型论 View 。

正式,PFPL,第 14 章:

The binary product of two types consists of ordered pairs of values, one from each type in the order specified. The associated eliminatory forms are projections, which select the first and second component of a pair. The nullary product, or unit, type consists solely of the unique “null tuple” of no values, and has no associated eliminatory form.

求和

求和类型表示数据结构变体之间的选择。有时它们被称为“联合类型”(如在 C 中)。许多语言没有求和类型的概念。

PFPL,第 15 章:

Most data structures involve alternatives such as the distinction between a leaf and an interior node in a tree, or a choice in the outermost form of a piece of abstract syntax. Importantly, the choice determines the structure of the value. For example, nodes have children, but leaves do not, and so forth. These concepts are expressed by sum types, specifically the binary sum, which offers a choice of two things, and the nullary sum, which offers a choice of no things.

递归类型

除了乘积和和之外,我们还可以引入递归,因此类型可以(部分)根据其自身来定义。很好的例子包括树和列表。

 data List a = Empty | a : List a

 data Tree a = Nil | Node a (Tree a) (Tree a)

和、乘积和递归的代数

给定一个类型,例如Int,我们就可以开始构建描述数据结构的代数表达式的符号:

孤立变量:

整数

两种类型的乘积(表示一对):

Int * Bool

两种类型的总和(表示两种类型之间的选择):

整数+ bool <​​

还有一些常量:

1 + 整数

其中 1 是单位类型,()

一旦您能够以这种方式描述类型,您就可以免费获得一些很酷的功能。首先,用于描述数据类型的非常简洁的符号,其次,一些结果从其他代数转移(例如 differentiation works on data structures )。

示例

单位类型,data () = ()

enter image description here

元组,最简单的产品类型:data (a,b) = (a,b)

enter image description here

一个简单的求和类型data Maybe a = Nothing |只是一个

enter image description here

及其替代方案,

enter image description here

还有一个递归类型,链表的类型:data [a] = [] |一个:[a]

enter image description here

鉴于这些,您可以通过组合求和、乘积和递归类型来构建相当复杂的结构。 例如。乘积和的乘积列表的简单表示法:[(Maybe ([Char], Double), Integer)] 产生了一些相当复杂的树:

enter image description here

<小时/>

引用文献

关于data-structures - "sums-and-products"数据结构是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5911267/

相关文章:

python - 从 0 到 n 的数字中数字出现的次数

c# - 想开始编程

Haskell - 函数式编程技巧(练习 4.3)

c - C 中的字和双字整数

c# - 区分 short、int、long 真的很重要吗?

c++ - 类转换为类型

c - 如何根据C中的可变整数访问 `struct'的成员?

algorithm - 求第 k 个非自由数

database - 在数据库中存储深层目录树

perl - 用于 Perl Web 开发的 ASP 样式标签?