You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/glossary/containers.hpp
+34-4Lines changed: 34 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -3,10 +3,40 @@
3
3
4
4
@page containers Container Types
5
5
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$.
8
9
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 |
Copy file name to clipboardExpand all lines: doc/glossary/product.hpp
+54-16Lines changed: 54 additions & 16 deletions
Original file line number
Diff line number
Diff line change
@@ -3,15 +3,15 @@
3
3
4
4
@page product Product Types
5
5
6
-
@tableofcontents
7
-
8
6
@section product_construction Product Constructions (The Logic of "AND")
9
7
10
8
Products represent types where multiple elements exist simultaneously.
11
9
The operands of the product are types.
12
10
The structure of a product type is determined by the fixed order of the operands.
13
11
14
-
@subsection product_type Product Type \f$ (A \times B) \f$
12
+
---
13
+
14
+
@section product_type Product Type \f$ (A \times B) \f$
15
15
16
16
A **Product Type** is a compound type formed by combining multiple types.
17
17
@@ -46,36 +46,70 @@ A product type follows **List Algebra**.
46
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
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
48
49
-
### Example
50
-
```cpp
51
-
std::tuple<int, float> p; // Ordered product
52
-
```
49
+
@subsection tuple_programming Programming Languages Considerations
53
50
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
Copy file name to clipboardExpand all lines: doc/glossary/sum.hpp
+29-7Lines changed: 29 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -3,28 +3,42 @@
3
3
4
4
@page sum Sum Types
5
5
6
-
@tableofcontents
7
-
8
6
@section sum_construction Sum Constructions (The Logic of "OR")
9
7
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.
11
10
12
11
@subsection sum_type Sum Type \f$ (A + B) \f$ (The Coproduct)
13
12
14
13
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
+
15
17
* **Exclusive Choice**: An instance of \f$ A + B \f$ contains either an A or a B, but never both simultaneously.
18
+
16
19
* **Cardinality**: The state space is the **sum** of its components: \f$ Card(A + B) = Card(A) + Card(B) \f$.
20
+
17
21
* **Algebraic Property**: It is the dual of the Product. While a Product asks you to "Take All," a Sum asks you to "Pick One."
18
22
23
+
In C++, this is most commonly represented by `std::variant`.
19
24
### Example
20
25
```cpp
21
26
std::variant<int, float> p;
22
27
```
23
28
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:
0 commit comments