|
| 1 | +#error This file is for documentation only - DO NOT INCLUDE |
| 2 | +/** |
| 3 | +
|
| 4 | +@page product Product Types |
| 5 | +
|
| 6 | +@tableofcontents |
| 7 | +
|
| 8 | +@section product_construction Product Constructions (The Logic of "AND") |
| 9 | +
|
| 10 | +Products represent types where multiple elements exist simultaneously. |
| 11 | +The operands of the product are types. |
| 12 | +The structure of a product type is determined by the fixed order of the operands. |
| 13 | +
|
| 14 | +@subsection product_type Product Type \f$ (A \times B) \f$ |
| 15 | +
|
| 16 | +A **Product Type** is a compound type formed by combining multiple types. |
| 17 | +
|
| 18 | +An instance of a product type in it's simplest form is a called a tuple. |
| 19 | +
|
| 20 | +Such a type only accepts one constructor that takes a variadic number of inputs |
| 21 | +and builds the product of them. A product type has a fixed length and the types of |
| 22 | +the elements is fixed. Formally it can be written as : |
| 23 | +
|
| 24 | +\f$ (x_1, x_2, ..., x_n) : T_1 \times T_2 \times ... \times T_n \f$ |
| 25 | +
|
| 26 | +This can be read as : the tuple is composed of elements of types \f$ T_1 \f$ to \f$ T_n\f$ |
| 27 | +with corresponding values \f$ x_1 \f$ to \f$ x_n \f$. |
| 28 | +
|
| 29 | +The corresponding definition in kumi could look like the following: |
| 30 | +```cpp |
| 31 | +kumi::tuple<int,float,char> a = { 42, 13.37f, 'd' }; |
| 32 | +``` |
| 33 | +
|
| 34 | +To extract values from a tuple, there are projections that are term constructors. |
| 35 | +A projection (often noted \f$ \pi \f$) is a function that given a product type |
| 36 | +returns the elements. Formally it is denoted as : |
| 37 | +
|
| 38 | +\f$ \pi_1(x) : T_1, \pi_2(x) : T_2, ..., \pi_n(x) : T_n \f$ |
| 39 | +
|
| 40 | +In kumi this corresponds to the `get` functions |
| 41 | +```c |
| 42 | +auto a = kumi::get<0>(kumi::tuple<int,float>{ 1, 2.f }); |
| 43 | +``` |
| 44 | +
|
| 45 | +A product type follows **List Algebra**. |
| 46 | ++ **Order Dependence**: Position is identity. \f$ (A, B) \f$ is structurally distinct from \f$ (B, A) \f$ even though they contain the same underlying types. |
| 47 | ++ **Cardinality**: The total state space is the **product** of the states of its components: \f$ Card(A \times B) = Card(A) \times Card(B) \f$. |
| 48 | +
|
| 49 | +### Example |
| 50 | +```cpp |
| 51 | +std::tuple<int, float> p; // Ordered product |
| 52 | +``` |
| 53 | +
|
| 54 | +@note A pair is a special case of a tuple that is composed of only two elements. |
| 55 | +
|
| 56 | +* **Reference** [Wikipedia: Tuple](https://en.wikipedia.org/wiki/Tuple) |
| 57 | +
|
| 58 | +--- |
| 59 | +
|
| 60 | +@subsection record_type Record Type \f$ \{label: Type\} \f$ (The Struct) |
| 61 | +
|
| 62 | +A **Record Type** identifies its components by a **unique label** rather than a position. |
| 63 | +
|
| 64 | +An instance of a record type is called a struct in many programming languages. |
| 65 | +(Let aside considerations about object oriented progamming, inheritance, etc...) |
| 66 | +
|
| 67 | +A **Record Type** is the labeled version of a Product, it can be seen as the product of mathematical sets. |
| 68 | +It follows **Set Algebra** on top of **List Algebra**. |
| 69 | +
|
| 70 | +Operation on records are traditionally not structural in the sens that they operate per label, never per |
| 71 | +element. However, as they are internally represented by a tuple, such operation are permitted. As an example, one |
| 72 | +could "rotate" a record \f$ n \f$ positions to the left. Mathematically this might not make a lot of sens |
| 73 | +whereas for a programmer that has to consider memory representation, this is potentially critical to have. |
| 74 | +
|
| 75 | ++ **Label Identity**: Components are identified by unique **Labels** (names) rather than position. |
| 76 | ++ **Permutation**: Theoretically, a record is a set of field mappings. In many formal definitions, `{x: int, y: float}` |
| 77 | + is isomorphic to `{y: float, x: int}` because the set of labels and their associated types are identical. |
| 78 | ++ **Semantic Access**: Access is performed via labels (`u.id`), adding semantic meaning to the structure. |
| 79 | +
|
| 80 | +### Example |
| 81 | +```cpp |
| 82 | +struct User { |
| 83 | + int id; // Label: id |
| 84 | + float score; // Label: score |
| 85 | +}; |
| 86 | +``` |
| 87 | +
|
| 88 | +* **Reference** [Wikipedia: Record](https://en.wikipedia.org/wiki/Record_(computer_science)) |
| 89 | +
|
| 90 | +[In the next page](@ref sum), we will see the duals of product types which are called sum types. |
| 91 | +
|
| 92 | +**/ |
0 commit comments