Skip to content

DSL Declarations

A .qvr module is a sequence of declarations. program blocks and contractions of several inputs have their own pages (Programs and Let-Expressions, Contractions), and the Overview summarizes the grammar.

Algebra

composition NAME [level=...] selects the module's composition rule, the operations \(\otimes\) and \(\bigoplus\) that composition uses (Core Types). The choice follows what the entries mean: boolean asks whether a path exists, real is ordinary sum-product contraction, and markov composes conditional probabilities. Without a declaration the module uses product_fuzzy, which combines soft relation scores and does not normalize rows.

composition log_prob [level=algebra]
object A : FinSet 3
morphism f : A -> A [role=parameter]
export f

The built-in rules at the algebra level are product_fuzzy, boolean, lukasiewicz, godel, tropical, max_plus, log_prob, markov, real, probability, and counting, and Core Types gives each one's operations and standard name. The value of level names the class the rule must belong to, and the compiler checks the registered rule against it:

Level Class Laws the rule claims
algebra Algebra an identity (a Semiring also claims associativity)
semigroupoid Semigroupoid associativity (a Semiring also has an identity)
magmoid Magmoid neither
rule any CompositionRule none required

No law is checked (The hierarchy of composition rules). The operations identity(A), f.dagger, f.trace(A), cup(A), and cap(A) compile only when the module's rule is an Algebra. Composition rules gives the denotation of each level.

A rule can also be defined inline in an indented body, each entry a let-expression. An algebra body requires tensor_op, join, unit, and zero, and may add negation and meet, while semigroupoid and magmoid bodies require only tensor_op and join. The compiler checks the entries against the level.

composition my_sum_product [level=algebra]
    tensor_op(a, b) = a * b
    join(t)         = sum(t)
    unit            = 1.0
    zero            = 0.0

object A : FinSet 3
morphism f : A -> A [role=parameter]
define g = f >> f
export g

Object

An object declaration names a type. Its right side is an object expression:

object X : FinSet 3
object Y : FinSet 4
object XY : X * Y                              # ProductSet(components=(X, Y))
object Sum : X + Y                             # CoproductSet(components=(X, Y))
object Strings : FreeMonoid(X, max_length=4)   # words over X of length at most 4
object Atoms : {np, s, n}                      # a finite set of named elements
object Cat : FreeResiduated(Atoms, depth=2, ops=[slash])

morphism f : XY -> X [role=parameter]
export f

FreeMonoid(X, max_length=n) is the set of words over X of length at most n, the Kleene star of X truncated at n. FreeResiduated(Atoms, depth=d, ops=[...]) is the finite set of categorial (Lambek) types over the given atoms of depth at most d, built with the listed connectives (slash gives / and \) (Lambek, 1958). A name declared as an object can stand for its definition anywhere a type is expected, so morphism f : XY -> X above has the product as its domain.

Morphism

A morphism's role, in its option block, decides what is stored and learned:

Role What the declaration holds Typical use
parameter without a family clause a table of learnable parameters a learned soft relation or weight table
parameter with ~ Family(...) program draws from the family; a learnable table in compositions random values with a specified prior
observed fixed data a known relation
kernel, or no role a distribution for each input random outputs given inputs
function a learned deterministic map a neural network layer
discretize, embed a map between finite and continuous objects binning, embedding

The role parameter names a learnable table. A kernel's learned parameters and a value drawn from the kernel are different things: changing a parameter changes the distribution, and a draw uses the current distribution.

object X : FinSet 3
object Y : FinSet 4
object Z : FinSet 2

# A learnable table
morphism f : X -> Y [role=parameter]

# The same with a given initialization scale
morphism g : Y -> Z [role=parameter, scale=0.3]

# Fixed data
morphism h : X -> X [role=observed] ~ identity(X)

define fg = f >> g
export fg

Draws from a parameter morphism

A parameter declaration may end with ~ Family(args), which fixes the distribution of a program draw using the given literal hyperparameters. A program step let v <- sample W draws v from that family and scores it under it, on the compiled runtime, the reference machine, and every transpilation target that supports the declaration.

In a morphism expression such as f >> W, W is its learnable table, a LatentMorphism constructed as for a declaration without the clause. The family clause does not affect its fit (Morphisms §7). For a kernel, a network computes the family's parameters from each input. The over clause and other options go in the option block, before the ~, and the whole declaration is one line:

object D : FinSet 3
object K : FinSet 2

# A matrix-normal distribution for 3 x 2 tensor draws from W
morphism W : D -> K [role=parameter, over=[dom, cod]] ~ MatrixNormal(loc=0.0, row_covariance=1.0, col_covariance=1.0)
export W

Init recipes ([init=auto])

By default a learnable table starts from randn * scale. The option [init=auto] uses instead the initialization recipe of the module's Algebra, from Algebra.init_spec(depth, intermediate_size). For product_fuzzy, boolean, godel, lukasiewicz, and probability the recipe sets the value after the sigmoid of LatentMorphism, so the raw parameter is logit(value). For the other algebras it sets the raw parameter directly.

object D : FinSet 3
object K : FinSet 2
morphism W : D -> K [role=parameter, init=auto]
export W

The recipes start intermediate values near the Algebra's neutral element, which avoids saturated sigmoids for truth values and exploding products for multiplicative algebras. recommend_init and saturation_warnings in Fitting and Diagnostics inspect a trained module.

Space

Continuous types are objects too. Real n is \(\mathbb R^n\), bounds go in braces, and Simplex n is the probability simplex of dimension n:

object R3 : Real 3
object R2_bounded : Real 2 {low=0.0, high=1.0}
object U : Real 1 {low=0.0, high=1.0}
object P2 : Real 2 {low=0.0}
object S3 : Simplex 3

# Product space
object RU : R3 * U

object X : FinSet 2
morphism k : X -> RU ~ Normal
export k

They resolve to subclasses of ContinuousSpace (Continuous Spaces).

Named composites

object is the only declaration that introduces a type. Finite sets, continuous spaces, products, coproducts, and categorial types all enter the module as object declarations, and a later declaration can use the name wherever a type is expected.

object Hidden : Real 64
object Output : Real 32
object Combined : Hidden * Output

# Categorial (Lambek-style) types are object expressions too.
object Sentence : S \ NP

Kernel

A kernel morphism declares a Markov kernel A -> B. It is the default role, so a morphism without role= is a kernel. There are two forms:

  • Without ~, a kernel between finite sets is a lookup table, a learnable matrix of conditional probabilities. Each row is a distribution over the codomain, an element of the distribution monad \(\mathcal D(B)\) of Stochastic Morphisms.
  • With ~ Family, a kernel into a continuous space draws from the family, whose parameters a network computes from each input. Each input is thus sent to a probability measure on B, an element of \(\mathcal G(B)\) for the Giry monad \(\mathcal G\). The optional over clause sets which codomain axes form one joint event and which are independent copies.

A family call whose arguments are all literals fixes the family: morphism prior : A -> R ~ Normal(loc=0.0, scale=1.0) ignores its input, draws from that Normal for every element of A, and has no learned parameters. Writing ~ Normal without arguments lets the input choose the parameters through the kernel's parameter source. A literal may be a number, a vector, or a matrix, as in ~ Dirichlet(concentration=[1.0, 2.0, 3.0]). A family argument that reads a name, such as ~ Normal(scale=q), is refused, since a declaration binds no value for it to read and a kernel does not mix learned and given parameters. A draw with a given scale around a computed mean belongs in a program step, let s <- sample Normal(loc=m, scale=q), where both values are bound.

object X : FinSet 3
object Y : FinSet 4
object Z : FinSet 2
object R3 : Real 3
object S3 : Simplex 3

# Lookup-table kernels on finite sets
morphism s : X -> Y
morphism cat : X -> Y * Z

# Parametric kernels into continuous spaces
morphism f : X -> R3 ~ Normal
morphism g : R3 -> R3 ~ Normal
morphism k : X -> S3 ~ Dirichlet

# A conditional normalizing flow; its options go before the ~
morphism flow : R3 -> R3 [n_layers=6, hidden_dim=32] ~ Flow()

export s

More than fifty families are registered (Continuous Distribution Families). Flow is not one of them: it builds a conditional normalizing flow (Rezende and Mohamed, 2015) with n_layers coupling layers (4 by default) and hidden width hidden_dim (64 by default). The runtime chooses the table lookup or the parametric form by whether the codomain is a FinSet or a ContinuousSpace.

Latent weights

The weights of a network, whether a network kernel's ([param_source=mlp(...)] ~ Normal) or a learned function's, are point estimates by default. With [weights=latent] every weight and bias becomes a latent sample site with an independent Normal(0, prior_scale) prior (prior_scale is 1 by default), drawn once per run and shared by every row and position that applies the network. A Bayesian neural network regression (MacKay, 1992) is then one kernel:

object Feature : Real 2
object Target : Real 1
object Row : FinSet 6

morphism net : Feature -> Target [param_source=mlp(8), weights=latent, prior_scale=1.0] ~ Normal

program bnn : Row -> Target
    observe y : Row <- net(x)
    return y

export bnn
  • Sites. A program without value parameters that uses the network draws its weights at its start, under the names net_param_layer0_weight (rows by columns), net_param_layer0_bias, and so on, through net_param_weight and net_param_bias for the output layer. A scan's cell and a called program read the weights their caller draws. Guides, MCMC, and Predictive treat these sites like any other latent site, and the compiled program has no parameters of its own for them.
  • Scope. The option applies to a kernel whose family a network parameterizes and to a learned function, with a linear or mlp network. A draw through such a kernel names its inputs (net(x)), and the morphism composes in program steps or a scan, not in a define chain.
  • Transpilation. No transpile target supports latent network weights (Transpiler support). Writing the layers as factor expressions over sampled weight matrices gives a model that every target accepts.

Discretize

A discretize morphism maps a bounded one-dimensional space to a finite set of equal-width bins:

object U : Real 1 {low=0.0, high=1.0}

# Twenty bins on the unit interval
morphism d : U -> FinSet 20 [role=discretize, bins=20]
export d

Its runtime class is Discretize, which accepts one-dimensional spaces only.

Embed

An embed morphism maps a finite set into a continuous space:

object X : FinSet 3
object R3 : Real 3

morphism e : X -> R3 [role=embed]
export e

Its runtime class Embed gives each element \(i\) a Normal distribution with a learnable center and a learnable scale, \(p(y \mid i) = \mathcal N(y; \mu_i, \sigma_i)\).

Function

A neural network layer is a learned function with no noise: a projection \(z = Wx + b\), a recurrent update \(h_t = \tanh(W [x_t; h_{t-1}] + b)\), a feature map. role=function declares one between continuous spaces:

object Feature : Real 3
object Hidden : Real 4
object Score : Real 2

# Affine by default: W x + b.
morphism proj : Feature -> Score [role=function]
# Two hidden layers of 16 under ReLU, then a tanh on the output.
morphism cell : Feature * Hidden -> Hidden [role=function, param_source=mlp(16, 16), activation=relu, output=tanh]
  • Network. param_source selects it: linear (the default), mlp with optional hidden widths (mlp(16, 16), and bare mlp is two layers of 64), or identity, which has no weights and needs a domain of one factor as wide as the codomain. activation names the nonlinearity between an mlp's hidden layers (tanh by default, or relu, sigmoid, softplus), and output the one applied to the network's output (identity by default, or tanh, sigmoid, softplus, relu).
  • No site. The function is the Dirac kernel at its output: every input goes to a point mass. A program calls it as a value, let z = proj(x), with one argument per factor of its domain, and never draws from it, so let z <- sample proj(x) is refused. In a chain it composes like any morphism, and as the cell of a scan it gives a deterministic recurrence.
  • Weights as point estimates. Its weights are parameters of the compiled program, fit by whatever objective trains the model, as a network kernel's are. The checked core reads them as typed program inputs (proj_param_weight, proj_param_bias, and cell_param_layer<k>_weight and _bias for hidden layers), as the learned functions reference states.

The runtime class is LearnedFunction. A linear Gaussian state-space model draws each state around a learned function of the previous state and the input. The cell step below reads the noise scales q and r as input labels, which the calling program ssm draws and binds at the apply step, apply run_all [q, r]:

object Driver : Real 2
object State : Real 3
object Obs : Real 2

morphism transition : Driver * State -> State [role=function]
morphism emission : State -> Obs [role=function]

program step(u_t, s_prev) : Driver * State -> State
    let mean = transition(u_t, s_prev)
    let s <- sample Normal(loc=mean, scale=q)
    let center = emission(s)
    observe o <- Normal(loc=center, scale=r)
    return s

define run_all = scan(step)

program ssm : Driver -> State
    let q <- sample HalfNormal(scale=1.0)
    let r <- sample HalfNormal(scale=1.0)
    let last <- apply run_all [q, r]
    return last

export ssm

Sequence functions: self-attention and position tables

Two networks read a whole sequence at once instead of one row at a time. A function declared with param_source=self_attention is multi-head scaled dot-product self-attention (Vaswani et al., 2017): it maps a sequence of T rows to a sequence of T rows, and the row at position i is a weighted sum of value projections of every row it attends to. A function declared with param_source=positions(N) adds a learned vector to each row, the t-th row of an N-row table to position t, which is how a model that attends tells positions apart.

object Model : Real 16

# Two heads of width 8; position i attends to positions 0 through i.
morphism attn : Model -> Model [role=function, param_source=self_attention, heads=2, causal=true]
# One learned vector per position, for sequences of up to 64 positions.
morphism positions : Model -> Model [role=function, param_source=positions(64)]
  • Options. heads (one by default) must divide the model width \(m\), the width of the codomain, and each head reads \(m / \mathrm{heads}\) coordinates of the query, key, and value projections. causal is required, since the mask decides what the model may read: causal=true lets position i attend to positions j <= i only, as a language model scoring the next token needs, and causal=false lets every position attend to every position. The domain is one space, not a product, and a position table's codomain is its domain. Neither network takes activation or output, and both refuse weights=latent, so their weights are point estimates.
  • Sequences, not rows. A program calls a sequence function in a let on a table Real[Fin T, Fin d]: the output of a scan with outputs=all, a let computed from one, or a table given as data. The value is a table of the same length. T is read from the data, so one program handles sequences of any length, up to N for a position table. A sequence function cannot be a link of a chain or the cell of a scan, which apply their morphisms one position at a time, and the compiler refuses such a chain where it is drawn.
  • Composition by let. A residual connection adds tables, let h = u + attn(layer_norm(u)), and layer_norm normalizes each row (Ba et al., 2016).

A scan whose cell ignores its state runs a morphism at every position. A decoder-only language model thus embeds each token in one scan, attends across the sequence in a let, and scores each next token in a second scan:

object Vocab : Real 8
object Model : Real 16
object Logits : Real 8

morphism embed : Vocab -> Model [role=function]
morphism positions : Model -> Model [role=function, param_source=positions(32)]
morphism attn : Model -> Model [role=function, param_source=self_attention, heads=2, causal=true]
morphism ff : Model -> Model [role=function, param_source=mlp(32), activation=relu]
morphism lm_head : Model -> Logits [role=function]

program embed_step(x_t, s_prev) : Vocab * Model -> Model
    let e = embed(x_t)
    return e

program head_step(h_t, s_prev) : Model * Model -> Model
    let z = h_t + ff(layer_norm(h_t))
    let logits = lm_head(layer_norm(z))
    observe next_token <- Categorical(logits=logits)
    return z

define embedded = scan(embed_step, outputs=all)
define scored = scan(head_step, outputs=all)

program transformer_lm : Vocab -> Model
    let e <- apply embedded
    let u = positions(e)
    let h = u + attn(layer_norm(u))
    let out <- apply scored(h)
    return out

export transformer_lm

Each input row is one sequence of one-hot tokens, of shape (batch, T, 8), and the observation next_token, of shape (batch, T), holds the token after each position. With the source above saved as transformer_lm.qvr:

import torch
from quivers.dsl import load

model = load("transformer_lm.qvr").morphism
tokens = torch.randint(0, 8, (4, 9))
x = torch.nn.functional.one_hot(tokens, 8).float()
next_token = torch.randint(0, 8, (4, 9))
log_joint = model.log_joint(x, {"next_token": next_token})  # shape (4,)

The runtime class is SequenceFunction over a SelfAttentionSource or a PositionSource. The checked core builds the same map from comprehensions over the open sequence length, with the weights as typed program inputs, as the sequence functions reference states.

Deduction

deduction NAME : Domain -> Codomain [options] with an indented body declares a weighted deduction system that an agenda algorithm evaluates (Eisner et al., 2005). The body gives the item constructors and the rules, and the option block gives the semiring, the goal, and the evaluation bounds:

import std.semiring

object Term : FinSet 16

deduction CCG[S = std.semiring.LogProb] : Term -> Term [start=S, depth=6, tolerance=0.0]
    atoms NP, S, N, VP, PP, Fwd, Bwd, span
    rule fwd_app : span(I, K, Fwd(X, Y)), span(K, J, Y) |- span(I, J, X)
    rule bwd_app : span(I, K, Y), span(K, J, Bwd(X, Y)) |- span(I, J, X)
  • atoms NAME, NAME, ... lists the constructor symbols. In a rule pattern an identifier that is neither a declared atom nor a numeral is a pattern variable, whatever its spelling, and a variable that occurs twice in a rule must match the same term at both occurrences. A declared atom in a slot for a chart position, such as the first two slots of span, is a compile error naming the rule and the identifier. A premise or conclusion that is a bare variable, and does not also occur inside a term of the rule, ranges over every judgment form: the rule is the family of its instances, one per form, which share its weight parameter.
  • rule NAME : premises |- conclusion is an inference rule with any number of comma-separated premises. A trailing #[learnable] pragma gives the rule a learnable weight, one for each distinct binding of its category variables and shared across chart positions, so a rule whose variables are all positions has one weight.
  • The semiring is the header parameter deduction G[S = std.semiring.X], naming LogProb, Viterbi, Boolean, Counting, Tropical, or Godel (semirings); without one a deduction runs in LogProb. A learned rule weight is a log weight, so it enters the chart as it is under LogProb and Viterbi, whose elements are log weights (ChartSemiring.log_weights), and contributes the unit under Boolean and Counting.
  • start= names the category of the goal items. With a lexicon, a goal span item must also cover the whole input; without start= every item is a goal.
  • depth= bounds the constructor depth of the categories in rule conclusions, so the agenda terminates on cyclic rules.
  • tolerance= (optional) stops the iteration of a cyclic chart once its weights change by less than the tolerance, which approximates the Kleene closure of a cycle (Mohri, 2002). The chart engine returns the stopped weights. The reference machine does not treat a stop within the tolerance as the value: it raises ContractNotMet (the deduction as a QIEC computation). It is used with rule weights marked #[learnable, bounded], which keeps cyclic weights below one.

Slash, product, and modal type constructors are declared atoms, not built-in syntax. In Steedman notation (Steedman, 2000), Fwd(X, Y) is X/Y, which takes a Y on its right to give X, and Bwd(X, Y) is X\Y, which takes a Y on its left. The usual further names are Tns(X, Y) for a product, Dia(X) and Box(X) for the modalities ◇ and □, and Cont(X) for the category of a continuation, and a grammar may declare any others.

Axioms

The axioms are the weighted items the input contributes. A deduction obtains them in one of three ways:

Form Use
a lexicon block of "word" : Cat = lf entries, optionally #[learnable] a lookup table written in the body
lexicon from "path.tsv" [learnable=true] a lookup table read from a TSV file at compile time
[axioms=some_morphism] in the option block a declared morphism from the input to weighted items
import std.semiring

object Term : FinSet 16

deduction PCFG[S = std.semiring.LogProb] : Term -> Term [start=S, depth=4]
    atoms S, NP, VP, Det, N, V, span, the, cat, sleeps
    rule s_np_vp : span(I, K, NP), span(K, J, VP) |- span(I, J, S) #[learnable]
    rule np_det_n : span(I, K, Det), span(K, J, N) |- span(I, J, NP) #[learnable]
    rule vp_v : span(I, J, V) |- span(I, J, VP) #[learnable]
    lexicon
        "the"    : Det = the    #[learnable]
        "cat"    : N   = cat    #[learnable]
        "sleeps" : V   = sleeps #[learnable]

A #[learnable] lexicon entry gets a log weight, a PyTorch nn.Parameter initialized to 0.0.

Expressions that query a chart

A program parses its input with parse, and the result is a chart that answers queries:

import std.semiring

object Term : FinSet 16
object Weight : Real 1

deduction PCFG[S = std.semiring.LogProb] : Term -> Term [start=S, depth=4]
    atoms S, NP, VP, Det, N, V, span, the, cat, sleeps
    rule s_np_vp : span(I, K, NP), span(K, J, VP) |- span(I, J, S) #[learnable]
    rule np_det_n : span(I, K, Det), span(K, J, N) |- span(I, J, NP) #[learnable]
    rule vp_v : span(I, J, V) |- span(I, J, VP) #[learnable]
    lexicon
        "the"    : Det = the     #[learnable]
        "cat"    : N   = cat     #[learnable]
        "sleeps" : V   = sleeps  #[learnable]

program parse_score : Term -> Weight
    let chart = parse(PCFG, sentence)
    score w = chart.goal_weight()
    return w

export parse_score

Here sentence is a data input, given with the observations. The chart's ChartView methods are:

  • chart.weight(item): the weight of one item;
  • chart.enumerate(pattern): the (item, weight) pairs matching a pattern with variables;
  • chart.derivations(item): the rule applications that derive item, its incoming hyperedges (Klein and Manning, 2001), as (rule_index, rule_name, antecedents) triples with the rule's position in the system, so two rules of one name stay apart, and the antecedent items in premise order;
  • chart.goal_weight(): the weight of the goal.

The weights are tensors, and their gradients reach every learnable rule and lexicon weight through the semiring's operations.

Replicated declarations

[replicate=N] declares N copies of a morphism with independent parameters, named NAME_0, ..., NAME_{N-1}. The base name then refers to the group, which fan expands:

object Latent : Real 4
object HeadOut : Real 2
object State : FinSet 3
object Obs : FinSet 2
object Token : FinSet 5
object Hidden : Real 3

# head_0, head_1, head_2, head_3, each with its own parameters
morphism head : Latent -> HeadOut [replicate=4] ~ Normal

# Replication works for lookup-table kernels and embeddings as well
morphism T : State -> Obs [replicate=3]
morphism tok : Token -> Hidden [role=embed, replicate=2]

define multi_head = fan(head)
export multi_head

Combinators on morphisms

The combinators below build morphisms from simpler ones in define declarations at the top level of a module, outside program blocks.

Fan-out (diagonal morphism)

fan(f, g, ...) sends one input to several morphisms with the same domain and returns all their outputs together. Its codomain is the product of their codomains, laid out by concatenating their outputs, so a vector output's width is the sum of the widths. f @ g, in contrast, expects a pair of inputs (Morphisms). fan takes morphism names or the name of a replicated group:

object Latent : Real 4
object HeadOut : Real 2
object Both : Real 8

morphism head : Latent -> HeadOut [replicate=4] ~ Normal

# fan(head) expands to fan(head_0, head_1, head_2, head_3)
define multi_head = fan(head)

# often followed by a projection that recombines the outputs
morphism proj : Both -> Latent ~ Normal
define recombined = fan(head) >> proj
export recombined

The morphisms keep their own parameters.

Repeat (iterated composition)

repeat(f, n) composes an endomorphism with itself n times using the same parameters at every step, as a transition applied several times. stack uses a fresh copy at each step, and a scan also reads a new input at each step. The three differ in which parameters the steps share.

object State : FinSet 3
object Obs : FinSet 2

morphism transition : State -> State
morphism emission : State -> Obs

# transition >> transition >> transition, one set of parameters
define deep = repeat(transition, 3)

# repeat(f, 1) is f
define same = repeat(transition, 1)

# the number of steps set at run time
define n_step = repeat(transition) >> emission

export n_step

Without a count, repeat(f) builds a RepeatMorphism whose number of steps is set by Program.forward(n_steps=N) and computed by repeated squaring in \(O(\log n)\) compositions:

prog = load("hmm.qvr")
obs_3 = prog(n_steps=3)    # T^3 >> E
obs_50 = prog(n_steps=50)  # T^50 >> E, same model, different length

Stack (independent multi-layer)

stack(f, n) composes n copies of an endomorphism f, each with its own parameters:

object State : FinSet 3

morphism transition : State -> State

# 3 layers, each with its own parameters
define deep = stack(transition, 3)

# 3 iterations sharing one set of parameters
define tied = repeat(transition, 3)

export deep

Scan (temporal recurrence)

scan(cell) threads a hidden state through a sequence. The cell has a product domain A * H -> H, where A is the type of one input and H the type of the state, and scan(cell) : A -> H, with the sequence on the second axis of the input tensor, [batch, seq_len, input_dim].

object Token : FinSet 256
object Embedded : Real 64
object Hidden : Real 128
object Output : Real 64

morphism tok_embed : Token -> Embedded [role=embed]
morphism cell : Embedded * Hidden -> Hidden ~ Normal
morphism output_proj : Hidden -> Output ~ Normal

# the last state, read by a projection
define rnn = tok_embed >> scan(cell) >> output_proj

# a learned initial state (zeros by default)
define rnn_learned = tok_embed >> scan(cell, init=learned) >> output_proj

# every state rather than the last, and a scan from the last position to the first
define states = tok_embed >> scan(cell, outputs=all)
define backward = tok_embed >> scan(cell, reverse=true)

export rnn

For a sequence \(x_0, \dots, x_{T-1}\) and an initial state \(h_{\mathrm{init}}\), the scan computes

\[ h_{-1} = h_{\mathrm{init}}, \qquad h_t = \mathrm{cell}(x_t, h_{t-1}) \quad (t = 0, \dots, T - 1), \]

so \(h_t\) is the state after reading \(x_t\).

  • Initial state. \(h_{\mathrm{init}}\) is zeros by default, a learned vector with init=learned, or, for a scan inside a program, the input label h0 of init=h0, which the apply step running the scan binds to a value of the program (apply run_all [h0 = start]).
  • Output. outputs=last (the default) returns \(h_{T-1}\), and outputs=all returns the sequence \(h_0, \dots, h_{T-1}\).
  • Direction. reverse=true visits the positions from last to first, \(h_t = \mathrm{cell}(x_t, h_{t+1})\) with \(h_T = h_{\mathrm{init}}\), and returns \(h_0\). Its sequence of states is still laid out in input order, entry \(t\) being the state after reading \(x_t\).
  • Options. init, outputs, and reverse may appear in any order, each at most once.
  • Cells. A cell may be a parametric kernel (morphism cell : A * H -> H ~ Normal), a learned function ([role=function], a deterministic recurrence with no site), or a program (program cell(x, h) : A * H -> H). Inside a program a scan's step runs once per position. A kernel cell is drawn at the scan's binding, with sites h@0, h@1, ..., for let h <- sample scan(cell). A name a program cell reads without binding it is an input label, which the apply step binds to a name of its program, apply run_all [W = weights], or by the pun [W] to the name spelled like it. The cell's sites are addressed s@0, s@1, ..., and an observe in the cell scores one observation per position. The scans reference states the rules, including input labels.

Scans stack. Each scan but the last returns every state with outputs=all, and the next one reads that sequence one state per position:

object Token : FinSet 16
object Embedded : Real 4
object H1 : Real 3
object H2 : Real 3
object Output : Real 2

morphism tok_embed : Token -> Embedded [role=embed]
morphism cell_1 : Embedded * H1 -> H1 ~ Normal
morphism cell_2 : H1 * H2 -> H2 ~ Normal
morphism output_proj : H2 -> Output ~ Normal

define deep_rnn = tok_embed >> scan(cell_1, outputs=all) >> scan(cell_2) >> output_proj
export deep_rnn

Each scan threads its own state. A morphism between two scans, scan(cell_1, outputs=all) >> proj >> scan(cell_2), runs at every position inside the second scan's step. A scan over the final state of another, scan(cell_1) >> scan(cell_2), is refused, since one state is not a sequence, and so is a morphism applied to a whole sequence of states outside a scan.

fan and @ pair two sequences of states position by position, concatenating the two states at each position. A bidirectional recurrence runs a forward and a reversed scan over the same tokens and reads both at every position with a third scan:

object Token : FinSet 16
object Embedded : Real 4
object Forward, Backward : Real 3
object Both : Real 6
object Tag : Real 2

morphism tok_embed : Token -> Embedded [role=embed]
morphism forward_cell : Embedded * Forward -> Forward ~ Normal
morphism backward_cell : Embedded * Backward -> Backward ~ Normal
morphism tag_cell : Both * Tag -> Tag ~ Normal

define forward_states = tok_embed >> scan(forward_cell, outputs=all)
define backward_states = tok_embed >> scan(backward_cell, outputs=all, reverse=true)
define tagger = fan(forward_states, backward_states) >> scan(tag_cell, outputs=all)

program tag_sequence : Token -> Tag
    let tags <- apply tagger
    return tags

export tag_sequence

At position \(t\) the tag cell reads the forward state after \(x_0, \dots, x_t\) and the backward state after \(x_{T-1}, \dots, x_t\). The two sequences pair by input position, so a reversed scan's states need no reindexing. Sites are numbered in the order the run reaches them, so the reversed scan's occurrence @0 is its draw at the last position.

Marginalized chains. A program cell whose state is a finite latent variable that it marginalizes and returns is one step of a discrete Markov chain, and scan(cell, init=p) sums out every path of states with the forward algorithm (Rabiner, 1989), in \(O(T K^2)\) time for \(T\) positions and \(K\) states. With emissions that observe the scanned symbols, this is a hidden Markov model:

object State : FinSet 2
object Symbol : FinSet 4
object StartProbs : Real 2

program hmm_step(symbol, z_prev) : Symbol * State -> State
    marginalize z : State <- Categorical(probs=transition[z_prev])
        observe symbol <- Categorical(probs=emission[z])
    return z

define chain = scan(hmm_step, init=start)

program hmm : Symbol -> StartProbs
    let start <- sample Dirichlet(concentration=1.0) [over=State]
    let transition : State <- sample Dirichlet(concentration=1.0) [over=State, iid_over=State]
    let emission : State <- sample Dirichlet(concentration=1.0) [over=Symbol, iid_over=State]
    let path <- apply chain [transition, emission, start]
    return start

export hmm

start is the distribution of the state before the first position, and the cell reads transition and emission as input labels, all three bound by the apply step; transition[z_prev] moves the chain, and the block scores each position's symbol at the new state. The chain adds its log marginal likelihood to the joint under the name path, which holds no state and is not read again. The cell's let steps may read the scanned input, as an input-output model's transitions do, and an observation under a name other than the input's is one observation per position. The marginalized chains reference states the rules and the refused forms, and chapter 5 of the tutorial compares the marginal with a forward recursion written in PyTorch.

Backward composition

<< writes composition right to left: g << f is f >> g. Both operators compose in the operands' shared Algebra and reject a mismatch, and an explicit .change_base between segments crosses algebras (Transformations).

object X : FinSet 3
object Y : FinSet 4
object Z : FinSet 2

morphism f : X -> Y [role=parameter]
morphism g : Y -> Z [role=parameter]

define fg = f >> g
define gf = g << f    # the same composite as fg
export gf

Curry combinators

Currying passes the arguments of a function of two arguments one at a time: a function of a pair (x, y) becomes a function that takes x and returns a function awaiting y. For morphisms between finite sets, a morphism into \(Y \times Z\) plays the part of that returned function, since finite sets with weighted matrices form a compact closed category in which the internal hom from \(Y\) to \(Z\) is \(Y \times Z\) (Mac Lane, 1998). For \(f : X \times Y \to Z\) the postfix combinators give

Postfix Result Entries
f.curry_right a morphism X -> Y * Z g[x, (y, z)] = f[(x, y), z]
f.curry_left a morphism Y -> X * Z g[y, (x, z)] = f[(x, y), z]

The entries are those of f, read with a different split into domain and codomain axes, so the domain must be a product of at least two factors.

object X : FinSet 3
object Y : FinSet 4
object Z : FinSet 5
morphism f : X * Y -> Z [role=parameter]
define g = f.curry_right    # g : X -> Y * Z
export g

Top-level define declarations

define binds a morphism expression at the top level of a module:

object X : FinSet 2
object Y : FinSet 3
object Z : FinSet 2
object W : FinSet 4

morphism f : X -> Y
morphism g : Y -> Z
morphism h : Z -> W

define fg = f >> g
define par = f @ g
define marg = par.marginalize(Y)
define composed = f >> g >> h

let binds values inside a program block (Programs and Let-Expressions).

where clauses

where attaches local define bindings, visible only in the enclosing definition:

object Token : FinSet 4
object State : FinSet 3
object Obs : FinSet 2

morphism tok_embed : Token -> State
morphism transition : State -> State
morphism output_proj : State -> Obs

define model = embed >> layers >> output_proj where
    define layers = stack(transition, 3)
    define embed = tok_embed

export model

Indexed families and effect declarations

Six declarations belong to the Quivers Indexed Effect Core (QIEC), the typed core language of QVR (language reference). They state contracts that a tensor shape cannot. An index introduces a finite enumerated index type and its constructors. A family declares an indexed family (Dybjer, 1994), a type such as vectors of a given length whose constructors fix the index, with type parameters in square brackets and indices in parentheses. An effect declares the operations of an effect interface, and an instance creates a named occurrence of one. A handler interprets the operations of an instance, and a typed define names a computation that a program or another computation calls.

index Nat = Z | S(Nat)

family Vec[A : Type](n : Nat) : Type
    constructor Nil : Vec[A](Z)
    constructor Cons[m : Nat] : A * Vec[A](m) -> Vec[A](S(m))

effect Abort[E : Type]
    abort : E -> Unit

instance stop : Abort[String]

handler ignore[E : Type, A : Type] for Abort[E] : A -> A [coverage=partial, implementation=foreign]
    abort resumes 0

A constructor's result names its family with explicit indices, so a case analysis on a Vec[A](S(m)) knows the vector is nonempty. A handler's options give its coverage (total or partial), its forwarding of uncovered operations (forwards=unknown|none), and the effects it introduces (introduces=!{...}). Each operation clause has a resumption grade: 0 never resumes, aff resumes at most once, 1 exactly once, and omega any number of times when the captured context can be copied. A handler's clause bodies are either written in QVR ([implementation=authored], each clause a => body that may resume) or given at run time by a Python provider ([implementation=foreign]). Types and indexed families, effects and handlers, and computations specify these forms.

Tree and graph autoencoders: syntax, encoder, decoder, loss

These four declarations define autoencoders over terms. A syntax declares sorts and constructors, with the variables each argument binds, an encoder folds a term into a vector with one learnable function per constructor, a decoder defines a distribution over terms given a vector, and a loss attaches a training objective to part of the module. Signatures and Encoders and Decoders and Losses describe them. A syntax may also state equations between its terms, and a judgment declares a relation over them, such as a typing relation (Object languages).

Export

A declaration is private to its file unless it is exported, either by writing export before it or by naming it in an export statement. Exported declarations are what other files see when they import this one. A file that is compiled or run directly sees all of its own declarations, exported or not.

Among the exports, the first that is a morphism or a program is the compiled module's primary output, and every later morphism or program export is reachable by name on the compiled object:

object A : FinSet 2
object B : FinSet 3
object R : Real 1

morphism f : A -> B
morphism g : B -> A

define fg = f >> g

program my_prog : A -> R
    let x <- sample Normal(loc=0.0, scale=1.0)
    return x

export f
export fg
export my_prog

The same exports can be written on the declarations themselves, export morphism f : A -> B or export program my_prog : .... A name spelled like a declaration keyword is exported in parentheses, export (encoder).

Cited sources

Joachim Lambek. The Mathematics of Sentence Structure. The American Mathematical Monthly, 65(3):154–170, 1958. URL: https://www.tandfonline.com/doi/abs/10.1080/00029890.1958.11989160, doi:10.1080/00029890.1958.11989160.

Danilo Rezende and Shakir Mohamed. Variational Inference with Normalizing Flows. In Proceedings of the 32nd International Conference on Machine Learning, volume 37 of Proceedings of Machine Learning Research, 1530–1538. 2015. URL: https://proceedings.mlr.press/v37/rezende15.html.

David J. C. MacKay. A Practical Bayesian Framework for Backpropagation Networks. Neural Computation, 4(3):448–472, 1992. URL: https://doi.org/10.1162/neco.1992.4.3.448, doi:10.1162/neco.1992.4.3.448.

Ashish Vaswani, Noam Shazeer, Niki Parmar, Jakob Uszkoreit, Llion Jones, Aidan N. Gomez, Łukasz Kaiser, and Illia Polosukhin. Attention is All you Need. In Advances in Neural Information Processing Systems, volume 30. 2017. URL: https://papers.nips.cc/paper/2017/hash/3f5ee243547dee91fbd053c1c4a845aa-Abstract.html.

Jimmy Lei Ba, Jamie Ryan Kiros, and Geoffrey E. Hinton. Layer Normalization. arXiv preprint arXiv:1607.06450, 2016. URL: https://doi.org/10.48550/arXiv.1607.06450, doi:10.48550/arXiv.1607.06450.

Jason Eisner, Eric Goldlust, and Noah A. Smith. Compiling Comp Ling: Practical Weighted Dynamic Programming and the Dyna Language. In Proceedings of Human Language Technology Conference and Conference on Empirical Methods in Natural Language Processing, 281–290. Association for Computational Linguistics, 2005. URL: https://aclanthology.org/H05-1036/.

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.

Mark Steedman. The Syntactic Process. MIT Press, 2000. URL: https://direct.mit.edu/books/book/4283/The-Syntactic-Process, doi:10.7551/mitpress/6591.001.0001.

Dan Klein and Christopher D. Manning. Parsing and Hypergraphs. In Proceedings of the Seventh International Workshop on Parsing Technologies, 123–134. 2001. URL: https://aclanthology.org/W01-1812/.

L. R. Rabiner. A tutorial on hidden Markov models and selected applications in speech recognition. Proceedings of the IEEE, 77(2):257–286, 1989. URL: https://doi.org/10.1109/5.18626, doi:10.1109/5.18626.

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.

Peter Dybjer. Inductive Families. Formal Aspects of Computing, 6(4):440–465, 1994. doi:10.1007/BF01211308.