Binding operations¶
Terms of a binding signature are stored with de Bruijn indices
in one index space shared by every sort, so alpha-equivalence
is structural equality. This module defines the operations every signature gets: weakening and
renaming, substitution and instantiation, free variables, alpha-equality, a hash and an order that
agree with it, conversion to and from named terms, printing with fresh names, and a generic fold
whose algebra receives each bound argument as a Scoped value
(Tree and graph autoencoders §1.2.1).
binding
¶
Generated operations on the terms of a second-order binding signature.
Following Fiore, Plotkin, and Turi, the terms of a signature with binding
are the initial algebra of a signature endofunctor on presheaves over
contexts, and substitution is its monoid structure
[@fiore1999abstract]. The terms of a Signature are well-scoped de Bruijn
terms in the style of Autosubst 2 [@stark2019autosubst]: a variable is
BoundVar(i), the i-th enclosing bound variable counted from the
innermost, and an argument that binds k variables is a term in a
context extended by k. Bound names are kept by the signature for
printing only, so alpha-equivalent terms are structurally equal and
alpha-equality, hashing, and ordering are structural.
Every signature has the operations of this module, generated from its binding arities rather than written per signature:
renameandweaken, the renamings;substitute(fromquivers.structural.signature), the simultaneous substitution, andinstantiate, the substitution of the variables of one binder;free_variables;alpha_equal,content_hash,compare, andsort_key;to_named,from_named, andshow, which print with fresh names;fold, the unique map into an algebra that respects substitution, the Sigma-monoid fold of Fiore and Szamozvancev [@fiore2022formal].
A context is written as a tuple of sorts, outermost first, so the variable
BoundVar(i) of a context ctx has sort ctx[-1 - i].
NamedArgument
¶
NamedArgument = NamedTerm | Name | Abstraction | DataLeaf
An argument of a named term.
FoldArgument
¶
What an algebra operation receives at one argument position.
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
SubstitutionError
¶
Bases: ValueError
A substitution cannot be applied to a term.
BindingError
¶
Bases: ValueError
A term does not fit the scope or the sorts its signature declares.
Name
didactic-model
¶
Bases: Model
A variable written by name in a named term.
| PARAMETER | DESCRIPTION |
|---|---|
name
|
The variable's name.
TYPE:
|
Abstraction
didactic-model
¶
NamedTerm
didactic-model
¶
Bases: ValueModel
A term written with named variables.
An argument that binds variables is an Abstraction; a variable is a
Name. Named terms are what a reader writes and what show prints;
from_named gives the de Bruijn term.
| PARAMETER | DESCRIPTION |
|---|---|
op
|
The constructor.
TYPE:
|
args
|
Its arguments.
TYPE:
|
Scoped
didactic-model
¶
Scoped(*, arity: int, apply: Callable[[tuple[V, ...]], V] = field(opaque=True, extras=OPAQUE_BY_IDENTITY))
Bases: ValueModel
The value of a binding argument: a function of its bound variables.
Applying it to one value per bound variable, outermost first, folds the argument in the environment extended by them. It is the abstraction of a Sigma-monoid's operations over the context presheaf.
| PARAMETER | DESCRIPTION |
|---|---|
arity
|
The number of variables bound.
TYPE:
|
apply
|
The fold of the argument as a function of the bound values.
TYPE:
|
__call__
¶
Fold the argument with its bound variables given values.
| PARAMETER | DESCRIPTION |
|---|---|
*values
|
One value per bound variable, outermost first.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
V
|
The fold of the argument. |
| RAISES | DESCRIPTION |
|---|---|
BindingError
|
If the number of values differs from the binding arity. |
Source code in src/quivers/structural/binding.py
SubstitutionAlgebra
¶
Bases: Protocol
An algebra of a binding signature: one operation per constructor.
operation receives, for each argument, the fold of an unbound object
argument, a Scoped function for a binding argument, the raw value of a
data argument, or the environment's value of the variable at an index
argument. Variables are interpreted by the environment, so the fold
respects substitution: fold(substitute(t, s), env) equals
fold(t, env') where env' holds the fold of each replacement.
operation
¶
operation(op: str, arguments: tuple[FoldArgument[V], ...]) -> V
Interpret one constructor.
| PARAMETER | DESCRIPTION |
|---|---|
op
|
The constructor.
TYPE:
|
arguments
|
Its folded arguments.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
V
|
The value. |
Source code in src/quivers/structural/binding.py
Built
didactic-model
¶
Built(*, at: Callable[[int], Term] = field(opaque=True, extras=OPAQUE_BY_IDENTITY))
Bases: ValueModel
A term as a function of the depth of the context it is placed in.
The carrier of TermAlgebra: a variable is a de Bruijn level, so its
index depends on how deep the context is where the term is used.
| PARAMETER | DESCRIPTION |
|---|---|
at
|
The term at each depth.
TYPE:
|
TermAlgebra
¶
TermAlgebra(signature: Signature)
The initial algebra: folding a term rebuilds it, as a check of fold.
| PARAMETER | DESCRIPTION |
|---|---|
signature
|
The signature.
TYPE:
|
Source code in src/quivers/structural/binding.py
operation
¶
operation(op: str, arguments: tuple[FoldArgument[Built], ...]) -> Built
Rebuild one constructor at any depth.
| PARAMETER | DESCRIPTION |
|---|---|
op
|
The constructor.
TYPE:
|
arguments
|
Its folded arguments.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Built
|
The node as a function of the depth of its context. |
Source code in src/quivers/structural/binding.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 | |
binder_arity
¶
binder_arity(signature: Signature, op: str) -> tuple[int, ...]
How many variables each argument of a constructor binds.
A binder pattern Lam(x. body) is well formed exactly when it gives
each argument as many names as this arity says.
| PARAMETER | DESCRIPTION |
|---|---|
signature
|
The signature.
TYPE:
|
op
|
A constructor of it.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple of int
|
One count per argument. |
| RAISES | DESCRIPTION |
|---|---|
KeyError
|
If |
Source code in src/quivers/structural/binding.py
argument_context
¶
argument_context(signature: Signature, op: str, position: int, context: tuple[str, ...]) -> tuple[str, ...]
The context of one argument of a constructor.
| PARAMETER | DESCRIPTION |
|---|---|
signature
|
The signature.
TYPE:
|
op
|
The constructor.
TYPE:
|
position
|
The argument.
TYPE:
|
context
|
The sorts of the variables in scope at the constructor, outermost first.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple of str
|
The context extended by the argument's bound variables. |
Source code in src/quivers/structural/binding.py
check_term
¶
check_term(term: Term, signature: Signature, context: tuple[str, ...] = (), sort: str | None = None) -> str
Check that a term is well scoped and well sorted, and give its sort.
| PARAMETER | DESCRIPTION |
|---|---|
term
|
The term.
TYPE:
|
signature
|
Its signature.
TYPE:
|
context
|
The sorts of its free variables, outermost first.
TYPE:
|
sort
|
The sort it must have, or
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
The term's sort. |
| RAISES | DESCRIPTION |
|---|---|
BindingError
|
If a constructor is undeclared or applied to the wrong number of arguments, an argument has the wrong kind or sort, or a variable is out of scope or of another sort. |
Source code in src/quivers/structural/binding.py
weaken
¶
Move a term under amount new innermost variables.
| PARAMETER | DESCRIPTION |
|---|---|
term
|
The term.
TYPE:
|
amount
|
How many variables are added; at least zero.
TYPE:
|
signature
|
Its signature.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Term
|
The term with each free index raised by |
Source code in src/quivers/structural/binding.py
rename
¶
Apply a renaming: free variable i becomes targets[i].
| PARAMETER | DESCRIPTION |
|---|---|
term
|
The term.
TYPE:
|
targets
|
The new index of each of the innermost
TYPE:
|
signature
|
Its signature.
TYPE:
|
shift
|
Where the variables beyond
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Term
|
The renamed term. |
Source code in src/quivers/structural/binding.py
instantiate
¶
Instantiate the variables of one binder in its argument.
body is an argument that binds len(values) variables, in
declaration order, and values are terms in the binder's outer
context. instantiate(M, (N,)) is the M[N] of beta reduction.
| PARAMETER | DESCRIPTION |
|---|---|
body
|
The argument, in the extended context.
TYPE:
|
values
|
One term per bound variable, outermost first.
TYPE:
|
signature
|
The signature.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Term
|
The body with the bound variables replaced, in the outer context. |
Source code in src/quivers/structural/binding.py
free_variables
¶
strengthen
¶
Remove unused variables from a term's context.
| PARAMETER | DESCRIPTION |
|---|---|
term
|
The term.
TYPE:
|
drop
|
Variables of its context to remove; the others are renumbered.
TYPE:
|
signature
|
Its signature.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Term or None
|
The term in the smaller context, or |
Source code in src/quivers/structural/binding.py
level_name
¶
The canonical name of the variable bound at a de Bruijn level.
Naming every bound variable by its level (the number of binders
outside it) makes alpha-equivalent named terms identical, which is how
a deduction's binders identify alpha-equivalent logical forms.
| PARAMETER | DESCRIPTION |
|---|---|
level
|
The level, counted from the outermost binder.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
|
Source code in src/quivers/structural/binding.py
alpha_equal
¶
Whether two terms are alpha-equivalent.
Terms are de Bruijn, so alpha-equivalence is structural equality.
| PARAMETER | DESCRIPTION |
|---|---|
left
|
The terms.
TYPE:
|
right
|
The terms.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
|
Source code in src/quivers/structural/binding.py
tokens
¶
tokens(term: Term) -> Iterator[_Token]
The prefix encoding of a term, one token per node and leaf.
The encoding records each operator with its arity, so it is injective; comparing encodings lexicographically is a total order consistent with equality.
| PARAMETER | DESCRIPTION |
|---|---|
term
|
The term.
TYPE:
|
| YIELDS | DESCRIPTION |
|---|---|
tuple
|
The tokens, in prefix order. |
Source code in src/quivers/structural/binding.py
sort_key
¶
sort_key(term: Term) -> tuple[_Token, ...]
A key that orders terms totally, consistently with equality.
Variables precede constructor applications; applications compare by operator name, then arity, then arguments from left to right.
| PARAMETER | DESCRIPTION |
|---|---|
term
|
The term.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple
|
The key. |
Source code in src/quivers/structural/binding.py
compare
¶
Compare two terms in the total order of sort_key.
| PARAMETER | DESCRIPTION |
|---|---|
left
|
The terms.
TYPE:
|
right
|
The terms.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
int
|
|
Source code in src/quivers/structural/binding.py
content_hash
¶
content_hash(term: Term) -> str
A stable, alpha-invariant content hash of a term.
The hash is the SHA-256 digest of the prefix encoding, so it is the
same in every process and for alpha-equivalent terms, unlike Python's
salted hash.
| PARAMETER | DESCRIPTION |
|---|---|
term
|
The term.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
The hexadecimal digest. |
Source code in src/quivers/structural/binding.py
from_named
¶
The de Bruijn term of a named term.
A name refers to the innermost enclosing binder of that name, then to
context.
| PARAMETER | DESCRIPTION |
|---|---|
term
|
The named term. |
signature
|
Its signature.
TYPE:
|
context
|
The names of the free variables, outermost first.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Term
|
The term. |
| RAISES | DESCRIPTION |
|---|---|
BindingError
|
If a name is unbound, an abstraction binds the wrong number of names, or a binding argument is given without an abstraction. |
Source code in src/quivers/structural/binding.py
to_named
¶
to_named(term: Term, signature: Signature, context: Sequence[str] = (), naming: Literal['fresh', 'level'] = 'fresh') -> NamedTerm | Name
A named term for a de Bruijn term, with fresh bound names.
With naming="fresh" each binder uses its declared variable names,
renamed apart from every name in scope. With naming="level" a bound
variable is named by its declared name followed by its de Bruijn level
inside the term, so names are distinct by construction; this is how
the generated QIEC show names them. Either way no variable is
captured: the result read back by from_named is the original term.
| PARAMETER | DESCRIPTION |
|---|---|
term
|
The term.
TYPE:
|
signature
|
Its signature.
TYPE:
|
context
|
The names of the free variables, outermost first; a free variable
beyond them is named
TYPE:
|
naming
|
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
NamedTerm or Name
|
The named term. |
Source code in src/quivers/structural/binding.py
773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 | |
render
¶
The text of a named term.
| PARAMETER | DESCRIPTION |
|---|---|
term
|
The named term. |
quote
|
Empty to write data leaves as Python literals, or a quotation mark written around string leaves instead, with other leaves bare.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
|
Source code in src/quivers/structural/binding.py
show
¶
show(term: Term, signature: Signature, context: Sequence[str] = (), naming: Literal['fresh', 'level'] = 'fresh') -> str
Print a term with fresh names for its bound variables.
| PARAMETER | DESCRIPTION |
|---|---|
term
|
The term.
TYPE:
|
signature
|
Its signature.
TYPE:
|
context
|
Names of the free variables, outermost first.
TYPE:
|
naming
|
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
The text, as |
Source code in src/quivers/structural/binding.py
fold
¶
fold(term: Term, signature: Signature, algebra: SubstitutionAlgebra[V], environment: Sequence[V] = ()) -> V
The unique substitution-respecting map from terms into an algebra.
Unbound object arguments are folded before their parent with an
explicit stack, so long first-order spines do not exhaust the
interpreter's recursion limit; a binding argument is folded when the
algebra applies its Scoped value.
| PARAMETER | DESCRIPTION |
|---|---|
term
|
The term.
TYPE:
|
signature
|
Its signature.
TYPE:
|
algebra
|
The algebra.
TYPE:
|
environment
|
The value of each free variable, outermost first.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
V
|
The fold. |
| RAISES | DESCRIPTION |
|---|---|
BindingError
|
If a variable is outside the environment. |
Source code in src/quivers/structural/binding.py
1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 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 | |
rebuild
¶
Fold a term into the initial algebra, which gives it back.
| PARAMETER | DESCRIPTION |
|---|---|
term
|
A term in a context of
TYPE:
|
signature
|
Its signature.
TYPE:
|
depth
|
The size of its context.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Term
|
The same term. |
Source code in src/quivers/structural/binding.py
environment_of
¶
Replacements of a finite context as an environment.
| PARAMETER | DESCRIPTION |
|---|---|
substitution
|
The replacement of each variable, by index.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple of Term
|
The replacements, outermost first. |