Skip to content

Commit a19b07c

Browse files
committed
Continuing fixes
1 parent 6fdec53 commit a19b07c

File tree

6 files changed

+175
-54
lines changed

6 files changed

+175
-54
lines changed

doc/glossary/containers.hpp

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,40 @@
33
44
@page containers Container Types
55
6-
## Container Type (Type Constructors)
7-
A **Container Type** is a **Type Constructor**; it is not a type itself but a function that takes a type \f$ T \f$ and returns a new type \f$ F(T) \f$.
6+
@section container_type Container Type (Type Constructors)
7+
A **Container Type** is not a type itself, but a **Type Constructor**: a function that takes a type \f$ T \f$ and
8+
returns a new type \f$ F(T) \f$.
89
9-
* **Generics:** In C++, these are implemented via Templates.
10-
* **Structure:** It defines the "shape" of the data (e.g., `std::vector`, `std::list`) independently of the contained value.
10+
---
11+
12+
@section type_constructor The Role of F(T)
13+
14+
While Product and Sum types compose existing types, a Container defines a "shape" or "structure" independently of the
15+
data it holds.
16+
17+
* **Generics**: In C++, these are implemented via Templates. The template `std::vector<T>` is the constructor;
18+
`std::vector<int>` is the resulting type.
19+
20+
* **Transformation**: Containers often allow for "Mapping" (Functors), where a function \f$ A \to B \f$ can be lifted
21+
to transform \f$ F(A) \to F(B) \f$.
22+
23+
@subsection container_programming Programming Languages Considerations
24+
25+
In the context of **Kumi**, understanding the difference between the **Container** (the wrapper) and the **Product**
26+
(the contents) is vital for optimization.
27+
28+
| Container Concept | C++ Implementation | Algebraic Intuition |
29+
|:------------------|:-------------------|:--------------------|
30+
| List / Sequence | `std::vector<T>` | Ordered collection of \f$ T \f$ |
31+
| Set | `std::set<T>` | Unique collection of \f$ T \f$ |
32+
33+
### Example
34+
```cpp
35+
template<typename T>
36+
struct Box {
37+
T value; // Box is the Type Constructor F
38+
};
39+
40+
Box<int> instance; // F(int)
1141
1242
**/

doc/glossary/identities.hpp

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,48 +3,57 @@
33
44
@page identity The Identity Elements (The "0" and "1" of Types)
55
6-
@tableofcontents
7-
8-
* Just as there are neutral elements for the different mathematical operations, there are neutral elements for types.
9-
With this said, before building complex types, we must define the identities that govern type algebra.
10-
These types are the foundational "identity" elements.
6+
Just as there are neutral elements for mathematical operations, there are neutral elements for types. The neutral is a
7+
mathematical element that has no effect on the computation such that using it results in the identity. The simplest
8+
example might be adding 0 to some natural number. With this said, before building complex types, we must define the
9+
neutrals that govern type algebra. These types are the foundational "identity" elements.
1110
1211
---
1312
1413
@section empty Empty Type : \f$ (0 / \bot) \f$
1514
16-
The **Empty Type** is the identity element for **Sums** \f$ (A + 0 \cong A ) \f$.
15+
The **Empty Type** is the identity element for **Sums** \f$ (A + 0 \cong A ) \f$. It represents a type with no
16+
inhabitants.
17+
1718
* **Cardinality**: 0 states. It is mathematically impossible to construct an instance of this type.
18-
* **Algebraic Role**: In logic, it represents a contradiction. In code, it represents a path that can never be taken.
19+
20+
* **Algebraic Role**: In logic, it represents a contradiction. In code, it represents a path that can never be reached.
1921
A function returning the Empty Type is a "diverging" function (it never returns).
22+
2023
* **Identity Property**: Just as \f$ x + 0 = x \f$, having a choice between Type A or an impossible Type 0 is logically
2124
equivalent to simply having Type A.
2225
2326
One cannot write such a type, but could specify a function that returns empty, in C++ this can be expressed the
2427
following way.
2528
```cpp
26-
[[noreturn]] void foo(){ }
29+
[[noreturn]] void foo() {
30+
throw std:runtime_error("Never returns");
31+
}
2732
```
2833
2934
---
3035
3136
@section unit Unit Type : \f$ (1 / \top) \f$
3237
33-
The **Unit Type** is the identity element for **Products** \f$ (A \times 1 \cong A) \f$.
34-
Also refered to as the product of no types.
38+
The **Unit Type** is the identity element for **Products** \f$ (A \times 1 \cong A) \f$. It represents a type with
39+
exactly one inhabitant.
40+
41+
@note It is also refered to as the product of no types.
3542
3643
* **Cardinality**: 1 state. It contains exactly one possible value (often written as `()`).
44+
3745
* **Algebraic Role**: It represents "no information" or "success without data." Because there is only one possible state,
3846
the value itself is predictable and thus carries zero entropy.
47+
3948
* **Identity Property**: Just as \f$ x \times 1 = x \f$, a pair containing Type A and a Unit Type provides no more
4049
information than Type A alone.
4150
42-
In C++ such a type can be written simply as a singleton.
51+
In C++ such a type can be written simply as an empty struct.
4352
```cpp
4453
struct unit {};
4554
```
4655
47-
[In the next page](@ref product), we will see how to combine these simple types in order to compose what we call
48-
product types which is the main topic of kumi.
56+
[In the next page](@ref product), we will see how to combine the simple types we saw until now in order to create more
57+
complex types.
4958
5059
**/

doc/glossary/introduction.hpp

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,22 @@
44
@page introduction Type Theoretic Foundations
55
66
This introduction does not aim at providing full understanding of type theory, but is more focused on giving a brief yet
7-
extensive overview of the basis that might be of interest to any new or even experienced programmer. Kumi makes extensive
8-
use of some concepts that will be detailed in the following sections.
7+
extensive overview of the basis that might be of interest to any new or even experienced programmer. **Kumi** makes
8+
extensive use of some concepts that will be detailed in the following sections. In the current state, the **Kumi**
9+
library focuses on handling [product types](@ref product_type).
10+
11+
---
912
1013
@section origin Origin
1114
12-
**Type Theory** is the academic study of type systems used to formalize how types are constructed and composed.
15+
Type theory is the academic study of type systems used to formalize how types are constructed and composed.
1316
While Set Theory categorizes elements into sets, Type Theory categorizes data (propositions) and the transformations
1417
(proofs) applied to them.
1518
1619
In this view:
1720
18-
* **Types** are classifications of data (propositions).
19-
* **Functions/Programs** are the logical steps that transform data (proofs).
21+
+ **Types** are classifications of data (propositions).
22+
+ **Functions/Programs** are the logical steps that transform data (proofs).
2023
2124
---
2225
@@ -26,14 +29,14 @@ The **Curry-Howard Correspondence** formalizes the direct link between logical r
2629
Every concept in logic has a direct counter part in type theory.
2730
The following table recapitulates the different operation that can be necessary to understand.
2831
29-
| Logic Name | Logic Notation | Type Notation | Type Name | C++ Example |
30-
| :------------ | :-------------------| :-------------------| :----------------------| :-----------------------|
31-
| True | \f$ \top \f$ | \f$ \top \f$ | Unit Type | `std::monostate` |
32-
| False | \f$ \bot \f$ | \f$ \bot \f$ | Empty Type | `void` |
33-
| Implication | \f$ A \to B \f$ | \f$ A \to B \f$ | Function | `std::function<B(A)>` |
34-
| Not | \f$ \neg A \f$ | \f$ A \to 0 \f$ | Function to empty type | `[[noreturn]] void foo` |
35-
| And | \f$ A \land B \f$ | \f$ A \times B \f$ | Product type | `std::tuple<A,B>` |
36-
| Or | \f$ A \lor B \f$ | \f$ A + B \f$ | Sum type | `std::variant<A,B>` |
32+
| Logic Name | Logic Notation | Type Notation | Type Name | C++ Example |
33+
| :------------ | :-------------------| :-------------------| :----------------------|:-----------------------------|
34+
| True | \f$ \top \f$ | \f$ \top \f$ | Unit Type | `std::monostate` |
35+
| False | \f$ \bot \f$ | \f$ \bot \f$ | Empty Type | `void` (as a non-return type)|
36+
| Implication | \f$ A \to B \f$ | \f$ A \to B \f$ | Function | `std::function<B(A)>` |
37+
| Not | \f$ \neg A \f$ | \f$ A \to 0 \f$ | Function to empty type | `[[noreturn]] void foo` |
38+
| And | \f$ A \land B \f$ | \f$ A \times B \f$ | Product type | `std::tuple<A,B>` |
39+
| Or | \f$ A \lor B \f$ | \f$ A + B \f$ | Sum type | `std::variant<A,B>` |
3740
3841
---
3942
@@ -52,11 +55,29 @@ x : bool // A formal variable
5255
Then in order to be able to manipulate these types, there is the concept of functions terms. This is simply a function
5356
in the programming sens. Given a parameter of a type \f$ \sigma \f$ and a return type \f$ \tau \f$ the associated function
5457
is noted \f$ \sigma \rightarrow \tau \f$. The addition of two real numbers can then written as :
55-
\f$ add : *nat* \rightarrow (*nat* \rightarrow *nat*)\f$
58+
add : nat \f$ \rightarrow \f$ (nat \f$ \rightarrow \f$ nat)
5659
5760
Finally, there are lambda terms, it is associated to concept of anonymous functions/lambdas functions in programming,
5861
these should sound familiar. They simply represent a way to define new function terms.
5962
63+
---
64+
65+
@section cardinality Type Cardinality
66+
67+
In the context of Set Theory, the **Cardinality** of a set is simply the number of element it contains. When translating
68+
it to Type THeory, the cardinality of a type refers to the total number of unique values (or states) an instance of that
69+
type can possibly represent.
70+
71+
By treating types as set of potential values, we can use standard arithmetic to predict the complexity of our data
72+
structuresr:
73+
74+
* **Finite Sets**: A type like ```bool``` as a cardinality of 2, the set of it's values is \f$ {true, false} \f$
75+
76+
* **The Power of Counting**: Understanding cardinality allows us to see types not just as labels, but as mathematical
77+
objects where the size of the state space dictates how much information a variable can have.
78+
79+
---
80+
6081
[In the next page](@ref identity), we will see some more specific types that are used as a base for more complex
6182
operations.
6283

doc/glossary/product.hpp

Lines changed: 54 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
44
@page product Product Types
55
6-
@tableofcontents
7-
86
@section product_construction Product Constructions (The Logic of "AND")
97
108
Products represent types where multiple elements exist simultaneously.
119
The operands of the product are types.
1210
The structure of a product type is determined by the fixed order of the operands.
1311
14-
@subsection product_type Product Type \f$ (A \times B) \f$
12+
---
13+
14+
@section product_type Product Type \f$ (A \times B) \f$
1515
1616
A **Product Type** is a compound type formed by combining multiple types.
1717
@@ -46,36 +46,70 @@ A product type follows **List Algebra**.
4646
+ **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.
4747
+ **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$.
4848
49-
### Example
50-
```cpp
51-
std::tuple<int, float> p; // Ordered product
52-
```
49+
@subsection tuple_programming Programming Languages Considerations
5350
54-
@note A pair is a special case of a tuple that is composed of only two elements.
51+
Product types can also be composed of homogeneous types, such a tuple does not differ from array types. In most programming
52+
models homogeneous product types can be efficiently stored via contiguous location in memory without needing to insert
53+
padding. By definition, an homogeneous product type can be iterated on by indexing as the gap between two consecutive
54+
elements is fixed. This property does not hold for their heterogeneous counterpart
5555
56-
* **Reference** [Wikipedia: Tuple](https://en.wikipedia.org/wiki/Tuple)
56+
The C++ standard library provides an implementation of a tuple type as well as some special cases. Kumi provides a generic
57+
yet optimized representation of them. Each type provided in the standard can be represented efficiently.
58+
59+
| STL type | Kumi |
60+
|:--------------------|:------------------------|
61+
| std::pair<T1,T2>; | kumi::tuple<T1,T2> |
62+
| std::tuple<Ts...>; | kumi::tuple<Ts...> |
63+
| std::array<T,5>; | kumi::tuple<T,T,T,T,T> |
64+
| std::complex<T>; | kumi::tuple<T,T> |
5765
5866
---
5967
60-
@subsection record_type Record Type \f$ \{label: Type\} \f$ (The Struct)
68+
@section record_type Record Type \f$ \{label: Type\} \f$ (The Struct)
6169
6270
A **Record Type** identifies its components by a **unique label** rather than a position.
6371
6472
An instance of a record type is called a struct in many programming languages.
6573
(Let aside considerations about object oriented progamming, inheritance, etc...)
6674
6775
A **Record Type** is the labeled version of a Product, it can be seen as the product of mathematical sets.
76+
Similarily to tuples, it accepts one constructor that takes a variadic number of input that are in this case fields.
77+
78+
Formally it can be written as :
79+
80+
\f$ \{l_1 : x_1, l_2 : x_2, ..., l_n : x_n\} : T_1 x T_2 x ... x T_n \f$
81+
82+
This can be read as : the record is composed of elements of types \f$ T_1 \f$ to \f$ T_n\f$
83+
with corresponding values \f$ x_1 \f$ to \f$ x_n \f$ that are labeled with \f$ l_1 to l_n \f$.
84+
85+
The corresponding definition in kumi could look like the following:
86+
```cpp
87+
using namespace kumi::literals;
88+
kumi::record a = { "x"_id = 42, "y"_id = 13.37f, "z"_id = 'd' };
89+
```
90+
91+
To extract values from a record, there are projections that are term constructors.
92+
A projection (often noted \f$ \pi \f$) is a function that given a product type
93+
returns the elements. Formally it is denoted as :
94+
95+
\f$ \pi_1(x) : T_1, \pi_2(x) : T_2, ..., \pi_n(x) : T_n \f$
96+
97+
In kumi this corresponds to the `get` functions
98+
```c
99+
auto a = kumi::get<"x">(kumi::record{ "x"_id = 42, "y"_id = 13.37f, "z"_id = 'd' });
100+
```
101+
68102
It follows **Set Algebra** on top of **List Algebra**.
103+
+ **Label Identity**: Components are identified by unique **Labels** (names) rather than position.
104+
+ **Permutation**: Theoretically, a record is a set of field mappings. In many formal definitions, `{x: int, y: float}`
105+
is isomorphic to `{y: float, x: int}` because the set of labels and their associated types are identical.
106+
+ **Semantic Access**: Access is performed via labels (`u.id`), adding semantic meaning to the structure.
69107
70108
Operation on records are traditionally not structural in the sens that they operate per label, never per
71109
element. However, as they are internally represented by a tuple, such operation are permitted. As an example, one
72110
could "rotate" a record \f$ n \f$ positions to the left. Mathematically this might not make a lot of sens
73111
whereas for a programmer that has to consider memory representation, this is potentially critical to have.
74112
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.
79113
80114
### Example
81115
```cpp
@@ -84,9 +118,13 @@ struct User {
84118
float score; // Label: score
85119
};
86120
```
87-
88-
* **Reference** [Wikipedia: Record](https://en.wikipedia.org/wiki/Record_(computer_science))
121+
---
89122
90123
[In the next page](@ref sum), we will see the duals of product types which are called sum types.
91124
125+
---
126+
127+
* **Reference** [Wikipedia: Tuple](https://en.wikipedia.org/wiki/Tuple)
128+
* **Reference** [Wikipedia: Record](https://en.wikipedia.org/wiki/Record_(computer_science))
129+
92130
**/

doc/glossary/sum.hpp

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,42 @@
33
44
@page sum Sum Types
55
6-
@tableofcontents
7-
86
@section sum_construction Sum Constructions (The Logic of "OR")
97
10-
Sums represent types where a choice is made between different alternatives. They are the mathematical **Dual** of Products.
8+
Sums represent types where a choice is made between different alternatives. They are the mathematical **Dual** of
9+
Products Types.
1110
1211
@subsection sum_type Sum Type \f$ (A + B) \f$ (The Coproduct)
1312
1413
An unlabeled, positional choice between types.
14+
15+
An instance of a sum type represents an **Exclusive Choice**, formally it is the disjoint union of sets.
16+
1517
* **Exclusive Choice**: An instance of \f$ A + B \f$ contains either an A or a B, but never both simultaneously.
18+
1619
* **Cardinality**: The state space is the **sum** of its components: \f$ Card(A + B) = Card(A) + Card(B) \f$.
20+
1721
* **Algebraic Property**: It is the dual of the Product. While a Product asks you to "Take All," a Sum asks you to "Pick One."
1822
23+
In C++, this is most commonly represented by `std::variant`.
1924
### Example
2025
```cpp
2126
std::variant<int, float> p;
2227
```
2328
24-
@subsection variant Variant (The Labeled Sum)
25-
A **Variant** is the labeled version of a Sum Type (and thus the dual of the Record).
26-
* **Active Labeling**: Just as a Record has multiple labels present at once, a Variant has exactly **one active label** from a set of possibilities.
27-
* **The Tag**: In C++, `std::variant` implements this as a **Tagged Union**. It uses a small integer (the tag/index) to track which label is currently active, ensuring type safety.
29+
---
30+
31+
@section variant Variant Type (The Labeled Sum)
32+
33+
A **Variant** (commonly refered to as Tagged Union) is the labeled version of a Sum Type, serving as the dual of the
34+
**Record**.
35+
36+
While a Record has multiple labels present at once, a Variant as exactly one active label from a set of possibilities.
37+
38+
* **The Tag**: To ensure type safety, the implementation often uses a tag to track the currently stored type.
39+
40+
* **Cardinality**: The state space of a variant is the sum of the cardinalities of it's fields:
41+
\f$ Card({l_1 : T_1, l_2 : T_2, ..., l_n : T_n}) = \sum_i^n Card(T_i) \f$
2842
2943
### Example
3044
```cpp
@@ -35,4 +49,12 @@ union particles {
3549
};
3650
```
3751
52+
@note As of today, kumi does not provides it's own sum types. The focus was made on product type and it's associated
53+
algebra.
54+
55+
---
56+
57+
[In the next page](@ref containers), we will one of the last kind of types that can be found througout kumi which are
58+
called container types.
59+
3860
**/

doc/layout.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<tab type="user" url="@ref identity" title="Identity Types"/>
1515
<tab type="user" url="@ref product" title="Product Types"/>
1616
<tab type="user" url="@ref sum" title="Sum Types"/>
17+
<tab type="user" url="@ref containers" title="Container Types"/>
1718
</tab>
1819
<tab type="user" url="@ref cpp_spec" title="C++ Vocabulary"/>
1920
<tab type="user" url="@ref nomenclature" title="Nomenclature"/>

0 commit comments

Comments
 (0)