Skip to content

Expressions

An expression is the body of a let binding or an export declaration. Its combinators take morphisms, and for the parser combinators grammar data, and build new morphisms. The semantics is a compositional denotational semantics: \(\llbracket e \rrbracket_\rho\) is defined by one clause for each form of \(e\), from the denotations of its immediate subexpressions. We write \(g \circ f\) in function order, \(f\) first, and f >> g writes the same composite from left to right.

Combinator Builds
f >> g, g << f f followed by g
f @ g f and g on the two components of a paired input
fan(f, g) f and g on one shared input
f.marginalize(X) f with the codomain factor X reduced away
repeat(f, n) f applied \(n\) times with shared parameters
stack(f, n) \(n\) copies of f with separate parameters, in sequence
scan(cell) a recurrent cell run along a sequence
f.change_base(t) f converted by a transformation t

1. Syntax

\[ \begin{array}{rcl} e & ::= & x \;\big|\; \mathsf{identity}(\tau) \;\big|\; e_1 \mathbin{>\!\!>} e_2 \;\big|\; e_2 \mathbin{<\!\!<} e_1 \;\big|\; e_1 \mathbin{@} e_2 \;\big|\; e.\mathsf{marginalize}(\bar X) \;\big|\; e.\mathsf{change\_base}(t) \\ & \big| & e.\mathsf{dagger} \;\big|\; e.\mathsf{trace}(A) \;\big|\; \mathsf{cup}(A) \;\big|\; \mathsf{cap}(A) \;\big|\; e.\mathsf{curry\_right} \;\big|\; e.\mathsf{curry\_left} \;\big|\; e.\mathsf{freeze} \;\big|\; \mathsf{from\_data}(\text{"K"}) \\ & \big| & \mathsf{fan}(e_1, \dots, e_n) \;\big|\; \mathsf{repeat}(e, n) \;\big|\; \mathsf{stack}(e, n) \;\big|\; \mathsf{scan}(e, \dots) \\ & \big| & \mathsf{parser}(\dots) \;\big|\; \mathsf{ccg}(\dots) \;\big|\; \mathsf{lambek}(\dots) \;\big|\; \mathsf{chart\_fold}(\dots) \;\big|\; \mathit{op}(e_1, \dots, e_n) \\[1ex] t & ::= & \mathit{name} \;\big|\; \mathit{name}(\overline{\mathit{arg}}) \;\big|\; t_1 \mathbin{>\!\!>\!\!>} t_2 \;\big|\; y \end{array} \]

The second grammar gives the transformations used by \(\mathsf{change\_base}\): a built-in transformation with no argument, a constructor applied to arguments, a sequential composite, and a let-bound transformation \(y\) (Composition rules §5). In \(\mathit{op}(e_1, \dots, e_n)\), \(\mathit{op}\) is a contraction declared in the module.

Each expression denotes a morphism in one of the three fragments of Morphisms. We write \(\llbracket e \rrbracket_{\rho}\) for its denotation in the environment \(\rho\) (Setting §6) and drop \(\rho\) when only morphism names occur.

2. Identifiers, identity, and the monoidal combinators

2.1 Identifiers

\(\llbracket x \rrbracket_{\rho} = \rho_{\mathrm{mor}}(x)\). A name outside \(\mathrm{dom}(\rho_{\mathrm{mor}})\) is a CompileError.

2.2 Identity

\(\llbracket \mathsf{identity}(\tau) \rrbracket_{\rho} = 1_{\llbracket \tau \rrbracket_{\rho}}\), the identity morphism on the denotation of \(\tau\): the identity matrix in \(\mathbf{Mat}(\mathcal V)\) for an object expression, and the kernel \(s \mapsto \delta_s\) in \(\mathbf{BorelStoch}\) for a space expression.

2.3 Composition

\[ \llbracket e_1 \mathbin{>\!\!>} e_2 \rrbracket = \llbracket e_2 \rrbracket \circ \llbracket e_1 \rrbracket = \llbracket e_1 \rrbracket ; \llbracket e_2 \rrbracket, \qquad \llbracket e_2 \mathbin{<\!\!<} e_1 \rrbracket = \llbracket e_1 \mathbin{>\!\!>} e_2 \rrbracket. \]

The codomain of \(e_1\) must equal the domain of \(e_2\). For two finite weighted morphisms the composite is the matrix product over the module's composition rule \(\mathcal V\):

\[ \llbracket f \mathbin{>\!\!>} g \rrbracket(x, z) = \bigoplus_{y} \llbracket f \rrbracket(x, y) \otimes \llbracket g \rrbracket(y, z). \]

Both operands must have the same rule, and the operators never convert one. Composing across two rules takes an explicit change_base. For kernels the composite is the integral of Setting §3. When one operand is a finite matrix and the other a kernel, the runtime first wraps the matrix as a kernel, as described in Morphisms §4.

2.4 Tensor product

\(\llbracket e_1 \mathbin{@} e_2 \rrbracket = \llbracket e_1 \rrbracket \boxtimes \llbracket e_2 \rrbracket\), the tensor product of Morphisms §4.

2.5 Marginalization

For \(\llbracket e \rrbracket : A \to X_1 \times \cdots \times X_k \times Y\),

\[ \llbracket e.\mathsf{marginalize}(X_1, \dots, X_k) \rrbracket(a, y) = \bigoplus_{x_1, \dots, x_k} \llbracket e \rrbracket\bigl(a, (x_1, \dots, x_k, y)\bigr), \]

a morphism \(A \to Y\) that reduces the named codomain factors with \(\bigoplus\). Under the probability semiring this is the marginal distribution. Under the Boolean semiring it asks whether some value of the reduced factors relates \(a\) to \(y\). This method reduces an existing matrix. A marginalize step in a program instead sums a latent variable out of a likelihood.

2.6 Change of base

For \(\llbracket e \rrbracket : A \to B\) over \(\mathcal V\) and \(t : \mathrm{Trans}[\mathcal V, \mathcal W]\),

\[ \llbracket e.\mathsf{change\_base}(t) \rrbracket = \llbracket t \rrbracket\bigl(\llbracket e \rrbracket\bigr), \]

a morphism over \(\mathcal W\). The action of \(t\) is entrywise for a CarrierMap and acts on whole rows or matrices for a MorphismTransformation. Bayesian inversion also swaps the domain and the codomain.

2.7 Sequential composition of transformations

\(\llbracket t_1 \mathbin{>\!\!>\!\!>} t_2 \rrbracket(f) = \llbracket t_2 \rrbracket(\llbracket t_1 \rrbracket(f))\), defined when the target of \(t_1\) is the source of \(t_2\). The checker rejects a mismatch with the line and column of the expression. Since this is composition of functions, the two groupings of \(t_1 \mathbin{>\!\!>\!\!>} t_2 \mathbin{>\!\!>\!\!>} t_3\) denote the same transformation (Composition rules §5).

2.8 Two spellings of composition

>> and << are the only composition operators. Both compose with the module's composition rule, which the composition declaration sets (Composition rules §3), and g << f is f >> g written from right to left.

2.9 Compact closed structure

A compact closed category is a symmetric monoidal category in which every object \(A\) has a dual \(A^*\), with a unit \(\eta_A : I \to A^* \boxtimes A\) (the cup) and a counit \(\varepsilon_A : A \boxtimes A^* \to I\) (the cap) that satisfy the two snake equations below (Kelly and Laplaza, 1980). In string diagrams, where morphisms are drawn as boxes and objects as wires, the cup bends a wire and the snake equations straighten it again (Selinger, 2010).

When the module's rule \(\mathcal V\) is a commutative semiring, \(\mathbf{Mat}(\mathcal V)\) is compact closed with each object its own dual. The compiler accepts the four expressions below only when the rule is an Algebra (Composition rules §2), which the default ProductFuzzyAlgebra is, though it is not a semiring.

Cup and cap. With \(\delta(a, a') = \mathbf 1\) if \(a = a'\) and \(\mathbf 0\) otherwise,

\[ \llbracket \mathsf{cup}(A) \rrbracket\bigl((), (a, a')\bigr) = \delta(a, a'), \qquad \llbracket \mathsf{cap}(A) \rrbracket\bigl((a, a'), ()\bigr) = \delta(a, a'), \]

morphisms \(\mathbf 1 \to A \times A\) and \(A \times A \to \mathbf 1\). The snake equations

\[ (\mathrm{id}_A \boxtimes \mathsf{cup}(A)) ; (\mathsf{cap}(A) \boxtimes \mathrm{id}_A) = \mathrm{id}_A, \qquad (\mathsf{cup}(A) \boxtimes \mathrm{id}_A) ; (\mathrm{id}_A \boxtimes \mathsf{cap}(A)) = \mathrm{id}_A \]

reduce on entries to \(\bigoplus_{a, a'} \delta(x, a) \otimes \delta(a, a') \otimes \delta(a', y) = \delta(x, y)\). Every term but one is a product with a factor \(\mathbf 0\), so the equations hold whenever \(\mathbf 1\) is a unit of \(\otimes\), \(\mathbf 0\) absorbs, and \(\mathbf 0\) is the unit of \(\bigoplus\), which is true of every built-in instance. That cup, cap, and the tensor product make \(\mathbf{Mat}(\mathcal V)\) a compact closed category needs the semiring laws as well.

Dagger. For \(f : A \to B\), \(\llbracket f.\mathsf{dagger} \rrbracket(b, a) = \llbracket f \rrbracket(a, b)\), the transpose. Transposition reverses composition, \((f ; g)^\dagger = g^\dagger ; f^\dagger\), when \(\otimes\) is commutative, so it is a dagger on \(\mathbf{Mat}(\mathcal V)\) for a commutative semiring, and together with cup and cap it makes \(\mathbf{Mat}(\mathcal V)\) dagger compact (Selinger, 2007).

Trace. A traced monoidal category has an operation that feeds an output wire back into an input wire (Joyal et al., 1996). For \(f : A \times X \to A \times Y\), the partial trace along \(A\) is

\[ \llbracket f.\mathsf{trace}(A) \rrbracket(x, y) = \bigoplus_{a \in \llbracket A \rrbracket} \llbracket f \rrbracket\bigl((a, x), (a, y)\bigr). \]

For a commutative semiring it equals the composite \((\mathsf{cup}(A) \boxtimes \mathrm{id}_X) ; (\mathrm{id}_A \boxtimes f) ; (\mathsf{cap}(A) \boxtimes \mathrm{id}_Y)\), up to the associativity isomorphisms, and so satisfies the trace axioms of every compact closed category (Joyal et al., 1996). The categorical trace is unrelated to an execution trace, the record of the random choices of a run.

2.10 Currying

For \(f : X \times Y \to Z\), the postfixes f.curry_right and f.curry_left denote the two currying bijections of a closed monoidal category. \(\mathbf{Mat}(\mathcal V)\) under the Cartesian product is compact closed, with each object its own dual, so the internal hom \([Y, Z]\) is the product \(Y \times Z\), and the bijections (Mac Lane, 1998) are

\[ \mathbf{Mat}(\mathcal V)(X \times Y, Z) \cong \mathbf{Mat}(\mathcal V)(X, Y \times Z), \qquad \mathbf{Mat}(\mathcal V)(X \times Y, Z) \cong \mathbf{Mat}(\mathcal V)(Y, X \times Z) \]

The curried matrices are

\[ \llbracket f.\mathsf{curry\_right} \rrbracket(x, (y, z)) = \llbracket f \rrbracket((x, y), z), \qquad \llbracket f.\mathsf{curry\_left} \rrbracket(y, (x, z)) = \llbracket f \rrbracket((x, y), z). \]

The runtime class CurriedMorphism computes these matrices, with declared domain and codomain matching its tensor, and UncurriedMorphism is its inverse. With more than two domain factors, curry_right moves the last factor into the codomain and curry_left the first. This is the currying of a compact closed category, not that of the biclosed categories of categorial grammar (Schemas §3), whose internal homs are the slash types.

2.11 Freeze

\(\llbracket f.\mathsf{freeze} \rrbracket = \llbracket f \rrbracket\). The runtime copies the current entries into an ObservedMorphism, so that training does not change them.

2.12 Data

\(\llbracket \mathsf{from\_data}(\text{"K"}) \rrbracket = \rho_{\mathrm{data}}(\text{"K"})\), the matrix given at fit time under the key "K". Its shape must match the declared type, and training does not change it.

2.13 Contraction calls

For a contraction \(\mathit{op}\) with \(n\) inputs of types \(A_i \to B_i\) and wiring \(w\),

\[ \llbracket \mathit{op}(e_1, \dots, e_n) \rrbracket = \Phi_w\bigl(\llbracket e_1 \rrbracket, \dots, \llbracket e_n \rrbracket\bigr), \]

where \(\Phi_w\) is the einsum of Composition rules §4. The call checks that each argument has as many entries as its declared type, and a mismatch is a CompileError.

A program specialization such as p[Row](data) uses the static list to select the program's types before giving its runtime values. Its denotation is the corresponding member of the kernel family in Programs as kernels §3.1. The brackets do not change a contraction's parenthesized morphism inputs; contraction application combines the given morphisms under its declared wiring.

3. Copying, iteration, and recurrence

3.1 Fan

For \(e_i : X \to Y_i\),

\[ \llbracket \mathsf{fan}(e_1, \dots, e_n) \rrbracket\bigl(x, (y_1, \dots, y_n)\bigr) = \prod_{i=1}^{n} \llbracket e_i \rrbracket(x, y_i), \]

the composite of the copy map \(X \to X^n\) with \(e_1 \boxtimes \cdots \boxtimes e_n\). For kernels, the outputs are independent given the shared input \(x\), and they may be dependent once \(x\) is averaged over. A kernel \(X \to Y_1 \times Y_2\) is not determined by its two marginals, since other couplings have the same marginals, so fan is not a categorical product: \(\mathbf{FinStoch}\) and \(\mathbf{BorelStoch}\) are Markov categories, not Cartesian ones (Fritz, 2020). For finite weighted morphisms, \(\otimes\) replaces the product of numbers.

3.2 Stack

For \(e : X \to X\) and \(n \ge 1\), \(\llbracket \mathsf{stack}(e, n) \rrbracket = \llbracket e_1 \rrbracket ; \cdots ; \llbracket e_n \rrbracket\), where \(e_1, \dots, e_n\) are copies of \(e\) with separate parameters. The output of copy \(i\) is the input of copy \(i + 1\).

3.3 Repeat

For \(e : X \to X\), \(\llbracket \mathsf{repeat}(e, n) \rrbracket = \llbracket e \rrbracket ; \cdots ; \llbracket e \rrbracket\) (\(n\) times), with one set of parameters shared by every step. repeat(e) has \(n = 1\).

3.4 Scan

A cell \(e : A \times H \to H\) maps an input and a state to a new state. For an input sequence \(x = (x_1, \dots, x_T) \in A^T\), the scan runs

\[ h_0 = \mathit{init}, \qquad h_t = e(x_t, h_{t-1}) \quad (t = 1, \dots, T), \]

and \(\mathsf{scan}(e) : A^T \to H\) returns \(h_T\). With outputs=all it returns the trajectory \((h_1, \dots, h_T) \in H^T\), a sequence along the same axis, so that a second scan can run over it. With reverse=true the recursion runs \(h_t = e(x_t, h_{t+1})\) from \(h_{T+1} = \mathit{init}\), returns \(h_1\), and still indexes the trajectory by input position. The initial state is zeros, a learned vector, or an input label (init=h0), which each application of the scan binds to a value of the applying program (apply run_all [h0 = start]).

For a stochastic cell, the density of a whole trajectory is \(\sum_t \log p(h_t \mid x_t, h_{t-1})\), the joint of the states. The density of the final state alone is a different quantity, which integrates over every intermediate trajectory. The runtime class ScanMorphism scores trajectories: log_joint(x, hidden_states) takes the trajectory as a tensor of shape (batch, sequence length, state dimension), or as a dictionary keyed by state_key (default "h").

When a program draws a scan, the sites of its cell become sites of the program, visited once per position. The joint density is \(\sum_t \sum_s \log p(s_t \mid \cdots)\) over the sites \(s\) of the cell, and the \(t\)-th visit of a site \(s\) has the address s@t. let h <- sample scan(cell) thus scores the trajectory at h@0, ..., h@{T-1}. A reversed scan numbers its visits in the order it makes them, so s@n is input position \(T - n\) (positions counted from 1), while observations are read by input position. The scans reference gives the scoping and addressing rules.

4. Parser combinators

4.1 parser, ccg, and lambek

The keywords parser, ccg, and lambek are three spellings of one form. Each takes a list of rule names, resolved through the morphism, schema, and bundle environments (Schemas §7), the atomic syntactic categories, a start category \(s\), a bound \(d\) on the nesting depth of categorial types, and the type constructors allowed. From these it builds a lexical morphism, a binary rule morphism, and optionally a unary rule morphism over the finite set of categorial types of depth at most \(d\) (Schemas §4), and passes them to InsideAlgorithm.

The denotation sends a token string \(w = w_1 \cdots w_n\) to the log of the inside weight of the start category over the whole string,

\[ \llbracket \mathsf{parser}(\dots) \rrbracket(w) = \log \beta(s, 0, n), \]

where \(\beta(c, i, j)\) is the sum, over every derivation of \(w_{i+1} \cdots w_j\) from the category \(c\), of the product of the weights of the rules it uses. The runtime computes \(\beta\) by the CKY recursion in the log semiring, whatever the module's composition rule. When the lexical and rule morphisms are stochastic, so that the rules form a probabilistic context-free grammar, \(\beta(s, 0, n)\) is the inside probability of \(w\) (Baker, 1979; Goodman, 1999). The value depends only on the rules and the string, not on the order in which the chart is filled. A rule of weight zero contributes the exact log zero \(-\infty\), and a string with no derivation has that same log weight.

With a unary rule matrix \(U\), a chain of unary rules derives a category over a span from another category over the same span, so the inside weights of a cell are closed under unary chains: the runtime multiplies the cell that the lexical and binary rules produce, once, by the Kleene star \(U^* = I \oplus U \oplus U^2 \oplus \cdots\), the sum over every chain of the product of its weights (Goodman, 1999; Mohri, 2002). For probabilities \(U^* = (I - U)^{-1}\), which exists when the spectral radius of \(U\) is below one. A closure whose series diverges, as for a row-stochastic \(U\), whose spectral radius is one, has no value, and parsing raises DivergentClosureError. When a loop weight lies so close to one that rounding cannot decide whether the series converges, parsing raises UndecidedClosureError.

4.2 chart_fold

The chart recursion states which derivations a cell sums. The primitive chart_fold takes the three morphisms directly:

\[ \mathit{args} = \bigl(\mathit{lex} = \ell,\ \mathit{binary} = B,\ \mathit{unary} = U,\ \mathit{start} = s,\ \mathit{depth} = d,\ \mathit{effect\_depth} = k\bigr), \]

where \(\ell : \mathrm{Token} \to \mathrm{Cat}\) gives the lexical axioms, \(B : \mathrm{Cat} \times \mathrm{Cat} \to \mathrm{Cat}\) the binary rules, the optional \(U : \mathrm{Cat} \to \mathrm{Cat}\) the unary rules, \(s\) the start category, \(d\) the bound on the depth of categorial types, and \(k\) the number of effects a cell may stack (default 0). It denotes the same function of \(w\) as parser. With \(k > 0\) each chart entry also carries a stack of at most \(k\) effects, and the rules fire as described in Effects §4.

4.3 Deduction calls

The following calls act on weighted deduction systems:

  • parse(D, x) evaluates the deduction system \(D\) on the input \(x\) and returns its chart, the weight function \(\alpha : I \to K\) from the items \(D\) proves to its semiring \(K\);
  • parse_outcomes(D, x, key) and parse_normalized_outcomes(D, x, key) return typed weighted outcomes from a checked lexical LogProb deduction.
  • compose(D_1, D_2) builds the system whose axioms are the goal items of \(D_1\) and whose rules, semiring, agenda, and goal are those of \(D_2\) (Weighted deduction §9.1); fitting it fits the parameters of both;
  • subst(t, v, w) replaces each subterm of the item \(t\) equal to \(v\) by \(w\) (Weighted deduction §9.3).

A chart \(\alpha\) answers these method calls:

Call Value
chart.weight(i) \(\alpha(i)\) for a proved item \(i\)
chart.try_weight(i, d) \(\alpha(i)\) if \(i\) is proved, and otherwise \(d\), or \(\mathbf 0_K\) when \(d\) is omitted
chart.enumerate(p) the pairs \((i, \alpha(i))\) for the proved items \(i\) that match the pattern \(p\)
chart.goal_weight() \(\bigoplus_{i \in \mathrm{goal}} \alpha(i)\)
chart.attached_loss the sum of the losses attached to the chart
chart.semiring the semiring \(K\)

5. Equations

When the module's rule \(\mathcal V\) is a commutative semiring, the denotations satisfy the equations of a symmetric monoidal category (Morphisms §1.1):

Law Statement
Associativity of composition \(\llbracket (e_1 \mathbin{>\!\!>} e_2) \mathbin{>\!\!>} e_3 \rrbracket = \llbracket e_1 \mathbin{>\!\!>} (e_2 \mathbin{>\!\!>} e_3) \rrbracket\)
Units of composition \(\llbracket \mathsf{identity}(\tau) \mathbin{>\!\!>} e \rrbracket = \llbracket e \rrbracket = \llbracket e \mathbin{>\!\!>} \mathsf{identity}(\tau') \rrbracket\)
Associativity of @ \(\llbracket (e_1 \mathbin{@} e_2) \mathbin{@} e_3 \rrbracket = \llbracket e_1 \mathbin{@} (e_2 \mathbin{@} e_3) \rrbracket\), up to the regrouping of Types and spaces §5
Interchange \(\llbracket (e_1 \mathbin{>\!\!>} e_2) \mathbin{@} (e_3 \mathbin{>\!\!>} e_4) \rrbracket = \llbracket (e_1 \mathbin{@} e_3) \mathbin{>\!\!>} (e_2 \mathbin{@} e_4) \rrbracket\)
Symmetry \(\llbracket e_1 \mathbin{@} e_2 \rrbracket = \sigma_{X_1, X_2} ; \llbracket e_2 \mathbin{@} e_1 \rrbracket ; \sigma_{Y_2, Y_1}\)

The same equations hold for stochastic matrices and Markov kernels, whose composition and tensor product are those of \(\mathbf{FinStoch}\) and \(\mathbf{BorelStoch}\). For a rule that is not a semiring they may fail. The compiler evaluates an expression in the grouping written, so a rewrite that regroups is justified only under these hypotheses. Implementation correspondence lists the tests of these operations.

Cited sources

G. M. Kelly and M. L. Laplaza. Coherence for Compact Closed Categories. Journal of Pure and Applied Algebra, 19:193–213, 1980. doi:10.1016/0022-4049(80)90101-2.

Peter Selinger. A Survey of Graphical Languages for Monoidal Categories. In New Structures for Physics, Lecture Notes in Physics, pages 289–355. Springer, 2010. doi:10.1007/978-3-642-12821-9_4.

Peter Selinger. Dagger Compact Closed Categories and Completely Positive Maps. Electronic Notes in Theoretical Computer Science, 170:139–163, 2007. doi:10.1016/j.entcs.2006.12.018.

André Joyal, Ross Street, and Dominic Verity. Traced Monoidal Categories. Mathematical Proceedings of the Cambridge Philosophical Society, 119(3):447–468, 1996. URL: https://www.cambridge.org/core/journals/mathematical-proceedings-of-the-cambridge-philosophical-society/article/abs/traced-monoidal-categories/2BE85628D269D9FABAB41B6364E117C8, doi:10.1017/S0305004100074338.

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.

Tobias Fritz. A Synthetic Approach to Markov Kernels, Conditional Independence and Theorems on Sufficient Statistics. Advances in Mathematics, 370:107239, 2020. URL: https://www.sciencedirect.com/science/article/pii/S0001870820302656, doi:10.1016/j.aim.2020.107239.

J. K. Baker. Trainable Grammars for Speech Recognition. The Journal of the Acoustical Society of America, 65(S1):S132, 1979. doi:10.1121/1.2017061.

Joshua Goodman. Semiring Parsing. Computational Linguistics, 25(4):573–606, 1999. URL: https://aclanthology.org/J99-4004/.

Mehryar Mohri. Semiring Frameworks and Algorithms for Shortest-Distance Problems. Journal of Automata, Languages and Combinatorics, 7(3):321–350, 2002. doi:10.25596/jalc-2002-321.