Types and spaces¶
An object expression denotes a finite set, and a space expression denotes a
standard Borel space. Both are
interpreted by structural recursion on the syntax tree of
quivers.dsl.ast_nodes: the denotation of a product is the product of
the denotations of its factors, and so on for each constructor. The finite sets are objects of
\(\mathbf{FinSet}\) and the spaces are objects of \(\mathbf{SBor}\)
(Setting §3).
1. Syntax¶
The object and space expressions are given by the grammar below. The names in parentheses are the classes of the syntax tree.
Here \(n\) is a positive integer literal, \(X\) ranges over object names, \(S\) over space names, and
\(T\) over declared effects. \(C\) ranges over the space constructors Real, Simplex, Sphere,
Ball, CholeskyFactor, Covariance, Correlation, Orthogonal, Stiefel,
LowerTriangular, and Diagonal, and \(C(\bar a; \bar k)\) is a constructor applied to positional
arguments \(\bar a\) and keyword arguments \(\bar k\). In source, * writes \(\times\), positional
arguments are separated by spaces (Real 3), and keyword arguments go in braces
(Ball 3 {radius=2}).
A finite declaration may leave its cardinality open, as in object Row : FinSet _.
This introduces a static extent to specialize before tensor execution, rather
than a new probability distribution over finite sets. Exact checked tensor
shapes constrain it by equality; direct gather indices constrain it by lower
bounds. The input specialization rules
specify when those constraints determine a cardinality.
The object language and QIEC value types serve different purposes. A program's
Row -> Out declares the domain and codomain of its kernel, whereas
x : Real[Fin Row] in its runtime telescope declares an external value
with one real entry per row. A static binder [Row : FinSet] can abstract over
the object itself. These inputs do not add factors to the kernel's domain;
their fixed valuation selects the kernel, as in
Programs as kernels §3.1.
A program-local static natural binder [n : Nat] may occur in a finite-object
position. At a valuation \(n=k\), it denotes the canonical ordinal
\(\{0,\ldots,k-1\}\). The same binder in Real[Fin n] fixes the tensor's
extent. This interpretation connects an observation plate with its input type
without placing a program-local binder in the module's object environment.
The slashes \(/\) and \(\backslash\) and the effect application \(T(\bar\tau)\) build
categorial types, formulas such as S / NP that serve as
syntactic categories in a grammar. They are elements of a finite set of formulas, not finite sets
themselves, and the compiler accepts them only inside rule schemata. Outside that context the
compiler rejects them with "residuated / effect-typed expressions do not resolve to a concrete
object or space".
2. Denotation of object expressions¶
The denotation of object expressions is a function
from an expression and an object environment \(\rho_{\mathrm{obj}}\) (Setting §6) to a finite set. We drop the environment argument when it is clear. The clauses are
where \(\sqcup\) is the disjoint union, whose elements remember which summand they came from. The product and the disjoint union are the product and coproduct of \(\mathbf{FinSet}\) (Mac Lane, 1998). The function is undefined on the slashes and on effect application, as §1 states.
2.1 Object declarations¶
The declaration object X : value binds \(X\) in \(\rho_{\mathrm{obj}}\), or in \(\rho_{\mathrm{spc}}\)
when the value is a space. Besides an object or space expression, the value may take three other
forms:
| Value | Denotation |
|---|---|
{e_1, ..., e_n} |
the finite set of the labels \(e_1, \dots, e_n\), in that order (class EnumSet) |
FreeMonoid(G, max_length = n) |
the words over \(G\) of length at most \(n\) (§3) |
FreeResiduated(G, depth = d, ops = [...]) |
the categorial types over the atoms \(G\) of depth at most \(d\) (Schemas §4) |
A set given by enumeration is determined by its labels: two such sets with the same number of
elements and different labels are different objects. A category C_1, ..., C_n declaration binds
no object: it adds atomic syntactic categories (Schemas §1). There is one
declaration keyword, object, for finite sets and spaces alike. The value decides which
environment receives the name.
3. Words of bounded length¶
The free monoid \(G^*\) on a finite set \(G\) is the set
of all finite words over \(G\), with concatenation as its operation. FreeMonoid(G, max_length = n)
denotes its finite subset of words of length at most \(n\):
a finite set of \(\sum_{k=0}^{n} |\rho_{\mathrm{obj}}(G)|^{k}\) words. For \(G = \{a, b\}\) and \(n = 2\)
it is \(\{\epsilon, a, b, aa, ab, ba, bb\}\), where \(\epsilon\) is the empty word. This set is not
closed under concatenation (\(aa\) followed by \(b\) has length three), so it is not a monoid, and a
statement that uses the universal property of \(G^*\) does not apply to it without a separate
treatment of the length bound. The generator \(G\) must be a declared object name. The runtime class
is FreeMonoid, which stores the set as the coproduct
\(\mathbf 1 + G + G^2 + \dots + G^n\).
4. Denotation of space expressions¶
The denotation of space expressions is a function
and we write \(\rho = (\rho_{\mathrm{spc}}, \rho_{\mathrm{obj}})\). Each constructor is resolved to a
class of quivers.continuous.spaces:
| Source | Class | Denotation |
|---|---|---|
Real d |
Euclidean |
\(\mathbb{R}^d\) |
Real d {low=l, high=h} |
Euclidean |
\([l, h]^d\) |
Simplex d |
Simplex |
\(\Delta^{d-1}\) |
Sphere d |
Sphere |
\(S^{d-1} \subset \mathbb{R}^d\) |
Ball d {radius=r} |
Ball |
closed ball of radius \(r\) in \(\mathbb{R}^d\), \(r = 1\) by default |
CholeskyFactor d |
CholeskyFactor |
Cholesky factors of \(d \times d\) correlation matrices |
Covariance d |
Covariance |
symmetric positive definite \(d \times d\) matrices |
Correlation d |
Correlation |
correlation matrices of size \(d\) |
Orthogonal d |
Orthogonal |
the orthogonal group \(O(d)\) |
Stiefel(N, K) |
Stiefel |
the Stiefel manifold \(V_K(\mathbb{R}^N)\), \(K \le N\) |
LowerTriangular d |
LowerTriangular |
lower triangular \(d \times d\) matrices |
Diagonal d |
Diagonal |
diagonal \(d \times d\) matrices, stored as \(\mathbb{R}^d\) |
The sets in the last column are
\(\Delta^{d-1}\) is the probability simplex,
\(O(d)\) the orthogonal group, and
\(V_K(\mathbb{R}^N)\) the Stiefel manifold of
matrices with orthonormal columns. \(\mathrm{Chol}_d\) is the set of Cholesky factors of
correlation matrices: \(L \mapsto L L^\top\) is a bijection from \(\mathrm{Chol}_d\) onto
\(\mathrm{Corr}_d\), and it is the support of the
LKJ distribution on
Cholesky factors (Lewandowski et al., 2009). It corresponds to Stan's cholesky_factor_corr, not to
the Cholesky factors of arbitrary covariance matrices.
Each of these sets is a Borel subset of \(\mathbb{R}^d\) or \(\mathbb{R}^{d \times d}\) with the inherited σ-algebra, and a Borel subset of a standard Borel space is standard Borel (Kechris, 1995). The remaining clauses are
where \(\iota : \mathbf{FinSet} \hookrightarrow \mathbf{SBor}\) gives a finite set all its subsets as
events. The second clause lets a product mix a finite set with a space, as in Real 3 * Token
for an object Token : FinSet 256. The product of two standard Borel spaces, with the product
σ-algebra, is standard Borel (Kechris, 1995), so the third clause always denotes an
object of \(\mathbf{SBor}\).
5. Regrouping and order of factors¶
The product and the disjoint union are associative and commutative only up to isomorphism: the map \(((a, b), c) \mapsto (a, (b, c))\) and the swap \((a, b) \mapsto (b, a)\) are bijections, not identities. By the coherence theorem for monoidal categories, any two composites of associativity and unit isomorphisms between the same two groupings are equal, so a regrouping has one meaning whichever way it is derived (Mac Lane, 1998). The swap is not covered by that theorem and changes the order of the factors.
The runtime makes regrouping literal. It flattens nested ProductSet and CoproductSet values
while keeping the order of the components, so (A * B) * C and A * (B * C) have the same
representation, while A * B and B * A stay different until a symmetry morphism swaps them. The
empty product denotes a one-element set \(\mathbf 1\), the unit of \(\times\), and the empty
coproduct EmptySet denotes \(\emptyset\), the unit of \(+\).
6. Implementation¶
One recursive walk over ObjectExpr in
quivers.dsl.compiler.resolution realizes both denotations. It
returns a SetObject for an object expression and a
ContinuousSpace for a space expression, including a mixed
ProductSpace such as Real 3 * Token. Callers that need a finite set reject a continuous
result with a compile error.
The matrix spaces store a \(d \times d\) (or \(N \times K\)) matrix as a flat tensor in row major
order, and the sampling family is responsible for producing values in the set. Membership tests
are approximate. Covariance and Correlation accept a matrix that is symmetric to within
\(10^{-5}\) and whose eigenvalues are at least \(-10^{-7}\), so they also accept singular matrices
that lie outside \(\mathrm{Sym}^+_d\). Orthogonal, Stiefel, and Sphere test their defining
equations to within \(10^{-5}\). CholeskyFactor has no membership test: its contains raises
NotImplementedError.
Cited sources¶
Saunders Mac Lane. Categories for the Working Mathematician. Volume 5 of Graduate Texts in Mathematics. Springer, 2 edition, 1998. URL: https://link.springer.com/book/10.1007/978-1-4757-4721-8, doi:10.1007/978-1-4757-4721-8.
Daniel Lewandowski, Dorota Kurowicka, and Harry Joe. Generating random correlation matrices based on vines and extended onion method. Journal of Multivariate Analysis, 100(9):1989–2001, 2009. URL: https://www.sciencedirect.com/science/article/pii/S0047259X09000876, doi:10.1016/j.jmva.2009.04.008.
Alexander S. Kechris. Classical Descriptive Set Theory. Graduate Texts in Mathematics. Springer, 1995. doi:10.1007/978-1-4612-4190-4.