2.2. Types¶
A reasonable description of a type is that it is a mathematical set of objects.
For example, the type int
is an approximation to the mathematical set \(\mathbb{Z}\), although there may be technological limitations on what values an object of type int
can take.
Suppose, however, that an algorithm \(p(n_{\text{POT}})\) is configured to operate on an integer \(n_{\text{POT}}\) that corresponds to the number of protons on target.
In such a case, specifying the function \(p\) as \(p: \mathbb{Z} \rightarrow R\) (where \(R\) is an arbitrary return type) is too permissive.
In a framework context, not all data of type int
(the equivalent to \(\mathbb{Z}\)) are suitable for processing by the algorithm \(p\).
In this document, the type therefore refers to a mathematical set that includes more than just the programming language’s type T
; it can also include various labels that identify which kind of T
is desired.
2.2.1. Boolean Set¶
The Boolean values true (\(\textsf{T}\)) and false (\(\textsf{F}\)) are used frequently in computing. It is convenient to denote a set that contains both values:
Although true and false are often represented by \(1\) and \(0\), respectively, we use the symbols \(\textsf{T}\) and \(\textsf{F}\) to avoid implicit comparisons between the members of the set \(\bool\) and (e.g.) \(\mathbb{Z}\). With this convention, \(\bool \cap \mathbb{Z} = \emptyset\).
2.2.2. Representing void
and NoneType
¶
In HEP, it is common to encounter C++ functions like:
void f(int);
double g();
where the function either returns nothing (i.e. void
) or it accepts no argument.
Python supports similar behavior for its functions and methods, but using the keyword None
instead of void
.
The mathematical set that is used to represent C++’s void
and Python’s NoneType
is the set \(\one\), which contains only one element [1].
The above functions are thus represented in function notation as:
This notation will be used as we discuss the operators required by Phlex’s higher-order functions.
The single element of the set \(\one\) can also be used to represent the value nullptr
for C++ pointers (see Section 2.2.3).
When necessary we will refer to that single element as the null value, or simply the open-closed parentheses ()
.
2.2.3. Representing Optional Types¶
It is occasionally necessary to represent a “nullable” or “optional” type \(\opt{T}\), whose objects either contain a value type \(T\) or are null. Mathematically, this is represented by the coproduct \(T \sqcup \one\), where a null or disengaged object of type \(\opt{T}\) has a value equal to the single element of the set \(\one\).
Table 2.1 gives examples of programming types in various languages that can be mathematically represented by \(\opt{T}\).
Although Phlex does not support algorithms written in Haskell, an example of the use of Maybe T
is given as an illustration of how \(\opt{T}\) is supported outside of C++ and Python.
Language |
Type \(T\) |
Type \(\opt{T}\) |
Engaged value |
Disengaged value |
---|---|---|---|---|
Haskell |
|
|
|
|
Python |
|
See caption |
|
|
C++ |
|
|
|
|
|
|
|
Footnotes