debruijn design

Design goal

Named syntax needs fresh names and renaming in every binder-crossing operation. debruijn provides a representation in which alpha-equivalence is syntactic equality and beta reduction needs no renaming at all, together with exact translations to and from named syntax. It is the kernel representation of the untyped NbE in utlc/nbe and a reference reducer against which the named calculus in utlc/lambda is tested.

Constraints

  • Exact translations. Converting to indices and back must lose nothing but binder names.
  • Errors are values. An ill-scoped index is reported as a ScopeError, never by an abort or a silently wrong term where the operation can detect it.
  • Open terms. Free variables come from users and downstream ASTs, so they stay names instead of needing a global numbering.

Mathematical background

Indices

In a term with nameless binders, a bound occurrence is a number ii, its De Bruijn index: the number of binders between the occurrence and the binder it refers to.11 N. G. de Bruijn, “Lambda calculus notation with nameless dummies”, Indagationes Mathematicae 34, 1972. With λ\lambda for Bind and juxtaposition for Apply:

λx. λy. x y  ⇝  λ. λ. 1  0,λx. x (λy. x y)  ⇝  λ. 0 (λ. 1 0).\lambda x.\,\lambda y.\,x\,y \;\rightsquigarrow\; \lambda.\,\lambda.\,1\;0 , \qquad \lambda x.\,x\,(\lambda y.\,x\,y) \;\rightsquigarrow\; \lambda.\,0\,(\lambda.\,1\,0).

The same variable has different indices at different depths (here xx is 00 and then 11). Free variables stay named (Free(x)), a “locally nameless” choice for the free part that keeps translations simple.

A term is well scoped at depth nn when every index under kk binders is smaller than n+kn + k; it is well scoped when it is well scoped at depth 00. validate decides this.

Levels

The level of a binder is its depth counted from the root. An occurrence under nn binders with index ii refers to the binder at level

ℓ=n−1−i,equivalentlyi=n−1−ℓ.\ell = n - 1 - i, \qquad\text{equivalently}\qquad i = n - 1 - \ell .

When a term is moved under one more binder (weakening: the context grows at its inner end), the indices of its free variables must be shifted by one, but their levels do not change. The NbE evaluator therefore uses levels for the variables it invents while going under binders, so that semantic values can be moved deeper without shifting, and converts back with i=n−1−ℓi = n - 1 - \ell when quoting.

Shifting

↑cd t\uparrow^{d}_{c}\,t adds dd to every index of tt that is free relative to cutoff c≥0c \ge 0:

↑cd i={ii<ci+di≥c,↑cd (λ. t)=λ. ↑c+1dt,↑cd (t uˉ)=(↑cdt) (↑cdu‾),↑cd v=v,↑cd x=x (x free).\begin{aligned} \uparrow^{d}_{c}\,i &= \begin{cases} i & i < c \\ i + d & i \ge c \end{cases}, & \uparrow^{d}_{c}\,(\lambda.\,t) &= \lambda.\,\uparrow^{d}_{c+1} t, \\ \uparrow^{d}_{c}\,(t\,\bar u) &= (\uparrow^{d}_{c} t)\,(\overline{\uparrow^{d}_{c} u}), & \uparrow^{d}_{c}\,v &= v, \quad \uparrow^{d}_{c}\,x = x \ (x \text{ free}). \end{aligned}

The shift is defined when no index crosses its cutoff: there is no index i≥ci \ge c with i+d<ci + d < c, where cc is the cutoff at that occurrence. Such an index would be captured by an enclosing binder, or become negative. A shift with d≥0d \ge 0 is always defined.

shift(t, d, c) implements it by carrying the binder depth kk and testing i≥c+ki \ge c + k, which unfolds the recursion on cc. A negative cc is outside the domain: it would count indices bound inside tt as free, so shift rejects it with NegativeCutoff before looking at tt.

Substitution of an index

[j↦s] t[j \mapsto s]\,t replaces the free index jj by ss:

[j↦s] i={si=jii≠j,[j↦s] (λ. t)=λ. [ j+1↦↑01s ] t.[j \mapsto s]\,i = \begin{cases} s & i = j \\ i & i \ne j \end{cases}, \qquad [j \mapsto s]\,(\lambda.\,t) = \lambda.\,[\,j+1 \mapsto \uparrow^{1}_{0} s\,]\,t .

substitute_bound unfolds the binder case: under kk binders it replaces index j+kj + k by ↑0ks\uparrow^{k}_{0} s. The two agree because shifts with the same cutoff compose (Lemma 1 below): ↑01⋯↑01s=↑0ks\uparrow^{1}_{0}\cdots\uparrow^{1}_{0} s = \uparrow^{k}_{0} s.

Beta reduction

(λ. t)  s  →β  ↑0−1( [ 0↦↑01s ]  t ).(\lambda.\,t)\;s \;\to_\beta\; \uparrow^{-1}_{0}\big(\,[\,0 \mapsto \uparrow^{1}_{0} s\,]\;t\,\big) .

The argument is shifted up because it moves under the binder of tt; after the substitution the binder is removed, so all remaining free indices of the body are shifted down. This is instantiate(t, s).22 B. C. Pierce, Types and Programming Languages, MIT Press 2002, §6.2–6.3.

Design decisions

Shifting is checked, not assumed

Problem. A downward shift can move a free index below its cutoff. Below zero the index refers to nothing; between zero and the cutoff it is captured by an enclosing binder. Either way the result is a silently wrong term.

Choice. shift returns Err(NegativeShift) when a free index would cross its cutoff and Err(NegativeCutoff) for a negative cutoff, and shift, substitute_bound, instantiate, validate and to_named report NegativeIndex for any negative index they meet. The lemmas below show that the error cases are unreachable from well-scoped input, so the Result costs nothing for correct callers and turns a silent corruption into data for incorrect ones. This follows the library rule that expected failures at public boundaries are structured values.

The index operations work on open terms, so they cannot tell a dangling (too large) index from one that points at a binder outside the term: instantiate shifts it like a free index. The reducers work on whole terms and validate them. reduce_once runs validate before searching and returns its error as ScopeFailure; normalize validates its input once, and Lemma 2 below shows that the later terms need no check. The search itself is a private function that is also applied to binder bodies, whose indices may point at enclosing binders.

Instantiation never fails on well-scoped input

Lemma 1 (shift composition). For a,b≥0a, b \ge 0: ↑ca↑cbt=↑ca+bt\uparrow^{a}_{c}\uparrow^{b}_{c} t = \uparrow^{a+b}_{c} t, and ↑c0t=t\uparrow^{0}_{c} t = t.

On an index ii relative to the current cutoff: if i<ci < c both sides leave it; if i≥ci \ge c then i+b≥ci + b \ge c, so

↑ca↑cb i=(i+b)+a=↑ca+b i.\uparrow^{a}_{c}\uparrow^{b}_{c}\, i = (i + b) + a = \uparrow^{a+b}_{c}\, i .

The binder case raises the cutoff on both sides alike. □\square

Lemma 2 (safety of the downward shift). Let λ. t\lambda.\,t and ss be well scoped at depth nn. Then every free index of u=[ 0↦↑01s ] tu = [\,0 \mapsto \uparrow^{1}_{0} s\,]\,t lies in {1,…,n}\{1, \dots, n\}, so ↑0−1u\uparrow^{-1}_{0} u succeeds and is well scoped at depth nn.

The body tt is well scoped at depth n+1n + 1, so its free indices lie in {0,…,n}\{0, \dots, n\}. Follow a free occurrence in tt under kk inner binders:

i=k+0:replaced by ↑0k↑01s=↑0k+1s (Lemma 1),whose free indices relative to the root are j+1∈{1,…,n} for j∈fi(s)⊆{0,…,n−1},i=k+m, m≥1:kept, with root-relative index m∈{1,…,n}.\begin{aligned} i = k + 0 &: \text{replaced by } \uparrow^{k}_{0}\uparrow^{1}_{0} s = \uparrow^{k+1}_{0} s \text{ (Lemma 1)}, \\ &\quad\text{whose free indices relative to the root are } j + 1 \in \{1, \dots, n\} \text{ for } j \in \mathrm{fi}(s) \subseteq \{0, \dots, n-1\}, \\ i = k + m,\ m \ge 1 &: \text{kept, with root-relative index } m \in \{1, \dots, n\}. \end{aligned}

No free index 00 remains. Under kk binders the shift by −1-1 has cutoff kk and moves the index k+mk + m with m≥1m \ge 1 to k+m−1≥kk + m - 1 \ge k, so no index crosses its cutoff, and the root-relative free indices are mapped {1,…,n}→{0,…,n−1}\{1, \dots, n\} \to \{0, \dots, n - 1\}. □\square

Hence instantiate and reduce_once return no ScopeError on well-scoped input, and a reduct of a well-scoped term is well scoped (subject reduction for scope). Together with the validation of the input, normalize returns ScopeFailure exactly when its input is ill scoped, and then with steps=0: Bind(Bound(-1)) gives NegativeIndex(index=-1) and (λ. 5) 1 gives UnboundIndex(index=5, depth=1).

The detailed proofs, including the commutation of shifts with different cutoffs and the De Bruijn substitution lemma, are in the attachment:

Index lemmas for De Bruijn terms

Exact translations

from_named keeps a stack of binder names and translates an occurrence of xx to the distance to the nearest binder of xx, or to Free(x) if there is none. to_named invents a binder name with fresh_name("x", U), where UU contains the free names of the term and the names of the enclosing binders.

Theorem (round trips).

  1. to_named(from_named(t))=αt\texttt{to\_named}(\texttt{from\_named}(t)) =_\alpha t for every named tt.
  2. from_named(to_named(d))=d\texttt{from\_named}(\texttt{to\_named}(d)) = d for every well-scoped dd.
  3. from_named(t)=from_named(u)  ⟺  t=αu\texttt{from\_named}(t) = \texttt{from\_named}(u) \iff t =_\alpha u.

For (2): along any path from the root, the names chosen by to_named are pairwise distinct and distinct from all free names, because each is chosen fresh for a set that contains the free names and every enclosing binder name. An occurrence of index ii under nn binders becomes the name of the binder at level n−1−in - 1 - i; translating back, the nearest binder with that name is that same binder (no other enclosing binder has the name), at distance ii. A Free(x) becomes xx, which is not the name of any binder on the path, so it translates back to Free(x). (3) is de Bruijn’s theorem; (1) follows from (2) and (3), since from_named(to_named(from_named(t)))=from_named(t)\texttt{from\_named}(\texttt{to\_named}(\texttt{from\_named}(t))) = \texttt{from\_named}(t).

Named and nameless beta agree

For named terms, beta is (λx. b) a→b[x:=a](\lambda x.\,b)\,a \to b[x := a] with the capture-avoiding substitution of substitution. Write ⌜b⌝x\ulcorner b \urcorner_{x} for the translation of bb with xx as the innermost binder. Then

⌜b[x:=a]⌝  =  instantiate(⌜b⌝x, ⌜a⌝),\ulcorner b[x := a] \urcorner \;=\; \texttt{instantiate}\big(\ulcorner b \urcorner_{x},\ \ulcorner a \urcorner\big),

by induction on bb: an occurrence of xx under kk inner binders has index kk and receives ↑0k⌜a⌝\uparrow^{k}_{0}\ulcorner a \urcorner, which is the translation of aa placed under those kk binders; at the root ⌜a⌝\ulcorner a \urcorner has no free indices, so the shift changes nothing. Renaming of binders by the named substitution is invisible after translation by (3), and the translation lemma of the substitution design makes this induction precise. Since the translation also preserves the shape of terms, the beta-only named reducer (@eval.evaluate with @lambda.beta_rule and NormalOrder) and reduce_once choose the same leftmost-outermost redex, and one step commutes with translation up to =α=_\alpha. (@lambda.normalize also contracts eta redexes, so its normal forms can be eta-shorter.) The test “named and debruijn beta reduction agree modulo alpha” checks an instance that needs renaming on the named side.

Spines and reduction order

Apply(head, args) is a curried spine: Apply(Bind(t), [a, ..rest]) is the redex (λ. t) a(\lambda.\,t)\,a applied to rest, and a step contracts only the first argument. An empty spine Apply(h, []) stands for hh itself, so empty applications around the Bind are looked through when a redex is matched. reduce_once searches root, head, arguments, and enters binders: it is the normal-order strategy of the eval design on nameless terms, so the normalization theorem applies to normalize.

Explicit stacks instead of host recursion

Problem. Every operation above is defined by structural recursion on the term, and a direct implementation keeps one host stack frame per level of nesting. Terms from users and downstream ASTs can be deep (a long spine f (f (f⋯ ))f\,(f\,(f \cdots)), a long chain of binders), and the js, wasm and wasm-gc stacks overflow at a depth of a few thousand, so the same call returned a result on native and crashed on the other backends.

Choice. Each operation runs as a loop over an explicit stack in a heap array, and visits the subterms in exactly the order of its recursive definition, so its results, including the error it reports, are unchanged. The host stack depth is constant; the heap stacks hold at most one entry per node of the term.

Checks. validate and == keep a stack of the subterms (or pairs of subterms) still to visit. Write pre(t)\mathrm{pre}(t) for the nodes of tt in pre-order, head before arguments and arguments from left to right:

pre(λ. t)=λ⋅pre(t),pre(t u1⋯um)=@⋅pre(t)⋅pre(u1)⋯pre(um),\mathrm{pre}(\lambda.\,t) = \lambda \cdot \mathrm{pre}(t), \qquad \mathrm{pre}(t\,u_1 \cdots u_m) = @ \cdot \mathrm{pre}(t) \cdot \mathrm{pre}(u_1) \cdots \mathrm{pre}(u_m),

and pre(a)=a\mathrm{pre}(a) = a for a leaf aa. The loop pops the top entry, checks it if it is a leaf, and otherwise pushes its children, umu_m first and the head tt last. With the stack t1⋯trt_1 \cdots t_r (top first) it maintains

pre(t1)⋯pre(tr)  =  the nodes of the input not visited yet, in pre-order,\mathrm{pre}(t_1) \cdots \mathrm{pre}(t_r) \;=\; \text{the nodes of the input not visited yet, in pre-order},

since popping t u1⋯umt\,u_1 \cdots u_m and pushing its children replaces pre(t u⃗)\mathrm{pre}(t\,\vec u) by its tail. So the leaves are checked in pre-order. The recursive validate returns the first error of its head, else of its arguments from left to right, which by induction is also the first error in pre-order. Both return the same error. An index is checked against the number of binders above it, which the stack stores with each entry.

Translations. shift, substitute_bound (both through one rebuilding traversal that applies a function to each Bound(i) and its binder count), from_named and to_named produce a term. They keep a stack of tasks, visit(t,k)\mathsf{visit}(t, k) and build@(m)\mathsf{build}_@(m) / buildλ\mathsf{build}_\lambda, and a stack of results. Visiting a leaf pushes its translation; visiting t u1⋯umt\,u_1 \cdots u_m pushes build@(m)\mathsf{build}_@(m) and above it the visits of um,…,u1,tu_m, \dots, u_1, t; build@(m)\mathsf{build}_@(m) pops the m+1m + 1 translations and pushes the application of the first to the others. By induction on tt, running the tasks pushed for visit(t,k)\mathsf{visit}(t, k) pushes exactly one result, the recursive translation of tt under kk binders, and leaves the rest of both stacks untouched. Leaves are visited in pre-order as above; errors arise only at leaves, and both versions stop at the first one, so they return the same Err. In substitute_bound the replacement at an occurrence is shifted by the same loop, run to its end before the traversal continues.

from_named keeps, for each name, the levels of the enclosing binders of that name, and maps an occurrence under nn binders to n−1−ℓn - 1 - \ell for the innermost level ℓ\ell, the index of the nearest binder of that name. to_named names a binder with the first name of x,x1,x2,…x, x_1, x_2, \dots that is neither free in the term (FF) nor the name of an enclosing binder. Let c0,c1,…c_0, c_1, \dots be the names of that sequence not in FF. By induction on the level, the enclosing binders of a binder at level nn are named c0,…,cn−1c_0, \dots, c_{n-1}, so it gets cnc_n: the name depends only on the level and is computed once. Both translations are therefore linear in the size of the term (up to hashing), where a copy of the binder stack at each binder and a rescan of the candidate names made a chain of nn binders cost O(n2)O(n^2).

Reduction. The recursive search of reduce_once tries a node, then its head, then its arguments from left to right, and enters binder bodies, and it contracts the first redex it meets: the first redex in pre-order. The loop walks the term in pre-order and keeps the path from the root, each ancestor with the child it descended into; when a leaf ends a branch it returns to the nearest application with an argument left. When it meets a redex it contracts it, then rebuilds the ancestors from the innermost outwards and prepends one frame per ancestor to the reduction path, which is the order in which the recursive calls return. The steps, their paths and their counts in normalize are therefore unchanged.

The tests in src/debruijn/deep_test.mbt run every operation on terms nested to depth 100 000 (application chains in head and in argument position, binder chains with indices that reach the outermost binder, and mixtures of these, well scoped and ill scoped) on every backend; before the change each of them overflowed the stack on js, wasm and wasm-gc.

Correctness and invariants

  • validate(from_named(t)) == Ok(()) for every named tt.
  • Round trips (1)–(3) above; == on well-scoped DbTerm is alpha-equivalence.
  • Lemma 2: on well-scoped input instantiate succeeds and preserves scope; reduce_once never returns ScopeFailure, and every reduct is well scoped. On ill-scoped input reduce_once and normalize return ScopeFailure with the error validate reports.
  • reduce_once satisfies the one-redex contract of rewrite, with rule name "beta".
  • shift(t, 0, c) == Ok(t) for every t without negative indices, and shifts with one cutoff compose (Lemma 1).
  • shift never turns a free index into a bound one: if a free index would fall below its cutoff, it returns Err(NegativeShift), and it rejects a negative cutoff, which would free bound indices, with Err(NegativeCutoff).

Cost: shift, validate, from_named and to_named are linear in the term size (the translations up to hashing of names). substitute_bound costs O(∣t∣+m⋅∣s∣)O(|t| + m \cdot |s|) for mm occurrences of the index, because each inserted copy is shifted. A reduce_once step costs one search plus one instantiate, plus building the reduction path, which has one frame per ancestor of the redex and is built with @rewrite.ReductionPath::prepend.

Stack: every operation uses a constant depth of host stack, whatever the nesting depth of the term; its pending work is kept in heap arrays with at most one entry per node.

Alternatives rejected

  • Levels instead of indices in syntax. Levels make weakening free but substitution under binders more complex; indices are the standard for syntax, and levels are used where they help (NbE).
  • Fully nameless free variables. Free variables are kept as names so that open terms from users and downstream ASTs need no global variable numbering, and so that to_named can restore them exactly.
  • Explicit substitutions. A calculus with suspended substitutions avoids repeated shifting but complicates every consumer; the NbE package provides the efficient path instead.

Boundaries

  • Binder names are not preserved: to_named chooses x, x_1, ….
  • shift, substitute_bound and instantiate operate on open terms and do not detect dangling indices; the reducers and validate do.
  • An empty application Apply(h, []) is read as h when a redex is matched, so it never hides a redex, but reduce_once leaves the node in place, whereas utlc/nbe drops it from its normal forms.
  • Only beta is implemented; there is no eta rule for De Bruijn terms.
  • Values are opaque; their contents are never shifted.
  • The derived Debug of DbTerm (and of the result types that contain one) recurses over the term, so printing a deeply nested term can overflow the host stack on js, wasm and wasm-gc. Every other operation, == included, handles any nesting depth.

Footnotes

  1. N. G. de Bruijn, “Lambda calculus notation with nameless dummies”, Indagationes Mathematicae 34, 1972. ↩

  2. B. C. Pierce, Types and Programming Languages, MIT Press 2002, §6.2–6.3. ↩