Signatures¶
A signature is a many-sorted
second-order binding signature: constructors over sorts, each argument with its own binding arity,
and, for graph signatures, vertex and edge kinds (Fiore et al., 1999). An
expression tree might
have a constant constructor and an application constructor with two children. These objects carry
the syntax declarations of the DSL at run time.
Variables are de Bruijn indices: a
BoundVar(i) term carries an integer index into one index space shared by every sort, and a
constructor argument that binds variables pushes a
ContextEntry(var_sort=sort, embedding=tensor, type_term=term) per variable onto an implicit
context Γ tracked by the encoder and decoder runtime. A bound variable may carry an annotation, an
earlier labeled argument of the same constructor, stored alongside its embedding in Γ.
Terms are immutable values with structural equality. Two terms are
equal exactly when their operators agree and their arguments agree
position by position, recursively through nested subterms, data
leaves, annotations, and de Bruijn indices. Hashing agrees with
equality, so terms work as dictionary keys and set members. Data
leaves compare by type as well as value: make_term("C", 1),
make_term("C", 1.0), and make_term("C", True) are three distinct
terms. A reconstruction check such as decoded == original thus
holds only when the decoder recovered every part of the term.
Arguments outside the TermArg union are rejected when the term is
constructed.
signature
¶
Signatures: typed multi-sorted second-order algebras.
A Signature is the runtime form of a syntax declaration. It
carries the sort table, the constructor table, and, for graph-shaped
signatures, the vertex and edge kind tables. Each argument of a
constructor has a binding arity: the variables, of declared sorts, that
are in scope in that argument and nowhere else. Let : Term, (x :
Term).Term -> Term binds one variable in its second argument only.
Terms over a signature are first-class Python values (Term
instances): each carries the constructor name and its positional
arguments. Variables are de Bruijn indices: BoundVar(i) refers to
the i-th enclosing bound variable, counting from the innermost.
Bound variable names are display names, so alpha-equivalent terms are
equal terms. The generated operations on terms (renaming, substitution,
instantiation, free variables, equality, hashing, ordering, printing,
and folds) are in quivers.structural.binding.
All structured value types in this module are didactic
didactic.api.Model records, matching the quivers
convention for schema-bearing data. Recursive term arguments and scope
entries use opaque value fields to preserve nested storage. Runtime
embeddings use opaque identity fields to preserve tensors and autograd.
quivers._values.ValueModel includes these fields in equality, hashing,
copying, and pickling.
SortVocabEntry
didactic-model
¶
SortVocabEntry(*, kind: Literal['string', 'integer', 'float'], value: DataLeaf)
Bases: Model
One closed-vocabulary entry on a data sort.
Each entry is a tagged Python value: kind is the leaf
type ("string" | "integer" | "float"), value is the
decoded Python value (str, int, or float).
| PARAMETER | DESCRIPTION |
|---|---|
kind
|
The leaf type.
TYPE:
|
value
|
The decoded value.
TYPE:
|
Sort
didactic-model
¶
Sort(*, name: str, kind: SortKind, dim: int | None = None, vocab: tuple[SortVocabEntry, ...] = ())
Bases: Model
A sort declaration: name, kind, optional dim, optional closed vocabulary.
A data sort may carry a vocab tuple of
SortVocabEntry records. Object / index sorts must
have an empty vocab (the framework checks at runtime).
| PARAMETER | DESCRIPTION |
|---|---|
name
|
The sort's name.
TYPE:
|
kind
|
TYPE:
|
dim
|
The embedding dimension, if declared.
TYPE:
|
vocab
|
The closed vocabulary of a data sort.
TYPE:
|
BoundVariable
didactic-model
¶
Bases: Model
A variable an argument of a constructor binds.
The variable is in scope in exactly the argument that binds it. Its name is used for display only: terms refer to it by de Bruijn index, so two terms that differ only in the names of their bound variables are the same term.
annotation is the position of an unbound argument of the same
constructor, before the binding argument, whose value annotates the
variable (a type annotation, for instance). The encoder and decoder
initialize the variable from it; substitution and equality ignore it.
| PARAMETER | DESCRIPTION |
|---|---|
name
|
The display name.
TYPE:
|
sort
|
The variable's sort, an object sort of the signature.
TYPE:
|
annotation
|
The position of the annotating argument, if any.
TYPE:
|
Constructor
didactic-model
¶
Constructor(*, name: str, domain: tuple[str, ...] = (), codomain: str = '', scopes: tuple[tuple[BoundVariable, ...], ...] = (), labels: tuple[str, ...] = ())
Bases: Model
A typed constructor symbol with a binding arity per argument.
A first-order constructor declares only domain. A constructor
with binding also declares scopes, one tuple of bound variables
per argument position, so Let : Term, (x : Term).Term -> Term
has the scopes ((), (x,)): its second argument is in the scope
of one new variable of sort Term and its first is not. The last
variable of a scope is the innermost, de Bruijn index 0, inside the
argument. This is the second-order binding signature of Fiore,
Plotkin, and Turi.
| PARAMETER | DESCRIPTION |
|---|---|
name
|
The constructor's name.
TYPE:
|
domain
|
The sort of each argument, in order.
TYPE:
|
codomain
|
The sort it produces.
TYPE:
|
scopes
|
Empty, or the variables each argument binds, one entry per argument.
TYPE:
|
labels
|
Empty, or a display label per argument,
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValidationError
|
If |
arity
property
¶
The number of arguments the constructor takes.
| RETURNS | DESCRIPTION |
|---|---|
int
|
The arity. |
binding_arity
property
¶
How many variables each argument binds.
| RETURNS | DESCRIPTION |
|---|---|
tuple of int
|
One count per argument, zero for an unbound argument. |
binds_variables
property
¶
Whether some argument binds a variable.
| RETURNS | DESCRIPTION |
|---|---|
bool
|
|
bound_at
¶
bound_at(position: int) -> tuple[BoundVariable, ...]
The variables an argument binds.
| PARAMETER | DESCRIPTION |
|---|---|
position
|
The argument position.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple of BoundVariable
|
Its scope, outermost first; empty for an unbound argument. |
Source code in src/quivers/structural/signature.py
arity_text
¶
The constructor's type as source writes it.
| RETURNS | DESCRIPTION |
|---|---|
str
|
|
Source code in src/quivers/structural/signature.py
label
¶
The display label of an argument.
| PARAMETER | DESCRIPTION |
|---|---|
position
|
The argument position.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
Its label, or |
Source code in src/quivers/structural/signature.py
VertexKind
didactic-model
¶
VertexKind(*, name: str, kind: SortKind, dim: int | None = None)
Bases: Model
A vertex kind in a graph-shaped signature.
| PARAMETER | DESCRIPTION |
|---|---|
name
|
The kind's name.
TYPE:
|
kind
|
The kind of its values.
TYPE:
|
dim
|
The embedding dimension, if declared.
TYPE:
|
EdgeKind
didactic-model
¶
Bases: Model
An edge kind in a graph-shaped signature.
| PARAMETER | DESCRIPTION |
|---|---|
name
|
The kind's name.
TYPE:
|
src
|
The vertex kind it leaves.
TYPE:
|
tgt
|
The vertex kind it enters.
TYPE:
|
directed
|
Whether it is directed.
TYPE:
|
Signature
didactic-model
¶
Signature(*, name: str, params: tuple[str, ...] = (), sorts_t: tuple[Sort, ...] = (), constructors_t: tuple[Constructor, ...] = (), vertex_kinds_t: tuple[VertexKind, ...] = (), edge_kinds_t: tuple[EdgeKind, ...] = ())
Bases: Model
A multi-sorted second-order signature, or a graph signature.
Sort, constructor, vertex_kind and edge_kind tables are represented
as tuples of records (rather than dicts) so the enclosing dx.Model
keeps a schema-bearing layout. The runtime exposes name-keyed tables
(sorts, constructors, ...) on top of those tuples, each built
once per signature, and every lookup through them takes constant
time. A constructor's arguments may bind variables (see
Constructor), so one table holds both first-order and binding
constructors.
| PARAMETER | DESCRIPTION |
|---|---|
name
|
The signature's name.
TYPE:
|
params
|
Its parameters.
TYPE:
|
sorts_t
|
Its sorts.
TYPE:
|
constructors_t
|
Its constructors.
TYPE:
|
vertex_kinds_t
|
Its vertex kinds, for a graph signature.
TYPE:
|
edge_kinds_t
|
Its edge kinds, for a graph signature.
TYPE:
|
sorts
cached
property
¶
sorts: Mapping[str, Sort]
The sort declarations keyed by sort name.
| RETURNS | DESCRIPTION |
|---|---|
Mapping[str, Sort]
|
A read-only table, built once. |
constructors
cached
property
¶
constructors: Mapping[str, Constructor]
The constructor declarations keyed by constructor name.
| RETURNS | DESCRIPTION |
|---|---|
Mapping[str, Constructor]
|
A read-only table, built once. |
vertex_kinds
cached
property
¶
vertex_kinds: Mapping[str, VertexKind]
The graph vertex kinds keyed by kind name.
| RETURNS | DESCRIPTION |
|---|---|
Mapping[str, VertexKind]
|
A read-only table, built once. |
edge_kinds
cached
property
¶
edge_kinds: Mapping[str, EdgeKind]
The graph edge kinds keyed by kind name.
| RETURNS | DESCRIPTION |
|---|---|
Mapping[str, EdgeKind]
|
A read-only table, built once. |
variable_sorts
cached
property
¶
The sorts some argument binds a variable of, in sort order.
| RETURNS | DESCRIPTION |
|---|---|
tuple of str
|
The sorts that have variables; a term of any other sort is never a variable. |
is_inductive
¶
Whether the signature declares any constructor.
| RETURNS | DESCRIPTION |
|---|---|
bool
|
|
is_graph
¶
Whether the signature declares both vertex and edge kinds.
| RETURNS | DESCRIPTION |
|---|---|
bool
|
|
Source code in src/quivers/structural/signature.py
is_first_order
¶
Whether no constructor binds a variable.
| RETURNS | DESCRIPTION |
|---|---|
bool
|
|
Source code in src/quivers/structural/signature.py
all_ops
¶
Iterate over every operation name of the signature.
| YIELDS | DESCRIPTION |
|---|---|
str
|
Each constructor name in declaration order. |
constructor
¶
constructor(op: str) -> Constructor
Return a constructor by name.
| PARAMETER | DESCRIPTION |
|---|---|
op
|
The constructor name.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Constructor
|
Its declaration. |
| RAISES | DESCRIPTION |
|---|---|
KeyError
|
If no constructor of that name is declared. |
Source code in src/quivers/structural/signature.py
codomain_of
¶
Return the codomain sort of a constructor.
| PARAMETER | DESCRIPTION |
|---|---|
op
|
The constructor name.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
The name of the sort the operation produces. |
| RAISES | DESCRIPTION |
|---|---|
KeyError
|
If |
Source code in src/quivers/structural/signature.py
domain_of
¶
Return the positional argument sorts of a constructor.
| PARAMETER | DESCRIPTION |
|---|---|
op
|
The constructor name.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[str, ...]
|
The sort of each positional argument. |
| RAISES | DESCRIPTION |
|---|---|
KeyError
|
If |
Source code in src/quivers/structural/signature.py
binds_variables
¶
Whether some argument of a constructor binds a variable.
| PARAMETER | DESCRIPTION |
|---|---|
op
|
The operation name.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
|
Source code in src/quivers/structural/signature.py
sort_dim
¶
Return the declared embedding dimension of a sort or vertex kind.
| PARAMETER | DESCRIPTION |
|---|---|
sort
|
The sort or vertex-kind name.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
int or None
|
The declared dimension, or |
Source code in src/quivers/structural/signature.py
Term
didactic-model
¶
Bases: ValueModel
A closed term over a signature.
op is the constructor name, the binder name, or the
framework-reserved "BoundVar" for a de-Bruijn variable
reference.
args are positional children. Object positions carry a
Term; data positions carry a DataLeaf raw
value; index positions carry a non-negative int. args
is held opaque (its element type is the open union
TermArg, which didactic cannot encode because one of its arms
is Term itself); construction checks that args is a tuple
whose elements are Term or DataLeaf values, and the encoder /
decoder enforce sort agreement at use time.
args compares by value, so equality and hashing are
structural (see quivers._values.ValueModel): two terms are equal
exactly when their operators agree and their arguments agree
position by position, recursively, so terms that differ in a data
leaf, a nested subterm, a binder, or a de-Bruijn index are
distinct. Data leaves compare by type as well as value, so 1,
1.0, and True at the same position are three different
terms. Both operations walk the term with an explicit stack, so
deep terms (long Cons lists) do not exhaust the interpreter's
recursion limit; a term's hash is computed once and cached, and
copies and pickles keep args.
No wrapping such as Term("Data", …) is used at any
position; raw values appear directly at data positions.
| PARAMETER | DESCRIPTION |
|---|---|
op
|
The constructor, binder, or
TYPE:
|
args
|
The positional children.
TYPE:
|
to_tuple
¶
A serialisable form matching the agenda's item-tuple
convention: (op, *args_serialised). Children that are
Terms are recursively serialised; data leaves and
indices pass through.
| RETURNS | DESCRIPTION |
|---|---|
tuple
|
The nested tuple |
Source code in src/quivers/structural/signature.py
ContextEntry
didactic-model
¶
ContextEntry(*, var_sort: str, embedding: Tensor = field(opaque=True, extras=OPAQUE_BY_IDENTITY), type_term: Term | None = field(default=None, opaque=True, extras=OPAQUE_BY_VALUE))
Bases: ValueModel
One scope entry on the binder stack.
embedding is a runtime tensor; type_term is the binder's
annotation term captured at scope-extension time. Tensor identity
participates in equality and hashing, preserving live autograd state.
| PARAMETER | DESCRIPTION |
|---|---|
var_sort
|
The sort of the bound variable.
TYPE:
|
embedding
|
Its embedding.
TYPE:
|
type_term
|
The binder's annotation of it, if any.
TYPE:
|
Context
didactic-model
¶
Context(*, entries: tuple[ContextEntry, ...] = field(default=(), opaque=True, extras=OPAQUE_BY_VALUE))
Bases: ValueModel
The de-Bruijn scope context threaded through encoder and decoder runtimes.
Position 0 is the most recently bound variable; lookup var(i)
returns the i-th entry from the top.
| PARAMETER | DESCRIPTION |
|---|---|
entries
|
The entries, innermost first.
TYPE:
|
push
¶
Return the context extended by one innermost bound variable.
| PARAMETER | DESCRIPTION |
|---|---|
var_sort
|
The sort of the new variable.
TYPE:
|
embedding
|
The variable's embedding.
TYPE:
|
type_term
|
The binder's annotation term for the variable, if any.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Context
|
A new context whose index 0 is the new variable; |
Source code in src/quivers/structural/signature.py
var
¶
var(index: int) -> Tensor
Return the embedding of a bound variable.
| PARAMETER | DESCRIPTION |
|---|---|
index
|
The de-Bruijn index, counted from the innermost variable.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
The variable's embedding. |
| RAISES | DESCRIPTION |
|---|---|
IndexError
|
If |
Source code in src/quivers/structural/signature.py
type_of
¶
type_of(index: int) -> Term | None
Return the annotation term of a bound variable.
| PARAMETER | DESCRIPTION |
|---|---|
index
|
The de-Bruijn index, counted from the innermost variable.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Term or None
|
The annotation captured when the variable was bound, or
|
Source code in src/quivers/structural/signature.py
depth
¶
Return the number of variables in scope.
| RETURNS | DESCRIPTION |
|---|---|
int
|
The context depth. |
by_sort
¶
by_sort(sort: str) -> list[tuple[int, ContextEntry]]
All entries whose var_sort matches; returned as
(depth-index, entry) pairs for the decoder's
categorical-over-in-scope-variables.
| PARAMETER | DESCRIPTION |
|---|---|
sort
|
The variable sort to select.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[tuple[int, ContextEntry]]
|
The matching entries with their de-Bruijn indices, innermost first. |
Source code in src/quivers/structural/signature.py
SubstitutionError
¶
Bases: ValueError
A substitution cannot be applied to a term.
Substitution
didactic-model
¶
Substitution(*, replacements: tuple[Term, ...] = field(default=(), opaque=True, extras=OPAQUE_BY_VALUE), shift: int = 0)
Bases: ValueModel
A simultaneous substitution of the free variables of de-Bruijn terms.
The variable with index i becomes replacements[i] when i is
below their number n, and the variable i - n + shift
otherwise. Every substitution of terms in a context of n + k
variables by terms in a context of shift + k variables has this
form. A renaming is a substitution whose replacements are variables.
| PARAMETER | DESCRIPTION |
|---|---|
replacements
|
The replacements of the innermost variables.
TYPE:
|
shift
|
Where the remaining variables go; at least zero.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValidationError
|
If the shift is negative. |
weakening
classmethod
¶
weakening(amount: int) -> Substitution
The renaming that makes room for new innermost variables.
| PARAMETER | DESCRIPTION |
|---|---|
amount
|
How many variables are added.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Substitution
|
The variable |
Source code in src/quivers/structural/signature.py
renaming
classmethod
¶
renaming(targets: tuple[int, ...], shift: int) -> Substitution
The renaming that sends variable i to targets[i].
| PARAMETER | DESCRIPTION |
|---|---|
targets
|
The new index of each of the innermost variables.
TYPE:
|
shift
|
Where the remaining variables go.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Substitution
|
The renaming. |
Source code in src/quivers/structural/signature.py
image
¶
image(index: int) -> Term
The replacement of one variable.
| PARAMETER | DESCRIPTION |
|---|---|
index
|
The variable.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Term
|
Its replacement. |
Source code in src/quivers/structural/signature.py
lift
¶
lift(count: int, signature: Signature) -> Substitution
The substitution under count new innermost variables.
| PARAMETER | DESCRIPTION |
|---|---|
count
|
The variables a binder introduces.
TYPE:
|
signature
|
The signature of the terms.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Substitution
|
The new variables go to themselves, and the replacements are weakened past them. |
Source code in src/quivers/structural/signature.py
then
¶
then(other: Substitution, signature: Signature) -> Substitution
The substitution that applies this one and then another.
| PARAMETER | DESCRIPTION |
|---|---|
other
|
The substitution applied second.
TYPE:
|
signature
|
The signature of the terms.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Substitution
|
|
Source code in src/quivers/structural/signature.py
bound_var
¶
bound_var(index: int) -> Term
A de-Bruijn variable reference.
| PARAMETER | DESCRIPTION |
|---|---|
index
|
The de-Bruijn index: 0 refers to the innermost bound variable.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Term
|
The term |
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
If |
Source code in src/quivers/structural/signature.py
make_term
¶
make_term(op: str, *args) -> Term
Construct a term over a signature.
Each arg must be a Term, a DataLeaf raw
value, or a non-negative int (for index-sorted positions). The
encoder / decoder runtime validate sort agreement at use
time.
| PARAMETER | DESCRIPTION |
|---|---|
op
|
The constructor or binder name.
TYPE:
|
*args
|
The positional children.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Term
|
The term |
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
If |
ValidationError
|
If an argument is neither a |
Source code in src/quivers/structural/signature.py
substitute
¶
substitute(term: Term, substitution: Substitution, signature: Signature) -> Term
Apply a substitution to the free variables of a term.
It goes under binders by lifting the substitution, so a replacement is never captured. An argument at an index-sorted position names a variable and may only be renamed. The cost is the size of the term times the cost of weakening each replacement used under binders; the traversal uses an explicit stack.
| PARAMETER | DESCRIPTION |
|---|---|
term
|
The term.
TYPE:
|
substitution
|
The substitution.
TYPE:
|
signature
|
The signature of the term.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Term
|
The substituted term. |
| RAISES | DESCRIPTION |
|---|---|
SubstitutionError
|
If an operation is undeclared, an index position would receive a term that is not a variable, or an argument's kind does not match its position. |
Source code in src/quivers/structural/signature.py
1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 | |
free_indices
¶
The free variables of a term.
| PARAMETER | DESCRIPTION |
|---|---|
term
|
The term.
TYPE:
|
signature
|
Its signature.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
frozenset of int
|
The indices of its free variables in its context. |
| RAISES | DESCRIPTION |
|---|---|
SubstitutionError
|
If an operation is undeclared. |
Source code in src/quivers/structural/signature.py
Type aliases and constants¶
Structural compression: signatures, encoders, decoders, losses.
Runtime substrate for QVR signatures, encoders, decoders, and attached losses: second-order binding signatures with their generated term operations, and a uniform algebraic interface for compressing structured objects to fixed-length vectors and decoding them under a learned distribution.
Cited sources¶
Marcelo Fiore, Gordon Plotkin, and Daniele Turi. Abstract Syntax and Variable Binding. In Proceedings of the 14th Symposium on Logic in Computer Science (LICS), 193–202. IEEE Computer Society, 1999. doi:10.1109/LICS.1999.782615.