Product type

Short description: Result of multiplying types in type theory

This article includes a list of references, related reading or external links, but its sources remain unclear because it lacks inline citations. Please help to improve this article by introducing more precise citations. (October 2020) (Learn how and when to remove this template message)

|

In programming languages and type theory, a product of types is another, compounded, type in a structure. The "operands" of the product are types, 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 A and B are the set of all possible values of 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. The pair type is a special case of the dependent pair type, where the type B may depend on the instance picked from A.

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 (an n-ary product type), and in this case, it characterizes the expressions that behave as tuples of expressions of the corresponding types. A degenerate 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 T1×T2×...×Tn is written `T1 * T2 * ... * Tn` in ML and `(T1, T2, ..., Tn)` in Haskell. In both these languages, tuples are written `(v1, v2, ..., vn)` and the components of a tuple are extracted by pattern-matching. Additionally, many functional programming languages provide more general algebraic data types, which extend both product and sum types. Product types are the dual of sum types.

## Product types in programming languages

* C++ defines the class `std::tuple` (expressed `tuple<Ts...>` using variadic templates)[1], and for the specific case of two elements defines `std::pair` (expressed `pair<T, U>`).[2] `std::tuple` can be empty (`tuple<>`).
* C#/.NET Framework defines the class `System.Tuple`. There are specific instantiations for 1 to 8 elements. For the specific case of two elements (a pair), it uses `Tuple<T1, T2>`.[3] In order to create a tuple with nine or more components, the final parameter `TRest` of `Tuple<T1, T2, T3, T4, T5, T6, T7, TRest>` is supplied as another tuple.[4] For iterating over collections like dictionary types, the class `System.Collections.Generic.KeyValuePair` (expressed `KeyValuePair<TKey, TValue>`) is provided.[5]
* Go does not have a tuple type, but can express multiple return values in a function as a sort of tuple.[6]
* Haskell has a data type `Data.Tuple`.[7]
* Java does not have a general tuple type, but JavaFX has a type `javafx.util.Pair` (expressed `Pair<K, V>`).[8] For iterating over associative containers such as `java.util.Map`, a pair in the map is expressed as `Map.Entry<K, V>`.[9]
* Kotlin does not have a general tuple type, but has classes `kotlin.Pair` (expressed `Pair<A, B>`)[10] and `kotlin.Triple` (expressed `Triple<A, B, C>`).[11]
* Python has a `tuple` collection which can be annotated as `typing.Tuple` (expressed `Tuple[T1, T2, ..., TN]`).[12]
* Rust defines the primitive tuple type, expressed as `(T1, T2, ..., TN)`, and a pair is just `(T, U)`.[13]
* Scala defines the class `scala.Tuple`[14], which supports between 2 to 22 objects as `scala.Tuple2` (expressed as `Tuple2[A, B]`)[15] to `scala.Tuple22` (expressed as `Tuple22[A, B, ..., V]`)[16].
* Swift expresses tuples as `(T1, T2, ..., TN)`.[17]

## See also

* Product (category theory)
* Cartesian product
* Record (computer science)
* Struct (C programming language)
* Sum type
* Quotient type

## References

* product type in nLab
* Homotopy Type Theory: Univalent Foundations of Mathematics, The Univalent Foundations Program, Institute for Advanced Study. See section 1.5.

1. ↑ "std::tuple". https://en.cppreference.com/w/cpp/utility/tuple.html.
2. ↑ "std::pair". https://en.cppreference.com/w/cpp/utility/pair.html.
3. ↑ "Tuple Class". Microsoft Learn. https://learn.microsoft.com/en-us/dotnet/api/system.tuple.
4. ↑ "Tuple<T1, T2, T3, T4, T5, T6, T7, TRest> Class". Microsoft Learn. https://learn.microsoft.com/en-us/dotnet/api/system.tuple-8.
5. ↑ "KeyValuePair<TKey, TValue> struct". Microsoft Learn. https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.keyvaluepair-2.
6. ↑ "Effective Go - Functions - Multiple return values". https://go.dev/doc/effective_go#multiple-returns.
7. ↑ "Data.Tuple". https://hackage.haskell.org/package/base-4.21.0.0/docs/Data-Tuple.html.
8. ↑ "Class Pair<K, V>". Oracle Corporation. https://download.java.net/java/GA/javafx25/docs/api/javafx.base/javafx/util/Pair.html.
9. ↑ "Interface Map.Entry<K, V>". Oracle Corporation. https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/util/Map.Entry.html.
10. ↑ "Pair". JetBrains s.r.o.. https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-pair/.
11. ↑ "Triple". JetBrains s.r.o.. https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-triple/.
12. ↑ "5. Data Structures". https://docs.python.org/3/tutorial/datastructures.html.
13. ↑ "tuple - Rust". https://doc.rust-lang.org/std/primitive.tuple.html.
14. ↑ "Tuple". https://www.scala-lang.org/api/current/scala/Tuple.html.
15. ↑ "Tuple2". https://www.scala-lang.org/api/current/scala/Tuple2.html.
16. ↑ "Tuple22". https://www.scala-lang.org/api/current/scala/Tuple22.html.
17. ↑ "Types Documentation". Apple Inc.. https://docs.swift.org/swift-book/documentation/the-swift-programming-language/types/.

* v
* t
* e

Data types

Uninterpreted|

* Bit
* Byte
* Trit
* Tryte
* Word
* Bit array

Numeric|

* Arbitrary-precision or bignum
* Complex
* Decimal
* Fixed point
* Floating point
* Double precision
* Extended precision
* Long double
* Octuple precision
* Quadruple precision
* Single precision
* Reduced precision
* Minifloat
* Half precision
* bfloat16
* Integer
* signedness
* Interval
* Rational

Pointer|

* Address
* physical
* virtual
* Reference

Text|

* Character
* String
* null-terminated

Composite|

* Algebraic data type
* generalized
* Array
* Associative array
* Class
* Dependent
* Equality
* Inductive
* Intersection
* List
* Object
* metaobject
* Option type
* Product
* Record or Struct
* Refinement
* Set
* Union
* tagged

Other|

* Boolean
* Bottom type
* Collection
* Enumerated type
* Exception
* Function type
* Opaque data type
* Recursive data type
* Semaphore
* Stream
* Top type
* Type class
* Unit type
* Void

Related
topics|

* Abstract data type
* Data structure
* Generic
* Kind
* metaclass
* Parametric polymorphism
* Primitive data type
* Protocol
* interface
* Subtyping
* Type constructor
* Type conversion
* Type system
* Type theory

See also platform-dependent and independent units of information

0.00

(0 votes)

Original source: https://en.wikipedia.org/wiki/Product type. Read more

|

Retrieved from "https://handwiki.org/wiki/index.php?title=Product_type&oldid=3990798"
*[v]: View this template
*[t]: Discuss this template
*[e]: Edit this template