substitution design

Design goal

Substitution is the operation every other part of the library is built on: beta reduction substitutes an argument for a parameter, rewriting rules instantiate their variables, and partial evaluation in downstream packages replaces known variables by values. substitution provides one capture-avoiding, simultaneous substitution for Term[T] and the same algorithm for any BindingSyntax AST, with laws that hold up to alpha-equivalence.

Constraints

  • Arbitrary input. Terms come from users and from other algorithms, so substitution must be correct without assuming that bound and free names are distinct.
  • Determinism and readability. Fresh names are reproducible and stay close to the original names; binders are renamed only when necessary.
  • One algorithm. Term[T] and every BindingSyntax AST use the same definition, so one proof covers both.

Mathematical background

Terms, FV\mathrm{FV}, names\mathrm{names} and =α=_\alpha are those of the syntax design.

Substitutions

A substitution is a map σ\sigma from names to terms that is the identity σ(x)=x\sigma(x) = x outside a finite domain dom⁡σ\operatorname{dom}\sigma. Its support is supp⁡σ=dom⁡σ∪⋃x∈dom⁡σnames(σ(x))\operatorname{supp}\sigma = \operatorname{dom}\sigma \cup \bigcup_{x \in \operatorname{dom}\sigma} \mathrm{names}(\sigma(x)), and σ∖x\sigma \setminus x removes xx from the domain. For a set of names SS, the relevant range of σ\sigma on SS is

Rσ(S)=⋃z∈S∩dom⁡σFV(σ(z)).R_\sigma(S) = \bigcup_{z \in S \cap \operatorname{dom}\sigma} \mathrm{FV}(\sigma(z)).

Capture-avoiding simultaneous substitution

Substitution::apply computes tσt\sigma by

vσ=v,xσ=σ(x),t(u1,…,un)σ=(tσ)(u1σ,…,unσ),(βx. t)σ={βx.  tσ′if x∉Rσ′(FV(t)),βx′.  (t{x↦x′})σ′otherwise,σ′=σ∖x,\begin{aligned} v\sigma &= v, \qquad x\sigma = \sigma(x), \qquad t(u_1, \dots, u_n)\sigma = (t\sigma)(u_1\sigma, \dots, u_n\sigma),\\ (\beta x.\, t)\sigma &= \begin{cases} \beta x.\; t\sigma' & \text{if } x \notin R_{\sigma'}(\mathrm{FV}(t)),\\[2pt] \beta x'.\; \big(t\{x \mapsto x'\}\big)\sigma' & \text{otherwise,} \end{cases} \qquad \sigma' = \sigma \setminus x, \end{aligned}

where x′=fresh⁡(x, supp⁡σ′∪names(t)∪{x})x' = \operatorname{fresh}(x,\ \operatorname{supp}\sigma' \cup \mathrm{names}(t) \cup \{x\}) and t{x↦x′}t\{x \mapsto x'\} is the checked bound renaming of the syntax design. The case split is exact: the binder is renamed only when a replacement that is actually inserted into tt mentions xx free.

Design decisions

Simultaneous, one pass

Problem. Should {x↦y, y↦2}\{x \mapsto y,\ y \mapsto 2\} applied to x+yx + y give y+2y + 2 or 2+22 + 2?

Options. Sequential substitution (apply the entries one after another), iterated substitution (repeat until nothing changes), or simultaneous substitution (every variable is looked up once in the original term).

Choice. Simultaneous. The variable case xσ=σ(x)x\sigma = \sigma(x) looks xx up once and never visits the inserted term again:

(x+y){x↦y, y↦2}=x{… }+y{… }=y+2.\begin{aligned} (x + y)\{x \mapsto y,\ y \mapsto 2\} &= x\{\dots\} + y\{\dots\} \\ &= y + 2 . \end{aligned}

Simultaneous substitution is the one with an algebra (composition below), it always terminates, and it expresses swaps such as {x↦y, y↦x}\{x \mapsto y,\ y \mapsto x\} directly. Sequential application is still available as composition with then, and iteration to a fixed point is a policy of the caller. The generic version is called apply_once to make this explicit.

Rename binders only when needed

Problem. Capture happens when a binder βx\beta x lies above an inserted replacement in which xx is free. Renaming every binder avoids it but makes results unreadable.

Choice. Rename only when x∈Rσ′(FV(t))x \in R_{\sigma'}(\mathrm{FV}(t)), and then choose the fresh name with the hint xx. The fresh name must avoid three sets, each for its own reason:

  • FV\mathrm{FV} of the relevant replacements, or the renamed binder would capture them again;
  • dom⁡σ′\operatorname{dom}\sigma', or the occurrences that were renamed from xx to x′x' would themselves be substituted (regression test “fresh binders avoid the substitution domain”);
  • names(t)\mathrm{names}(t), so that the checked bound renaming t{x↦x′}t\{x \mapsto x'\} cannot fail, and the renamed variable cannot be confused with an inner binder of the same name.

supp⁡σ′\operatorname{supp}\sigma' covers the first two sets. The abort in the implementation is therefore unreachable: by the freshness lemma x′∉names(t)x' \notin \mathrm{names}(t), which is exactly the side condition of alpha_rename_bound.

Composition as a first-class operation

then builds σ;τ\sigma \mathbin{;} \tau with

(σ;τ)(x)={σ(x) τx∈dom⁡σ,τ(x)x∈dom⁡τ∖dom⁡σ,xotherwise,(\sigma \mathbin{;} \tau)(x) = \begin{cases} \sigma(x)\,\tau & x \in \operatorname{dom}\sigma,\\ \tau(x) & x \in \operatorname{dom}\tau \setminus \operatorname{dom}\sigma,\\ x & \text{otherwise,} \end{cases}

so that sequential application can be expressed as one simultaneous substitution, which is cheaper (one traversal) and has the laws below.

Correctness and invariants

Substitution and GenericSubstitution are abstract types: only the functions of the package build them, each of those keeps at most one entry per name, and to_array returns a copy of the entries. So every value denotes a map σ\sigma and keeps denoting the same map. The replacement terms are shared, not copied; the laws assume, as the whole library does, that terms are not changed in place.

The laws below are proved through the nameless reading of terms. The direct argument by induction on named terms is the classical one, but its binder case has to compare results whose fresh names differ, and that comparison is exactly what the nameless reading makes trivial.

The nameless reading

Let ⌜t⌝\ulcorner t \urcorner be @debruijn.from_named(t), the translation of the debruijn design: bound occurrences become indices, free names stay names. It is computed by closing each binder’s variable. For a nameless term dd, let κxk d\kappa^k_x\,d replace every free name xx under jj inner binders by the index k+jk + j; then

⌜v⌝=v,⌜x⌝=x,⌜t(u1,…,un)⌝=⌜t⌝(⌜u1⌝,…,⌜un⌝),⌜βx. t⌝=β. κx0⌜t⌝.\ulcorner v \urcorner = v, \qquad \ulcorner x \urcorner = x, \qquad \ulcorner t(u_1, \dots, u_n) \urcorner = \ulcorner t \urcorner(\ulcorner u_1 \urcorner, \dots, \ulcorner u_n \urcorner), \qquad \ulcorner \beta x.\, t \urcorner = \beta.\, \kappa^0_x \ulcorner t \urcorner .

This is the same as the environment-based definition of from_named: the occurrences of xx that are still free in ⌜t⌝\ulcorner t \urcorner are exactly those whose nearest binder is this one. Translations are locally closed: every index points at a binder inside the term. Two facts are used below.

  • (T1) t=αu  ⟺  ⌜t⌝=⌜u⌝t =_\alpha u \iff \ulcorner t \urcorner = \ulcorner u \urcorner (de Bruijn’s theorem, see the syntax design).
  • (T2) If y∉names(t)y \notin \mathrm{names}(t), then κy0⌜t{x↦y}⌝=κx0⌜t⌝\kappa^0_y \ulcorner t\{x \mapsto y\} \urcorner = \kappa^0_x \ulcorner t \urcorner. The free name yy occurs in ⌜t{x↦y}⌝\ulcorner t\{x \mapsto y\} \urcorner exactly where xx occurs free in ⌜t⌝\ulcorner t \urcorner, because yy occurs nowhere in tt, so closing yy in the one gives the same indices as closing xx in the other.

On nameless terms, substitution for free names is plain replacement. For a substitution σ\sigma write d⟨σ⟩d\langle\sigma\rangle for dd with every free name zz replaced by ⌜σ(z)⌝\ulcorner \sigma(z) \urcorner. No binder is renamed and no index is shifted: the inserted terms are locally closed, so they mean the same under any number of binders.

The translation lemma

Lemma 0. ⌜tσ⌝=⌜t⌝⟨σ⟩\ulcorner t\sigma \urcorner = \ulcorner t \urcorner\langle\sigma\rangle for every term tt and substitution σ\sigma.

By induction on the size of tt, for all σ\sigma at once. Values, variables and applications are immediate. For βx. t\beta x.\,t let σ′=σ∖x\sigma' = \sigma \setminus x. Closing commutes with replacement under two conditions, checked occurrence by occurrence:

κxk(d⟨σ′⟩)=(κxkd)⟨σ′⟩if x∉dom⁡σ′ and x∉FV(σ′(z)) for every free name z∈dom⁡σ′ of d.(C)\kappa^k_x\big(d\langle\sigma'\rangle\big) = \big(\kappa^k_x d\big)\langle\sigma'\rangle \quad\text{if } x \notin \operatorname{dom}\sigma' \text{ and } x \notin \mathrm{FV}(\sigma'(z)) \text{ for every free name } z \in \operatorname{dom}\sigma' \text{ of } d . \tag{C}

The first condition holds because xx was removed. The free names of ⌜t⌝\ulcorner t \urcorner are FV(t)\mathrm{FV}(t), so the second is exactly x∉Rσ′(FV(t))x \notin R_{\sigma'}(\mathrm{FV}(t)). Hence, without renaming,

⌜(βx. t)σ⌝=β. κx0⌜tσ′⌝=β. κx0(⌜t⌝⟨σ′⟩)induction=β. (κx0⌜t⌝)⟨σ′⟩(C)=β. (κx0⌜t⌝)⟨σ⟩=⌜βx. t⌝⟨σ⟩,x is not free in κx0⌜t⌝\begin{aligned} \ulcorner (\beta x.\,t)\sigma \urcorner &= \beta.\, \kappa^0_x \ulcorner t\sigma' \urcorner = \beta.\, \kappa^0_x \big(\ulcorner t \urcorner\langle\sigma'\rangle\big) && \text{induction} \\ &= \beta.\, \big(\kappa^0_x \ulcorner t \urcorner\big)\langle\sigma'\rangle && \text{(C)} \\ &= \beta.\, \big(\kappa^0_x \ulcorner t \urcorner\big)\langle\sigma\rangle = \ulcorner \beta x.\,t \urcorner\langle\sigma\rangle , && x \text{ is not free in } \kappa^0_x \ulcorner t \urcorner \end{aligned}

and with renaming to x′x', which lies outside supp⁡σ′∪names(t)\operatorname{supp}\sigma' \cup \mathrm{names}(t),

⌜(βx. t)σ⌝=β. κx′0(⌜t{x↦x′}⌝⟨σ′⟩)induction, same size=β. (κx′0⌜t{x↦x′}⌝)⟨σ′⟩(C) for x′∉supp⁡σ′=β. (κx0⌜t⌝)⟨σ′⟩=⌜βx. t⌝⟨σ⟩.(T2)\begin{aligned} \ulcorner (\beta x.\,t)\sigma \urcorner &= \beta.\, \kappa^0_{x'} \big(\ulcorner t\{x \mapsto x'\} \urcorner\langle\sigma'\rangle\big) && \text{induction, same size} \\ &= \beta.\, \big(\kappa^0_{x'} \ulcorner t\{x \mapsto x'\} \urcorner\big)\langle\sigma'\rangle && \text{(C) for } x' \notin \operatorname{supp}\sigma' \\ &= \beta.\, \big(\kappa^0_{x} \ulcorner t \urcorner\big)\langle\sigma'\rangle = \ulcorner \beta x.\,t \urcorner\langle\sigma\rangle . && \text{(T2)} \end{aligned}

□\square

Everything else follows from Lemma 0, because replacement on locally closed nameless terms is a homomorphism with no side conditions.

Free variables

Lemma 1. FV(tσ)=⋃z∈FV(t)FV(σ(z))\mathrm{FV}(t\sigma) = \bigcup_{z \in \mathrm{FV}(t)} \mathrm{FV}(\sigma(z)).

The free names of ⌜t⌝⟨σ⟩\ulcorner t \urcorner\langle\sigma\rangle are those of the inserted ⌜σ(z)⌝\ulcorner \sigma(z) \urcorner for the free names zz of ⌜t⌝\ulcorner t \urcorner, that is for z∈FV(t)z \in \mathrm{FV}(t) (with σ(z)=z\sigma(z) = z outside the domain). □\square

Corollary (no capture). A variable free in an inserted replacement σ(z)\sigma(z), z∈FV(t)z \in \mathrm{FV}(t), is free in tσt\sigma.

Only the free variables matter

Lemma 2. tσ=αt (σ∣FV(t))t\sigma =_\alpha t\,(\sigma|_{\mathrm{FV}(t)}), where σ∣S\sigma|_S is restrict(S). More generally, if σ(z)=ατ(z)\sigma(z) =_\alpha \tau(z) for every z∈FV(t)z \in \mathrm{FV}(t), then tσ=αtτt\sigma =_\alpha t\tau.

⌜t⌝⟨σ⟩\ulcorner t \urcorner\langle\sigma\rangle consults σ\sigma only at the free names of ⌜t⌝\ulcorner t \urcorner, and only through ⌜σ(z)⌝\ulcorner \sigma(z) \urcorner, which (T1) makes invariant under =α=_\alpha. Equal translations mean alpha-equivalent terms by (T1). The two sides need not be equal as values, because the fresh binder names avoid the support of different substitutions. □\square

Alpha-invariance

Lemma 3. If t=αt′t =_\alpha t' then tσ=αt′σt\sigma =_\alpha t'\sigma.

By (T1), ⌜t⌝=⌜t′⌝\ulcorner t \urcorner = \ulcorner t' \urcorner, so ⌜tσ⌝=⌜t⌝⟨σ⟩=⌜t′σ⌝\ulcorner t\sigma \urcorner = \ulcorner t \urcorner\langle\sigma\rangle = \ulcorner t'\sigma \urcorner by Lemma 0, and (T1) again gives the claim. □\square

Substitution is therefore well defined on alpha-equivalence classes, and the choice of fresh names never matters semantically.

Composition

Lemma 4. t(σ;τ)=α(tσ)τt(\sigma \mathbin{;} \tau) =_\alpha (t\sigma)\tau.

By Lemma 0 on both sides it suffices to show d⟨σ⟩⟨τ⟩=d⟨σ;τ⟩d\langle\sigma\rangle\langle\tau\rangle = d\langle\sigma \mathbin{;} \tau\rangle for a nameless dd. Replacement is homomorphic, so only a free name zz needs checking, and it splits as in the definition of σ;τ\sigma \mathbin{;} \tau:

z∈dom⁡σ:z⟨σ⟩⟨τ⟩=⌜σ(z)⌝⟨τ⟩=⌜σ(z) τ⌝=z⟨σ;τ⟩Lemma 0,z∈dom⁡τ∖dom⁡σ:z⟨σ⟩⟨τ⟩=z⟨τ⟩=⌜τ(z)⌝=z⟨σ;τ⟩,otherwise:z⟨σ⟩⟨τ⟩=z=z⟨σ;τ⟩.\begin{aligned} z \in \operatorname{dom}\sigma:&\quad z\langle\sigma\rangle\langle\tau\rangle = \ulcorner \sigma(z) \urcorner\langle\tau\rangle = \ulcorner \sigma(z)\,\tau \urcorner = z\langle\sigma \mathbin{;} \tau\rangle && \text{Lemma 0},\\ z \in \operatorname{dom}\tau \setminus \operatorname{dom}\sigma:&\quad z\langle\sigma\rangle\langle\tau\rangle = z\langle\tau\rangle = \ulcorner \tau(z) \urcorner = z\langle\sigma \mathbin{;} \tau\rangle,\\ \text{otherwise}:&\quad z\langle\sigma\rangle\langle\tau\rangle = z = z\langle\sigma \mathbin{;} \tau\rangle . \end{aligned}

□\square

Corollary (substitution lemma). For x≠yx \ne y and x∉FV(r)x \notin \mathrm{FV}(r),

t[x:=s][y:=r]  =α  t[y:=r][x:=s[y:=r]].t[x := s][y := r] \;=_\alpha\; t[y := r]\big[x := s[y := r]\big].

By Lemma 4 each side is one simultaneous substitution applied to tt. The left is {x↦s[y:=r], y↦r}\{x \mapsto s[y := r],\ y \mapsto r\}. The right is {y↦r[x:=s[y:=r]], x↦s[y:=r]}\{y \mapsto r[x := s[y := r]],\ x \mapsto s[y := r]\}, and r[x:=s[y:=r]]=αrr[x := s[y := r]] =_\alpha r by Lemma 2, because x∉FV(r)x \notin \mathrm{FV}(r) and the empty substitution is the identity. The two substitutions agree up to =α=_\alpha at every name, so Lemma 2 makes the results alpha-equivalent.11 Barendregt, The Lambda Calculus, Lemma 2.1.16. This is the lemma that makes beta reduction commute with substitution in the lambda calculus packages: M→βNM \to_\beta N implies Mσ→βNσM\sigma \to_\beta N\sigma up to =α=_\alpha.

Renamings embed into substitutions

Term::rename_free obeys the analogue of Lemma 0, ⌜tρ⌝=⌜t⌝⟨ρ⟩\ulcorner t\rho \urcorner = \ulcorner t \urcorner\langle\rho\rangle, with the same proof: its test x∉tgt⁡(ρ∖x)x \notin \operatorname{tgt}(\rho \setminus x) implies the second condition of (C), and its fresh name avoids supp⁡(ρ∖x)∪names(t)\operatorname{supp}(\rho \setminus x) \cup \mathrm{names}(t). For a list of names L⊇FV(t)L \supseteq \mathrm{FV}(t), from_renaming(L, ρ) is σρ={x↦ρ(x)∣x∈L, ρ(x)≠x}\sigma_\rho = \{x \mapsto \rho(x) \mid x \in L,\ \rho(x) \ne x\} (as variables). It agrees with ρ\rho on FV(t)\mathrm{FV}(t), so tσρ=αtρt\sigma_\rho =_\alpha t\rho. The two results may still differ as values: rename_free freshens a binder whenever it is a target of the renaming, while apply freshens it only when an inserted variable would be captured.

Cost

apply computes the free variables of every replacement once. At each binder it removes one entry from the substitution and looks for entries whose replacement has the binder free; only if there is one does it compute the free variables of the body (see the capture test below). So a term of size nn costs O(n⋅∣σ∣)O(n \cdot |\sigma|) besides the free variables of the replacements, and each binder that is a candidate for capture adds one traversal of its body, each renamed binder one more. then costs one apply and one set per entry of self, so O(k2)O(k^2) entry copies for kk entries besides the applications.

Generic substitution

GenericSubstitution::apply_once is the same definition with every constructor replaced by its BindingSyntax counterpart. Reading a node through its view gives a named term, so Lemmas 0–3 hold for any implementation that satisfies the view laws of the adapter design. On Term[T] the two algorithms coincide.

Traversal with an explicit work stack

Problem. The definition of tσt\sigma is a structural recursion, and run as host recursion it overflows the js, wasm and wasm-gc stacks on terms nested a few thousand levels deep.

Choice. Substitution::apply and GenericSubstitution::apply_once are one private loop, substitute, over the view of the generic substitution above. It is an instance of the rebuild scheme of the syntax design with the context c=σc = \sigma and

binder⁡(x,t,σ)={(x, t, σ′)if x∉Rσ′(FV(t)),(x′, t{x↦x′}, σ′∖x′)otherwise,\operatorname{binder}(x, t, \sigma) = \begin{cases} (x,\ t,\ \sigma') & \text{if } x \notin R_{\sigma'}(\mathrm{FV}(t)),\\ (x',\ t\{x \mapsto x'\},\ \sigma' \setminus x') & \text{otherwise,} \end{cases}

with σ′=σ∖x\sigma' = \sigma \setminus x and x′x' as in the definition. By the rebuild lemma the loop computes exactly tσt\sigma, the same term and the same fresh names as the recursion, using a constant amount of host stack; the analyses it calls (FV\mathrm{FV}, names\mathrm{names} and the checked bound renaming) are stack-safe as well. On Term[T], Value and the variables outside the domain are returned as they are instead of being rebuilt, which gives an equal term.

The capture test. The condition x∈Rσ′(FV(t))x \in R_{\sigma'}(\mathrm{FV}(t)) is decided in a cheaper order. Unfolding RR,

x∈Rσ′(FV(t))  ⟺  ∃z∈dom⁡σ′.  x∈FV(σ′(z)) ∧ z∈FV(t)  ⟺  ∃z∈C.  z∈FV(t),x \in R_{\sigma'}(\mathrm{FV}(t)) \iff \exists z \in \operatorname{dom}\sigma'.\; x \in \mathrm{FV}(\sigma'(z)) \,\wedge\, z \in \mathrm{FV}(t) \iff \exists z \in C.\; z \in \mathrm{FV}(t),

where C={z∈dom⁡σ′∣x∈FV(σ′(z))}C = \{z \in \operatorname{dom}\sigma' \mid x \in \mathrm{FV}(\sigma'(z))\} is the set of candidates. The sets FV(σ(z))\mathrm{FV}(\sigma(z)) are computed once per call, and since σ′⊆σ\sigma' \subseteq \sigma as a set of entries they serve every binder. If C=∅C = \varnothing the condition is false without looking at tt; otherwise FV(t)\mathrm{FV}(t) is computed once. The boolean is the same, so the result is too, but a chain of nn binders none of which can capture now costs O(n)O(n) instead of O(n2)O(n^2).

The tests in src/substitution/stack_safety_test.mbt run substitution, freshening at the outermost binder, deep replacements, composition and the generic substitution on terms nested 100 000 levels deep, on every backend.

Alternatives rejected

  • The Barendregt variable convention. Assuming that bound names are distinct from all free names would remove the renaming case, but terms come from users and from other algorithms, and the convention is not preserved by reduction. The library renames explicitly instead.
  • Substitution on De Bruijn terms only. Index-based substitution needs no renaming and is provided by debruijn, but the shared interface for downstream ASTs is named, so named substitution must be correct on its own.
  • Iterated substitution. Repeating until no domain variable occurs can diverge (x↦f(x)x \mapsto f(x)) and has no composition law. Callers that want a fixed point iterate explicitly.

Boundaries

  • No unification, matching or occurs check: substitutions are given, not solved for.
  • GenericSubstitution has no then or restrict; compose generic substitutions by applying them in turn.
  • Results are equal to the textbook definition only up to =α=_\alpha; use @syntax.alpha_equal to compare them.
  • Stack safety is in the nesting depth, not in the cost: a chain of nn binders every one of which is renamed still costs O(n2)O(n^2), because each renaming traverses the body below it.
  • Values are never entered, so a Value payload that contains variables is not substituted into.

Footnotes

  1. Barendregt, The Lambda Calculus, Lemma 2.1.16. ↩