Product type

Encyclopedia

In programming language

s and type theory

, a

s, and the structure of a product type is determined by the fixed order of the operands in the product. An instance of a product type retains the fixed order, but otherwise may contain all possible instances of its primitive data types. The expression of an instance of a product type will be a tuple

, and is called a "tuple type" of expression. A product of types is a direct product

of two or more types.

If there are only two component types, it can be called a "pair type". For example, if two component types

In many languages, product types take the form of a record

type, for which the components of a tuple can be accessed by label

. In languages that have algebraic data types, as in most functional programming

languages, algebraic data types with one constructor are isomorphic to a product type.

In the Curry-Howard correspondence, product types are associated with logical conjunction

(AND) in logic.

The notion directly extends to the product of an arbitrary finite number of types (a n-ary product type), and in this case, it characterizes the expressions which behave as tuples of expressions of the corresponding types. A degenerated form of product type is the unit type

: it is the product of no types

.

In call-by-value programming languages, a product type can be interpreted as a set of pairs whose first component is a value

in the first type and whose second component is a value in the second type. In short, it is a cartesian product

and it corresponds to a product

in the category of types.

Most functional programming

languages have a primitive notion of product type. For instance, the product of

and

. In both these languages, tuples are written

s, which extend both product and sum types.

The brochure for the International System of Units starts out in section 1.1 saying "The value of a quantity is generally expressed as the product of a number and a unit", and also presents the unit product of a Newton and a meter with the product notation of mathematics: Newton meter (N m or N · m).

Programming language

A programming language is an artificial language designed to communicate instructions to a machine, particularly a computer. Programming languages can be used to create programs that control the behavior of a machine and/or to express algorithms precisely....

s and type theory

Type theory

In mathematics, logic and computer science, type theory is any of several formal systems that can serve as alternatives to naive set theory, or the study of such formalisms in general...

, a

**product**of*types*is another, compounded, type in a structure. The "operands" of the product are typeData type

In computer programming, a data type is a classification identifying one of various types of data, such as floating-point, integer, or Boolean, that determines the possible values for that type; the operations that can be done on values of that type; the meaning of the data; and the way values of...

s, and the structure of a product type is determined by the fixed order of the operands in the product. An instance of a product type retains the fixed order, but otherwise may contain all possible instances of its primitive data types. The expression of an instance of a product type will be a tuple

Tuple

In mathematics and computer science, a tuple is an ordered list of elements. In set theory, an n-tuple is a sequence of n elements, where n is a positive integer. There is also one 0-tuple, an empty sequence. An n-tuple is defined inductively using the construction of an ordered pair...

, and is called a "tuple type" of expression. A product of types is a direct product

Direct product

In mathematics, one can often define a direct product of objectsalready known, giving a new one. This is generally the Cartesian product of the underlying sets, together with a suitably defined structure on the product set....

of two or more types.

If there are only two component types, it can be called a "pair type". For example, if two component types

**A**and**B**are the set of all possible values that type, the product type written**A**×**B**contains elements that are pairs (a,b), where "a" and "b" are instances of**A**and**B**respectively.In many languages, product types take the form of a record

Record (computer science)

In computer science, a record is an instance of a product of primitive data types called a tuple. In C it is the compound data in a struct. Records are among the simplest data structures. A record is a value that contains other values, typically in fixed number and sequence and typically indexed...

type, for which the components of a tuple can be accessed by label

Identifier

An identifier is a name that identifies either a unique object or a unique class of objects, where the "object" or class may be an idea, physical [countable] object , or physical [noncountable] substance...

. In languages that have algebraic data types, as in most functional programming

Functional programming

In computer science, functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids state and mutable data. It emphasizes the application of functions, in contrast to the imperative programming style, which emphasizes changes in state...

languages, algebraic data types with one constructor are isomorphic to a product type.

In the Curry-Howard correspondence, product types are associated with logical conjunction

Logical conjunction

In logic and mathematics, a two-place logical operator and, also known as logical conjunction, results in true if both of its operands are true, otherwise the value of false....

(AND) in logic.

The notion directly extends to the product of an arbitrary finite number of types (a n-ary product type), and in this case, it characterizes the expressions which behave as tuples of expressions of the corresponding types. A degenerated form of product type is the unit type

Unit type

In the area of mathematical logic, and computer science known as type theory, a unit type is a type that allows only one value . The carrier associated with a unit type can be any singleton set. There is an isomorphism between any two such sets, so it is customary to talk about the unit type and...

: it is the product of no types

Empty product

In mathematics, an empty product, or nullary product, is the result of multiplying no factors. It is equal to the multiplicative identity 1, given that it exists for the multiplication operation in question, just as the empty sum—the result of adding no numbers—is zero, or the additive...

.

In call-by-value programming languages, a product type can be interpreted as a set of pairs whose first component is a value

Value (computer science)

In computer science, a value is an expression which cannot be evaluated any further . The members of a type are the values of that type. For example, the expression "1 + 2" is not a value as it can be reduced to the expression "3"...

in the first type and whose second component is a value in the second type. In short, it is a cartesian product

Cartesian product

In mathematics, a Cartesian product is a construction to build a new set out of a number of given sets. Each member of the Cartesian product corresponds to the selection of one element each in every one of those sets...

and it corresponds to a product

Product (category theory)

In category theory, the product of two objects in a category is a notion designed to capture the essence behind constructions in other areas of mathematics such as the cartesian product of sets, the direct product of groups, the direct product of rings and the product of topological spaces...

in the category of types.

Most functional programming

Functional programming

In computer science, functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids state and mutable data. It emphasizes the application of functions, in contrast to the imperative programming style, which emphasizes changes in state...

languages have a primitive notion of product type. For instance, the product of

*type*, ...,_{1}*type*is written_{n}*type*_{1}` * `

...` * `

*type*in ML_{n}ML programming language

ML is a general-purpose functional programming language developed by Robin Milner and others in the early 1970s at the University of Edinburgh, whose syntax is inspired by ISWIM...

and

`(`

*type*_{1}`,`

...`,`

*type*_{n}`)`

in HaskellHaskell (programming language)

Haskell is a standardized, general-purpose purely functional programming language, with non-strict semantics and strong static typing. It is named after logician Haskell Curry. In Haskell, "a function is a first-class citizen" of the programming language. As a functional programming language, the...

. In both these languages, tuples are written

`(`

*v*_{1}`,`

...`,`

*v*_{n}`)`

and the components of a tuple are extracted by pattern-matching. Additionally, many functional programming languages provide more general algebraic data typeAlgebraic data type

In computer programming, particularly functional programming and type theory, an algebraic data type is a datatype each of whose values is data from other datatypes wrapped in one of the constructors of the datatype. Any wrapped datum is an argument to the constructor...

s, which extend both product and sum types.

The brochure for the International System of Units starts out in section 1.1 saying "The value of a quantity is generally expressed as the product of a number and a unit", and also presents the unit product of a Newton and a meter with the product notation of mathematics: Newton meter (N m or N · m).