Term Autoencoder¶
Overview¶
A stochastic autoencoder on simply typed lambda terms, built from the four structural declarations: a syntax fixing the multi-sorted term algebra, an Encoder folding each term bottom-up into one fixed-width code vector, a Decoder corecursively expanding a code back into a term, and a loss head scoring the round trip by reconstruction negative log-likelihood.
This example wires all four declarations together in one module. It shows the keyword-led encoder body (an explicit op rule alongside compiler-scaffolded defaults), the per-encoder / per-decoder dim override, a binder that threads a de Bruijn type annotation through the recursion, and a loss attached to a named site via [on=...]. The #! comments describe the following declaration.
QVR source¶
# Term Autoencoder
#
# A stochastic autoencoder on simply typed lambda terms: a
# multi-sorted syntax fixes the term algebra, an encoder folds
# each term bottom-up into one fixed-width code vector, a decoder
# corecursively expands a code back into a term, and a loss head
# scores the round trip by reconstruction negative log-likelihood.
#! Simply typed lambda terms: Term and Type are the recursively
#! decoded object sorts; Name is a data sort with a closed
#! vocabulary that the decoder samples and scores leaves from.
syntax STLC
sorts
Term : object
Type : object [dim=24]
Name : data [dim=24, vocab=["f", "x", "plus", "base"]]
constructors
Const : Name -> Term
App : Term, Term -> Term
Base : Name -> Type
constructors
Lam : (ty : Type), (x : Term : ty).Term -> Term
#! Fold a term bottom-up into one code vector. App carries an
#! explicit op rule; every other operator keeps its learned
#! scaffold. Bound variables enter the context as their type's
#! annotation embedding.
encoder Enc : STLC
dim Term = 24
op App(fun, arg) |-> relu(fun + arg)
var_init Term from Type as ty |-> ty
#! Expand a code vector back into a term, scoring constructor,
#! binder, and bound-variable candidates at every node down to a
#! bounded depth.
decoder Dec : STLC [depth=6]
dim Term = 24
body |-> recursive
#! Reconstruction negative log-likelihood of the round trip.
loss reconstruct [weight=1.0, on=decoder(Dec)]
-Dec.log_prob(term, Enc(term))
Understanding the model¶
Consider an application tree App(function, argument). The encoder first
encodes the function and argument subtrees, then combines those vectors into
one representation. A decoder runs in the generative direction: it chooses
a node kind and then predicts children. It thus defines a distribution
over possible trees rather than an exact inverse of compression.
Reconstruction negative log likelihood rewards assigning high probability
to the original tree. It can train without sampling the correct tree on every
iteration. Binder scope is an additional structural constraint: a de Bruijn index 0 refers to the nearest enclosing binder, so changing a variable's
printed name does not change which binder it denotes.
Walkthrough¶
The syntax¶
syntax STLC
sorts
Term : object
Type : object [dim=24]
Name : data [dim=24, vocab=["f", "x", "plus", "base"]]
constructors
Const : Name -> Term
App : Term, Term -> Term
Base : Name -> Type
constructors
Lam : (ty : Type), (x : Term : ty).Term -> Term
Term and Type are object sorts, decoded recursively. Name is a data sort whose closed vocab provides the tokens the decoder samples and scores leaves from. Term deliberately omits its dim: each encoder or decoder over the syntax must then provide one via a dim Term = 24 body entry, and the compiler raises when neither the syntax nor the block does. The Lam binder introduces a Term-sorted variable annotated by a Type. The framework threads that annotation through a typed de Bruijn context during both encoding and decoding.
The encoder¶
encoder Enc : STLC
dim Term = 24
op App(fun, arg) |-> relu(fun + arg)
var_init Term from Type as ty |-> ty
Every encoder body entry is keyword-led. The op keyword introduces a per-operation rewrite rule: App's two child embeddings are summed and passed through relu. Operators without a rule (Const, Base, Lam) receive a compiler-scaffolded 2-layer MLP with the correct per-argument dimensions, so a body may override as few or as many operations as the model demands. The var_init entry sets the embedding a Lam-bound variable enters the context with. Here the type annotation's embedding is passed through unchanged.
The decoder¶
The decoder header names its syntax after :, exactly like the encoder header. body |-> recursive accepts the scaffolded corecursion: at every node the runtime scores constructor, binder, and bound-variable candidates for the current sort, splits the parent vector into per-child vectors, and recurses down to the depth bound. Each slot (structure, primitive, factor, binder selection) can instead be overridden with an explicit rule, as described in the decoders and losses guide.
The loss¶
A loss declaration registers a weighted scalar head in the module's LossRegistry. The body is compiled as a let-expression closure over an environment: Enc and Dec resolve to the compiled module's own artifacts, while term is provided by the caller at evaluation time. The on=decoder(Dec) option attaches the entry to a named site, so a training driver may evaluate it selectively via evaluate_on. weight=1.0 sets the multiplier applied by evaluate.
The same declarations lower to a checked QIEC graph. STLC becomes an opaque term family indexed by Term, Type, and Name. Enc, Dec, and Dec__nll become typed host-computation requests. And reconstruct calls Enc followed by Dec__nll. program.run("reconstruct", term) provides the compiled PyTorch modules for those requests and validates their host results against the checked types. Its result is the same tensor as losses.evaluate({"term": term}), including the autograd edges to encoder and decoder parameters. The encoder and decoder are registered submodules, so program.parameters() and program.state_dict() expose their learned tensors. The support matrix records which targets can execute these structural computations.
Try it¶
Compile, encode, decode¶
Compile the module with quivers.dsl.load, build a term with make_term and bound_var, and run it through the round trip.
import torch
from quivers.dsl import load
from quivers.structural import bound_var, make_term
torch.manual_seed(0)
prog = load("docs/examples/source/term_autoencoder.qvr")
enc = prog.encoders["Enc"]
dec = prog.decoders["Dec"]
# \x : base. plus x
term = make_term(
"Lam",
make_term("Base", "base"),
make_term("App", make_term("Const", "plus"), bound_var(0)),
)
code = enc(term)
print("code shape:", tuple(code.shape)) # (24,)
recon = dec(code)
print("reconstruction root op:", recon.op)
Score and train the round trip¶
Decoder.log_prob scores an observed term against a code vector. The registry's evaluate sums every registered loss under a caller-provided environment, and prog.run("reconstruct", term) computes the same loss through the checked reconstruct -> Enc -> Dec__nll graph. The block first asserts that the two paths agree on every term. It then trains the encoder and decoder on six distinct terms, two of which bind variables (one with a nested binder whose body refers to the outer variable, de Bruijn index 1), and checks reconstruction two ways: the decoder must assign each training term probability above 0.99 given its own code, and 20 independent decodes of each code must reproduce the term exactly, compared as full trees with Term.to_tuple so that binders and indices count. Two held-out terms are reported separately.
import torch
from quivers.dsl import load
from quivers.structural import bound_var, make_term
torch.manual_seed(0)
prog = load("docs/examples/source/term_autoencoder.qvr")
enc = prog.encoders["Enc"]
dec = prog.decoders["Dec"]
const = lambda name: make_term("Const", name)
app = lambda fun, arg: make_term("App", fun, arg)
lam = lambda body: make_term("Lam", make_term("Base", "base"), body)
train = {
"plus": const("plus"),
"f x": app(const("f"), const("x")),
"\\v. v": lam(bound_var(0)),
"\\v. plus v": lam(app(const("plus"), bound_var(0))),
"\\v. f (plus v)": lam(app(const("f"), app(const("plus"), bound_var(0)))),
"\\v. \\w. plus v": lam(lam(app(const("plus"), bound_var(1)))),
}
held_out = {
"\\v. f v": lam(app(const("f"), bound_var(0))),
"f (plus x)": app(const("f"), app(const("plus"), const("x"))),
}
def loss_of(term):
return prog.losses.evaluate({"term": term})
# The data-leaf embedding tables allocate parameters on first lookup,
# so every term is scored once before the optimizer collects them.
for term in [*train.values(), *held_out.values()]:
checked = prog.run("reconstruct", term)
torch.testing.assert_close(checked.value, loss_of(term))
assert checked.result.runtime == "core+structural"
params = list(enc.parameters()) + list(dec.parameters())
opt = torch.optim.Adam(params, lr=1e-2)
for _ in range(300):
opt.zero_grad()
loss = sum(loss_of(term) for term in train.values())
loss.backward()
opt.step()
def recovered(term, n_draws=20):
"""How many independent decodes of the term's code reproduce it."""
code = enc(term)
return sum(dec(code).to_tuple() == term.to_tuple() for _ in range(n_draws))
with torch.no_grad():
for name, term in train.items():
p_self = float(torch.exp(-loss_of(term)))
hits = recovered(term)
print(f"train {name:18} p(term | code) = {p_self:.3f}, exact decodes {hits}/20")
assert p_self > 0.99 and hits == 20, name
for name, term in held_out.items():
p_self = float(torch.exp(-loss_of(term)))
print(f"held out {name:18} p(term | code) = {p_self:.3f}, exact decodes {recovered(term)}/20")
The six training terms are reconstructed exactly, binders and de Bruijn indices included. The held-out terms are not: six training terms teach the decoder to reproduce those trees, not to generalize to new combinations of their parts, and a reconstruction objective on so few terms gives no pressure to do so. Generalization would need a larger and more varied training set.
Categorical perspective¶
An encoder recursively replaces each constructor by its vector operation. For an ordinary signature \(\Sigma\), this is the fold from the initial term algebra \(T_\Sigma\) to the chosen vector algebra \(\mathrm{Vec}_D\), and it satisfies the corresponding algebra-homomorphism equations (Meijer et al., 1991). With binders, the recursion also carries the typed variable context described in structural semantics.
The decoder unfolds a vector by choosing a constructor and producing child vectors. Recursing with the depth bound yields a kernel \(\mathrm{Vec}_D\to\mathcal{G}(T_\Sigma)\) on the supported terms, where \(\mathcal{G}\) denotes probability measures. Composing encoding and decoding gives a stochastic map \(T_\Sigma\to\mathcal{G}(T_\Sigma)\). The reconstruction loss is the negative log probability of the observed term under that map, not a metric distance between functions.
See also¶
- Structural Compression: Signatures and Encoders
- Structural Compression: Decoders and Losses
- Structural Compression semantics
- API reference: Signatures, Encoders, Decoders, Loss Registry
Cited sources¶
Erik Meijer, Maarten Fokkinga, and Ross Paterson. Functional Programming with Bananas, Lenses, Envelopes and Barbed Wire. In Functional Programming Languages and Computer Architecture (FPCA), Lecture Notes in Computer Science, 124–144. Springer, 1991. doi:10.1007/3540543961_7.