utlc/lambda design

Design goal

utlc/lambda is the untyped lambda calculus written as directly as possible on top of the shared layers: named terms from syntax, capture-avoiding substitution from substitution, and a strategy from eval. It is meant to be obviously right rather than fast, so that faster normalizers (debruijn, utlc/nbe) and the typed calculus stlc can be checked against it.

Constraints

  • Obviously right. The calculus is the reference that faster normalizers are tested against, so it reuses the shared substitution and strategies instead of reimplementing them.
  • Shared syntax. Terms are Term[T], so domain values ride along and every analysis of syntax applies.

Mathematical background

Lambda terms are t::=v∣x∣t u∣λx. tt ::= v \mid x \mid t\,u \mid \lambda x.\,t, encoded as Value, Variable, Apply(t, [u]) and Bind(x, t); an n-ary Apply(f, [a_1, …, a_n]) denotes the curried spine f a1⋯anf\,a_1 \cdots a_n. Terms are taken modulo =α=_\alpha.

Beta and eta

(β)(λx. b) a  →  b[x:=a],(η)λx. f x  →  fif x∉FV(f).\begin{aligned} (\beta)\quad & (\lambda x.\,b)\,a \;\to\; b[x := a], \\ (\eta)\quad & \lambda x.\,f\,x \;\to\; f \qquad \text{if } x \notin \mathrm{FV}(f). \end{aligned}

Both are closed under all contexts, including under λ\lambda (the ξ\xi rule). The side condition of eta is essential: without it λx. x x→x\lambda x.\,x\,x \to x would turn a closed term into an open one and identify functions that behave differently. Eta expresses extensionality: in the presence of β\beta, it is equivalent to the rule “if f x=g xf\,x = g\,x for a fresh xx then f=gf = g”, because

f  ←η  λx. f x  =  λx. g x  →η  g(x∉FV(f)∪FV(g)).f \;\leftarrow_\eta\; \lambda x.\,f\,x \;=\; \lambda x.\,g\,x \;\to_\eta\; g \qquad (x \notin \mathrm{FV}(f) \cup \mathrm{FV}(g)).

Classical properties

  • Confluence. →β\to_\beta and →βη\to_{\beta\eta} are Church–Rosser, so a term has at most one normal form up to =α=_\alpha.11 Barendregt, The Lambda Calculus, Theorems 3.2.8 and 3.3.9.
  • Normalization. If a term has a beta normal form, the leftmost-outermost strategy reaches it (normalization theorem, see the eval design).
  • Eta postponement. Every βη\beta\eta reduction can be rearranged so that all β\beta steps precede all η\eta steps; consequently a term has a βη\beta\eta normal form iff it has a β\beta normal form.22 Barendregt, The Lambda Calculus, §15.1.
  • Undecidability. Whether a term has a normal form is undecidable, which is why the normalizer takes a step limit.

Design decisions

Beta through the shared substitution

beta_rule calls Substitution::singleton(x, a).apply(b). All capture avoidance is therefore in one place, proved once in the substitution design. For the redex (λx. λy. x y) y(\lambda x.\,\lambda y.\,x\,y)\,y:

(λx. λy. x y) y→β(λy. x y)[x:=y]=λy1. (x y1)[x:=y]y∈FV(replacement), rename y=λy1. y y1→ηy.\begin{aligned} (\lambda x.\,\lambda y.\,x\,y)\,y &\to_\beta (\lambda y.\,x\,y)[x := y] \\ &= \lambda y_1.\,(x\,y_1)[x := y] && y \in \mathrm{FV}(\text{replacement}),\ \text{rename } y \\ &= \lambda y_1.\,y\,y_1 \\ &\to_\eta y . \end{aligned}

Alpha-invariance of substitution (Lemma 3 of the substitution design) is what makes beta well defined on alpha classes. The substitution lemma (Barendregt 2.1.16, derived there as well) makes beta commute with substitution, M→βN⇒Mσ→βNσM \to_\beta N \Rightarrow M\sigma \to_\beta N\sigma, which the confluence proofs rely on.

Spines are contracted one argument at a time

A redex is Apply(Bind(x, b), [a, ..rest]). It contracts with the first argument only and keeps the rest: (λx. b) a rˉ→b[x:=a] rˉ(\lambda x.\,b)\,a\,\bar r \to b[x := a]\,\bar r. This is beta on the curried reading of the spine, so every step is a single beta step and the step count equals the length of the corresponding curried reduction. The De Bruijn reducer makes the same choice, which keeps the two step by step comparable.

The redex (λx. b) a(\lambda x.\,b)\,a is a prefix of the spine but is reported at the spine’s position, the only position the n-ary tree has for it. The strategies of eval account for this, so each of them takes the same steps on Apply(f, [a1, a2]) and on Apply(Apply(f, [a1]), [a2]); in particular, applicative order contracts the prefix before any redex inside a2.

Eta only on unary applications

eta_rule matches Bind(x, Apply(f, [Variable(x)])). On the curried reading, λx. f a x\lambda x.\,f\,a\,x (written Apply(f, [a, x])) is also an eta redex, but recognising it would require splitting the spine and rebuilding Apply(f, [a]). The rule stays syntactic; callers who build spines n-ary and need eta can normalize spines to unary form first.

Rules with a bounded host stack

Problem. The rules look below the root of a term in two places: through empty applications Apply(h, []) around a head, a body or an argument, and into the function ff of an eta redex λx. f x\lambda x.\,f\,x to decide x∉FV(f)x \notin \mathrm{FV}(f). A deep ff, or a long chain of empty applications, must not overflow the js, wasm and wasm-gc stacks (issue #13).

Choice. Stripping empty applications is a loop. The side condition of eta is decided by a search instead of computing FV(f)\mathrm{FV}(f): a heap array holds the subterms still to be searched, and the search answers “free” at the first occurrence of xx that is not under a binder of xx. It computes the same answer, because

x∈FV(y)  ⟺  x=y,x∈FV(v)  ⟺  ⊥,x∈FV(h u1⋯un)  ⟺  x∈FV(h)∨⋁ix∈FV(ui),x∈FV(λy. b)  ⟺  x≠y∧x∈FV(b),\begin{aligned} x \in \mathrm{FV}(y) &\iff x = y, & x \in \mathrm{FV}(v) &\iff \bot, \\ x \in \mathrm{FV}(h\,u_1 \cdots u_n) &\iff x \in \mathrm{FV}(h) \vee \textstyle\bigvee_i x \in \mathrm{FV}(u_i), & x \in \mathrm{FV}(\lambda y.\,b) &\iff x \ne y \wedge x \in \mathrm{FV}(b), \end{aligned}

and the search unfolds exactly this disjunction: it pushes the head and the arguments of an application, and the body of a binder unless the binder is xx. The order in which the disjuncts are tried does not matter, since the search has no other effect; stopping at the first occurrence also avoids building the set. Beta substitutes with the stack-safe substitution, and normalize runs the stack-safe normal-order search of eval, so every function of the package uses a constant host stack depth.

Normal order with one combined rule

normalize runs beta_eta_rule with the NormalOrder strategy, so each step contracts the leftmost-outermost redex of either kind. The combined rule has one name, "beta_eta"; traces show positions but not which of the two rules fired. Because beta and eta redexes have different root constructors, the priority inside beta_eta_rule never changes the outcome of a step.

Beta then eta versus normalize

Problem. A caller may compute the beta normal form NN first, with @eval.evaluate(t, _, beta_rule, NormalOrder, _), and then its eta normal form MM, with eta_rule. Does MM agree with the result LL of normalize(t, _)?

Not literally. The rules read Apply(h, []) as hh when they match a redex, but no rule removes the node. On t=λz. (λy. y) (z ())t = \lambda z.\,(\lambda y.\,y)\,(z\,()), where z ()z\,() is Apply(Variable(z), []), beta alone gives N=λz. z ()N = \lambda z.\,z\,(), and λz. z ()\lambda z.\,z\,() is no eta redex because its body is not an application to zz; so M=NM = N. normalize instead contracts the eta redex at the root first, reading the argument z ()z\,() as zz, and returns L=λy. yL = \lambda y.\,y. MM and LL are not alpha-equivalent.

Agreement modulo empty applications. Let ⌊⋅⌋\lfloor \cdot \rfloor erase every empty application:

⌊v⌋=v,⌊x⌋=x,⌊λx. t⌋=λx. ⌊t⌋,⌊Apply(h,[])⌋=⌊h⌋,⌊Apply(h,[a1,…,an])⌋=Apply(⌊h⌋,[⌊a1⌋,…,⌊an⌋])(n≥1).\begin{aligned} \lfloor v \rfloor &= v, & \lfloor x \rfloor &= x, & \lfloor \lambda x.\,t \rfloor &= \lambda x.\,\lfloor t \rfloor, \\ \lfloor \texttt{Apply}(h, []) \rfloor &= \lfloor h \rfloor, & \lfloor \texttt{Apply}(h, [a_1, \dots, a_n]) \rfloor &= \texttt{Apply}(\lfloor h \rfloor, [\lfloor a_1 \rfloor, \dots, \lfloor a_n \rfloor]) \quad (n \ge 1). \end{aligned}

Call a term unary when each of its Apply nodes has at most one argument; terms built with application are unary. On a unary term, ⌊⋅⌋\lfloor \cdot \rfloor gives an ordinary lambda term with binary application.

Theorem. Let tt be unary. If @eval.evaluate(t, _, beta_rule, NormalOrder, _) returns NormalForm(N, _), @eval.evaluate(N, _, eta_rule, NormalOrder, _) returns NormalForm(M, _), and normalize(t, _) returns NormalForm(L, _), then ⌊M⌋=α⌊L⌋\lfloor M \rfloor =_\alpha \lfloor L \rfloor.

Proof. (1) Unary terms are closed under the rules. Beta on a unary redex Apply(h, [a]) returns b[x:=a]b[x := a] with no remaining spine; substitution copies the unary term aa into the unary term bb; eta returns a subterm. (2) Erasure is a step-by-step simulation on unary terms. Erasure keeps every name, so it commutes with capture-avoiding substitution up to =α=_\alpha: ⌊b[x:=a]⌋=α⌊b⌋[x:=⌊a⌋]\lfloor b[x := a] \rfloor =_\alpha \lfloor b \rfloor[x := \lfloor a \rfloor]. beta_rule fires on Apply(h, [a]) exactly when hh is a Bind wrapped in empty applications, that is, when ⌊Apply(h,[a])⌋=(λx. ⌊b⌋) ⌊a⌋\lfloor \texttt{Apply}(h, [a]) \rfloor = (\lambda x.\,\lfloor b \rfloor)\,\lfloor a \rfloor is a beta redex, and the contractum erases to its contractum. eta_rule fires on Bind(x, B) exactly when BB is Apply(f, [a]) up to empty applications with aa a wrapped Variable(x) and x∉FV(f)x \notin \mathrm{FV}(f), that is, when λx. ⌊B⌋=λx. ⌊f⌋ x\lambda x.\,\lfloor B \rfloor = \lambda x.\,\lfloor f \rfloor\,x is an eta redex. No rule fires on an empty application itself, and the redexes of ⌊u⌋\lfloor u \rfloor are the images of the redexes of uu, where an empty application contributes the redexes of its head. Hence a rule step u→u′u \to u' is a step ⌊u⌋→⌊u′⌋\lfloor u \rfloor \to \lfloor u' \rfloor of the same kind up to =α=_\alpha, and a term with no redex for the rules erases to a term with no redex. (3) By (2), ⌊t⌋→β∗⌊N⌋\lfloor t \rfloor \to_\beta^{*} \lfloor N \rfloor with ⌊N⌋\lfloor N \rfloor beta-normal, then ⌊N⌋→η∗⌊M⌋\lfloor N \rfloor \to_\eta^{*} \lfloor M \rfloor with ⌊M⌋\lfloor M \rfloor eta-normal. Eta steps keep a term beta-normal: a new beta redex would need (λx. f x) a(\lambda x.\,f\,x)\,a or λx. (λy. b) x\lambda x.\,(\lambda y.\,b)\,x in the term, both beta redexes already. So ⌊M⌋\lfloor M \rfloor is beta-eta normal and ⌊t⌋→βη∗⌊M⌋\lfloor t \rfloor \to_{\beta\eta}^{*} \lfloor M \rfloor. Likewise ⌊t⌋→βη∗⌊L⌋\lfloor t \rfloor \to_{\beta\eta}^{*} \lfloor L \rfloor with ⌊L⌋\lfloor L \rfloor beta-eta normal. By confluence of →βη\to_{\beta\eta} the two normal forms are alpha-equivalent. □\square

On the example, ⌊M⌋=λz. z\lfloor M \rfloor = \lambda z.\,z and ⌊L⌋=λy. y\lfloor L \rfloor = \lambda y.\,y. Equivalently, MM and LL are equal in the least congruence containing =α=_\alpha and Apply(h, []) =h= h; in particular their nameless, spine-flattened forms are equal.

The unary hypothesis is needed for eta only. On the curried reading, Bind(x, Apply(f, [a, Variable(x)])) is the eta redex λx. (f a) x\lambda x.\,(f\,a)\,x, which eta_rule does not recognise (see “Eta only on unary applications” above), so for terms with longer spines step (2) fails and no agreement is claimed.

Correctness and invariants

  • beta_rule(t) = Some(u) implies t→βut \to_\beta u at the root; eta_rule likewise for η\eta, with the side condition x∉FV(f)x \notin \mathrm{FV}(f) decided by a search for a free occurrence (above).
  • normalize returns NormalForm(u, n) only for uu with no (unary) β\beta or η\eta redex anywhere (rewrite normal-form lemma).
  • By confluence of beta, the beta normal forms computed by @eval.evaluate with beta_rule, by debruijn and by utlc/nbe coincide after conversion and spine flattening. The test “named and debruijn beta reduction agree modulo alpha” checks a step that requires renaming.
  • Every strategy of eval with beta_rule takes the same steps on a term and on its curried form, up to alpha-equivalence; the test “every strategy takes the same steps on a term and its curried form” checks this on 1000 random terms.
  • normalize returns a beta-eta normal form, which is in general not the beta normal form of the other normalizers: λx. f x\lambda x.\,f\,x is beta-normal and normalizes to ff here. The two are related by eta alone, but only modulo empty applications and for unary spines, as the theorem in “Beta then eta versus normalize” above states.
  • The rules and normalize use a constant host stack depth; terms nested 100 000 levels deep are handled on every backend (src/utlc/lambda/deep_test.mbt).
  • With beta and eta combined, normal order is used as the strategy; that it reaches the βη\beta\eta normal form of every normalizing term rests on the normalization theorem for beta and eta postponement, and is checked by tests rather than proved here.

Alternatives rejected

  • A separate lambda AST. Using Term[T] keeps the calculus inside the shared substrate: the same analyses, substitution and traces apply, and domain values ride along unchanged.
  • Separate beta and eta normalizers. Offered indirectly: pass beta_rule or eta_rule to @eval.evaluate with any strategy.
  • Iterated substitution in beta. Beta substitutes once; the argument is not re-substituted, as the calculus requires.

Boundaries

  • No types: ill-behaved terms such as Ω\Omega are accepted and end with StepLimitReached.
  • No sharing: a duplicated argument is reduced once per copy. Use utlc/nbe for efficient normalization.
  • Eta recognises unary applications only.
  • An empty application Apply(h, []) is read as hh when a redex is matched, but it is not removed from the term (see the eval design). Results of different reduction orders, such as beta then eta and normalize, therefore agree only after empty applications are erased.
  • Constants (Value) have no reduction rules here; add domain rules with rewrite or eval.

Footnotes

  1. Barendregt, The Lambda Calculus, Theorems 3.2.8 and 3.3.9. ↩

  2. Barendregt, The Lambda Calculus, §15.1. ↩