Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

programming languages - What are "sums-and-products" data structures?

A recent blog post on William Cook's Fusings mentions:

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

In type theory, regular data structures can be described in terms of sums, products and recursive types. This leads to an algebra for describing data structures (and so-called algebraic data types). Such data types are common in statically typed functional languages, such as ML or Haskell.

Products

Products can be thought of as the type-theoretic view of "structs" or "tuples".

Formally, PFPL, Ch 14:

The binary product of two types consists of ordered pairs of values, one from each type in the order speci?ed. The associated eliminatory forms are projections, which select the ?rst 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.

Sums

Sum types express choice between variants of a data structure. Sometimes they are called "union types" (as in C). Many languages have no notion of sum types.

PFPL, ch 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, speci?cally the binary sum, which offers a choice of two things, and the nullary sum, which offers a choice of no things.

Recursive types

Along with products and sums, we can introduce recursion, so a type may be defined (partially) in terms of itself. Nice examples include trees and lists.

 data List a = Empty | a : List a

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

Algebra of sums, products and recursion

Give a type, say Int, we can start building up a notation for algebraic expressions that describe data structures:

A lone variable:

Int

A product of two types (denoting a pair):

Int * Bool

A sum of two types (denoting a choice between two types):

Int + Bool

And some constants:

1 + Int

where 1 is the unit type, ().

Once you can describe types this way, you get some cool power for free. Firstly, a very concise notation for describing data types, secondly, some results transfer from other algebras (e.g. differentiation works on data structures).

Examples

The unit type, data () = ()

enter image description here

A tuple, the simplest product type: data (a,b) = (a,b)

enter image description here

A simple sum type, data Maybe a = Nothing | Just a

enter image description here

and its alternative,

enter image description here

and a recursive type, the type of linked lists: data [a] = [] | a : [a]

enter image description here

Given these, you can build quite complicated structures by combining sums, products and recursive types. E.g. the simple notation for a list of products of sums of products: [(Maybe ([Char], Double), Integer)] gives rise to some quite complicated trees:

enter image description here


References


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...