KUMI v5.0.0
Gorgeous Garnet
Loading...
Searching...
No Matches
Identity Elements

An identity element, or neutral element, of a binary operation \( \ast \) on a set is an element \( e \) such that \( e \ast a = a \ast e = a \) for every element \( a \), as 0 is for the addition of the natural numbers and 1 for their multiplication.

The sum and the product of types have identity elements as well, the empty type 0 and the unit type 1, for which the laws hold up to isomorphism rather than equality, \( A + 0 \cong A \) and \( A \times 1 \cong A \).


Empty Type

The empty type 0 has no value. It is the identity element of the sum, \( A + 0 \cong A \), since a choice between a value of \( A \) and a value that cannot exist amounts to a value of \( A \), and it absorbs the product, \( A \times 0 \cong 0 \), since a pair whose second component cannot exist cannot exist either.

  • Cardinality: 0, so that no value of the type can be constructed.
  • Logic: the empty type reads as falsity, \( \bot \), and the function type \( A \to 0 \) as the negation \( \neg A \).
  • Computation: in a total calculus, a term of type \( A \to 0 \) exists only when \( A \) is empty itself. In a language with general recursion or effects, a function whose return type is empty never returns, since it loops, throws, or terminates the program, which makes the code after its call unreachable.

C++ has no complete object type without values. The type void has an empty set of values, but it is an incomplete type that no object has, and a function returning void returns normally. A class whose default constructor is deleted keeps its copy and move constructors, so that a value of it can still be produced, by std::bit_cast among others.

The attribute [[noreturn]] marks a function that never returns, the runtime counterpart of a function to the empty type, although the attribute lies outside the type of the function:

#include <iostream>
#include <stdexcept>
[[noreturn]] void fail() { throw std::runtime_error("never returns"); }
int main()
{
// the call never yields a value, the exception leaves it
try
{
fail();
}
catch (std::exception const& e)
{
std::cout << "fail: " << e.what() << '\n';
}
}

The empty type has no counterpart in KUMI, since the library provides no sum type and C++ provides no empty type.


Unit Type

The unit type 1 has exactly one value, often written \( () \). It is the identity element of the product, \( A \times 1 \cong A \), since the second component of a pair whose second type has a single value carries no information, and it is the product of no type, the empty tuple.

  • Cardinality: 1, so that every object of the type holds the same value.
  • Logic: the unit type reads as truth, \( \top \), whose proof is its single value.
  • Information: an object of the unit type carries no information, \( \log_2 1 = 0 \) bits.

The unit type of KUMI is kumi::unit, with the constant kumi::none, and the library recognizes a unit type through the concept kumi::concepts::unit_type, which accepts a product type of size 0, kumi::tuple<> among them, and std::nullptr_t:

#include <kumi/kumi.hpp>
#include <cstddef>
#include <iostream>
void show(char const* text, bool b)
{
std::cout << text << ": " << std::boolalpha << b << '\n';
}
int main()
{
// the unit types of kumi, and its constant
show("kumi::unit is a unit type", kumi::concepts::unit_type<kumi::unit>);
show("kumi::tuple<> is a unit type",
kumi::concepts::unit_type<kumi::tuple<>>);
show("std::nullptr_t is a unit type",
std::cout << "kumi::none prints as " << kumi::none << '\n';
}
Concept specifying a type represents a Unit Type.
Definition concepts.hpp:157

An empty class such as struct unit {}; has a single value as well, yet it models no product-type concept of the library, since neither std::tuple_size nor std::tuple_element is specialized for it:

#include <kumi/kumi.hpp>
#include <iostream>
void show(char const* text, bool b)
{
std::cout << text << ": " << std::boolalpha << b << '\n';
}
struct unit {};
int main()
{
// an empty class with one value, unknown to the tuple protocol
show("struct unit is a unit type", kumi::concepts::unit_type<unit>);
show("struct unit is a product type", kumi::concepts::product_type<unit>);
}
Concept specifying a type follows the Product Type semantic.
Definition concepts.hpp:77
Previous Next
Introduction Product Types