KUMI v5.0.0
Gorgeous Garnet
Loading...
Searching...
No Matches
Product Types

Product Constructions

A product combines several types into one, whose values hold a value of each combined type at once. Read as a proposition, the product \( A \times B \) is the conjunction \( A \land B \), whose proof is a proof of \( A \) together with a proof of \( B \). The product comes in two forms, which differ by the way that they designate their components: by position in a tuple, and by label in a record.


Product Type

A product type \( T_1 \times \dots \times T_n \) has as values the tuples \( (x_1, \dots, x_n) \) whose component \( x_i \) has the type \( T_i \), for a length \( n \) and component types fixed by the type:

\[ (x_1, x_2, \dots, x_n) : T_1 \times T_2 \times \dots \times T_n \]

The tuple is the single introduction form of the product, which takes exactly \( n \) components, and the projections are its elimination forms: for \( 1 \le i \le n \), the projection \( \pi_i \) maps a value \( x \) of the product to its \( i \)-th component,

\[ \pi_i : T_1 \times \dots \times T_n \to T_i, \qquad \pi_i(x) : T_i. \]

Two equations relate the two forms, the computation rule, by which a projection of a tuple yields its component, and the uniqueness rule, by which a value of the product is the tuple of its projections:

\[ \pi_i(x_1, \dots, x_n) = x_i, \qquad (\pi_1(x), \dots, \pi_n(x)) = x. \]

For a sequence of indices \( i_1, \dots, i_k \) between 1 and \( n \), with \( k \ge 0 \), the projection onto several indices maps a value of the product to the tuple of its components at these indices:

\[ \pi_{i_1, \dots, i_k} : T_1 \times \dots \times T_n \to T_{i_1} \times \dots \times T_{i_k}, \qquad \pi_{i_1, \dots, i_k}(x) = (\pi_{i_1}(x), \dots, \pi_{i_k}(x)). \]

Its codomain is the unit type 1 when \( k = 0 \), and the product of the single type \( T_{i_1} \), isomorphic to \( T_{i_1} \), when \( k = 1 \). KUMI keeps these two types distinct, kumi::tuple<T> and T, so that the projection onto the single index \( i \) is \( \pi_i \) followed by the isomorphism. The map deletes components when the indices omit some, permutes them when the indices change their order, and duplicates a component when its index repeats.

The projection of relational algebra is the image of a relation under this map: it applies the map to every tuple of the relation and returns the set of the results, in which two tuples equal at the chosen indices become one. In the unnamed perspective of Abiteboul, Hull, and Vianu, section 4.4, whose tuples take their components in a single domain of values, its indices form any sequence, repetitions included. In their named perspective, where a tuple maps attributes to values (section 3.2), the projection of section 4.4 takes a set of attributes, without repetition and without order.

The product has two further properties:

  • Order: the types \( A \times B \) and \( B \times A \) are distinct when \( A \ne B \), and isomorphic through the swap of the two components. The position of a component designates it, which also tells apart the two components of \( A \times A \).
  • Cardinality: the number of values of the product is the product of the numbers of values of its components, \( \mathrm{Card}(A \times B) = \mathrm{Card}(A) \times \mathrm{Card}(B) \).

The positional product of KUMI is kumi::tuple (see Tuple Types and associated Functions), an aggregate initialized from a braced list, whose template arguments its deduction guide deduces, or built by kumi::make_tuple, kumi::tie, and kumi::forward_as_tuple. A kumi::tuple may also hold fields, which give its components a name besides their position, and it applies the empty-base optimization to its empty, non-final element types.

The projections are the functions get and the subscript by a compile-time index, which the literal _c of the namespace kumi::literals builds. They return a reference to the component and count from 0 where \( \pi_i \) counts from 1, so that the index 0 computes \( \pi_1 \):

#include <kumi/kumi.hpp>
#include <iostream>
int main()
{
using namespace kumi::literals;
kumi::tuple<int, float, char> t = {42, 13.37f, 'd'};
// the index 0 computes the first projection
std::cout << t << '\n';
std::cout << t[0_c] << " and " << kumi::get<0>(t) << '\n';
}
decltype(auto) constexpr get(array< T, N > &t) noexcept
Extracts the Ith element from a kumi::array.
Definition array.hpp:672
Fixed-size collection of heterogeneous values.
Definition tuple.hpp:35

The algorithms of the group Projections count the indices from 0 and compute three kinds of maps:

#include <kumi/kumi.hpp>
#include <iostream>
#include <type_traits>
int main()
{
using namespace kumi::literals;
kumi::tuple t = {1, 2.5, 'c', 4.f};
// the projection onto the indices 3, 1, 1, written 2, 0, 0 from 0
std::cout << kumi::reorder<2, 0, 0>(t) << '\n';
// every second component, from the index 0 to the end
std::cout << kumi::extract(t, 0_c, 4_c, 2_c) << '\n';
// the components of integral type
std::cout << kumi::filter<std::is_integral>(t) << '\n';
// the pair of projections cut at the index 2
std::cout << kumi::split(t, 2_c) << '\n';
// a projection of values holds copies, so t keeps its first component
auto u = kumi::reorder<0>(t);
kumi::get<0>(u) = 42;
std::cout << t << " and " << u << '\n';
}
constexpr extract_t extract
Definition extract.hpp:158
constexpr reorder_t< I... > reorder
Definition reorder.hpp:135
constexpr split_t split
Definition extract.hpp:305

On a kumi::record, kumi::reorder and kumi::reorder_fields, which selects the fields by label, reject a repeated index or label, since a record holds each label once.


Power Type

A power type \( T^n \) is the product of \( n \) copies of one type \( T \), isomorphic to the homogeneous product \( T \times \dots \times T \) and to the function type \( \{0, \dots, n - 1\} \to T \) from the indices to the components, the array of \( n \) values of type \( T \).

Its cardinality is \( \mathrm{Card}(T)^n \), and \( T^0 \), the array without components, is isomorphic to the unit type 1, the identity element of the product.

Since every component of a power type has the type \( T \), an index known only at run time selects a value of type \( T \). In a type system without dependent types, the same index into a heterogeneous product yields at most a value of the sum of the component types, a std::variant in C++, and only a compile-time index yields the component at its own type.

The library keeps the two notions apart:

kumi::array (see Power Types and associated Functions) is indexed at compile time only, through operator[] with a kumi::index_t, as a[1_c]. The empty kumi::array{} deduces to kumi::array<void, 0>, the identity of the power types.

An algorithm returns a power type when its results share one type, as kumi::map does with a function that returns one type, and a kumi::tuple once they differ, as kumi::push_back of a double onto an array of int does.

kumi::tuple stores the elements of a homogeneous tuple of size greater than 1 as an array, when they are neither references nor empty types.

The tuple types of the C++ standard library model kumi::concepts::product_type as they are, which lets every algorithm of KUMI accept them without conversion, and each of them has a counterpart in the library:

Standard library type KUMI counterpart
std::pair<T1, T2> kumi::tuple<T1, T2>
std::tuple<Ts...> kumi::tuple<Ts...>
std::array<T, N> kumi::array<T, N>

A kumi::array prints its components between brackets, and a kumi::tuple between parentheses:

#include <kumi/kumi.hpp>
#include <iostream>
#include <type_traits>
void show(char const* text, bool b)
{
std::cout << text << ": " << std::boolalpha << b << '\n';
}
int main()
{
using namespace kumi::literals;
using std::is_same_v;
// a power type of int, indexed at compile time
kumi::array a = {1, 2, 3};
std::cout << a << " has " << a[1_c] << " at index 1\n";
show("kumi::array<int, 3> is a power type",
show("kumi::tuple<int, int, int> is a power type",
show("kumi::tuple<int, int, int> is homogeneous",
// the empty array, the identity of the power types
show("kumi::array{} is kumi::array<void, 0>",
is_same_v<decltype(kumi::array{}), kumi::array<void, 0>>);
// an algorithm that mixes types returns a kumi::tuple
auto m = kumi::map([](int x) { return x * 2; }, a);
auto p = kumi::push_back(a, 4.5);
std::cout << m << " and " << p << '\n';
}
Concept specifying is Product Type which types are all the same.
Definition concepts.hpp:299
Concept specifying a type follows the Power Type semantic.
Definition concepts.hpp:113
constexpr map_t map
Definition map.hpp:156
constexpr push_back_t push_back
Definition push_pop.hpp:246
Fixed-size collection of homogeneous values.
Definition array.hpp:43

Record Type

A record type \( \{l_1 : T_1, \dots, l_n : T_n\} \) designates its components by labels, distinct from each other, instead of positions, and it is the product \( \prod_{l \in L} T_l \) indexed by a finite set \( L \) of labels. Its values are the records \( \{l_1 = x_1, \dots, l_n = x_n\} \), in the notation of Pierce, section 11.8:

\[ \{l_1 = x_1, \dots, l_n = x_n\} : \{l_1 : T_1, \dots, l_n : T_n\} \]

C, C++, and Rust call such a type a structure, while Pascal, Ada, and the languages of the ML family call it a record, setting aside what object-oriented programming adds to it, such as inheritance.

The projections of a record are labeled, the projection \( \pi_l \) mapping a record to the value of its field labeled \( l \), which Pierce writes \( x.l \):

\[ \pi_{l_i}(x) : T_i \]

Two properties distinguish the record from the tuple:

  • Labels: a component is designated by its label, unique within the record, rather than by its position, so that a projection names the field that it reads, as get<"x">(r) does.
  • Permutation: the record calculi differ on the order of the fields. Standard ML identifies the record types that differ by the order of their fields, whereas the basic calculus of Pierce keeps the order in the type and relates the permuted types by the subtyping rule S-RcdPerm, section 15.2.

The labeled product of the library is kumi::record (see Record Types and associated Functions), an aggregate initialized, like kumi::tuple, from a braced list of fields, each binding an identifier to a value. Two records whose fields differ by their order are distinct types, and neither converts to the other, yet their comparison and their assignment match the fields by identifier, whatever their order.

The projection by name takes the identifier of the field, "x"_id, in the subscript or in get, or the label alone, a kumi::str, in get<"x">:

#include <kumi/kumi.hpp>
#include <iostream>
int main()
{
using namespace kumi::literals;
kumi::record r = {"x"_id = 42, "y"_id = 13.37f, "z"_id = 'd'};
std::cout << r << '\n';
// the identifier of the field, in the subscript or in get
std::cout << r["x"_id] << " and " << kumi::get<"x"_id>(r) << '\n';
// the label alone, a kumi::str
std::cout << kumi::get<"x">(r) << '\n';
}
Fixed-size collection of heterogeneous fields with distinct labels.
Definition record.hpp:36

A record also has an index order, the order in which its fields are declared, which std::tuple_size, std::tuple_element, and the get overloads that take an index expose. The positional algorithms that keep the fields of a record return a record, kumi::rotate_left and kumi::reverse among them, while an algorithm that adds a component without a label, such as kumi::push_back, does not accept a record.

An access by index returns the field, whose type \( \{l : T\} \) combines the label and the type of the value, while an access by label returns the value, of type \( T \).