This note accompanies Start/Representation.lean. It explains, informally, how the three
syntactic representations of the untyped λ-calculus that appear in or around this development are
related, and exactly which statements in the Lean sources justify each link.
The three representations are:
- De Bruijn —
LambdainStart/Syntax.lean, the representation used everywhere in this library. A term isvar i,app s torlam u; every variable is a numeric index, and an indexiunderdbinders is free wheni ≥ d. - Locally nameless —
Cslib.LambdaCalculus.LocallyNameless.Untyped.Term Varfromcslib. Bound variables are de Bruijn indices (bvar i) but free variables are atoms (fvar a). A term with no danglingbvaris locally closed (LC); reduction is only defined on locally closed terms, and abstraction rules are quantified cofinitely over fresh atoms. - Binary λ-calculus (BLC) — Tromp's bit encoding,
Lambda.bitsandLambda.isBLCinStart/BLC.lean, wherelam u ↦ 00 · u,app s t ↦ 01 · s · tandvar i ↦ 1^{i+1} 0.
Start/BLC.lean proves that Lambda.bits is injective with decidable image, packaged as
Lambda.bitsEquiv : Lambda ≃ {bs : List Bool // isBLC bs = true}This leg is a plain bijection of syntax: BLC is a serialisation of the de Bruijn syntax tree, so nothing about substitution or reduction has to be checked — reduction on bit strings is defined by transport.
This is the substance of Start/Representation.lean, and the only genuinely non-trivial link,
because the two representations disagree about what a free variable is.
The usual way to translate is to carry an environment mapping each free index to a name. We avoid
that. Fix an ambient context of D binders and agree that the free index j (counted from the
innermost ambient binder outwards) denotes the atom D - 1 - j. That is: free indices are read
as de Bruijn levels rather than indices. Thus
toLN D d (var i) = if i < d then bvar i else fvar (D - 1 - (i - d))
toLN D d (app a b) = app (toLN D d a) (toLN D d b)
toLN D d (lam u) = abs (toLN D (d+1) u)where d counts the binders of the term itself, and ofLN inverts this. Two things make levels
the right choice:
- the translation of a term is stable under going under a binder — a level does not shift, so
no renaming is needed when
dincreases; and - the atom
Dis canonically fresh for every term read in a context of sizeD(Lambda.fv_toLN). This matters becausecslib's abstraction rules are of the form "for all atoms outside some finite setxs", and to use such a rule one must produce a fresh atom and then rename. Having a distinguished fresh atom makes the renaming a one-liner.
Lambda.ofLN_toLN and Lambda.toLN_ofLN prove the two directions inverse — the first for terms
with freeMax t ≤ d + D, the second for terms locally closed at d with all atoms < D.
Specialised to D = 0 this is a bijection of closed terms,
Lambda.closedEquiv : {t : Lambda // freeMax t = 0} ≃ {M : LNTerm // M.LC ∧ M.fv = ∅}The key computational lemma is
Lambda.toLN_subst_zero : toLN D 0 (Lambda.subst s 0 t) = (toLN D 1 t) ^ (toLN D 0 s)i.e. de Bruijn substitution of s for index 0 is exactly cslib's opening of the body with
the translation of s. It is proved from a general form Lambda.toLN_subst, which in turn needs
Lambda.toLN_lift: the de Bruijn lift is invisible on the locally nameless side, because levels
do not move when a binder is added. This is precisely the "substitution commutation" that makes
the whole bridge work, and it is where the level-based translation pays off — with an index-based
translation the lifting lemma would need a renaming of atoms.
- Forward (
Lambda.step_toLN): ift ⟶β t'thentoLN D 0 t ⭢βᶠ toLN D 0 t'. The β-case is the substitution lemma above plus the local-closure side conditions (Lambda.lc_toLN); thelamcase must produce acslibξ-derivation, which quantifies over cofinitely many atoms — we prove it for the canonical atomDand rename withTerm.FullBeta.redex_subst_cong. - Backward (
Lambda.reflect_step): iftoLN D 0 t ⭢βᶠ NthenN = toLN D 0 t'for somet'witht ⟶β t'. Here the induction is on the de Bruijn term, with inversion on thecslibderivation; under an abstraction one instantiates the cofinite quantifier at a fresh atom, renames it toD, applies the induction hypothesis in contextD+1, and recovers the body withTerm.open_injective.
Both extend to the reflexive-transitive closures (Lambda.reduces_toLN,
Lambda.reflect_reduces). Note that reflection is what makes this an equivalence rather than a
mere simulation: the image of the translation is closed under cslib reduction, so no cslib
term reachable from a translated term falls outside the correspondence.
Because the correspondence is an isomorphism of reduction systems, confluence transfers both ways, and we prove both transports:
Lambda.confluence_of_cslib : Lambda.Confluence -- from cslib's confluent_fullBeta
Lambda.cslib_confluence_of_lambda -- from our confluence_theoremThis library already proves Lambda.confluence_theorem directly, by the Tait–Martin-Löf parallel
reduction argument. cslib proves Term.confluent_fullBeta by its own route. The two proofs are
independent, and after the bridge each one implies the other, so the two formalizations check each
other on their headline theorem.
Composing the closed-term bijection with the BLC bijection gives
Lambda.closedBitsEquiv :
{M : LNTerm // M.LC ∧ M.fv = ∅} ≃ {bs : {bs // isBLC bs} // freeMax (bitsEquiv.symm bs) = 0}so the closed terms of the three representations are in bijection, and — by the reduction lemmas
above — that bijection carries β-reduction to β-reduction. Concretely this means the
Kolmogorov-complexity and randomness development of this library, which is stated over BLC bit
strings and de Bruijn terms, could equally have been stated over cslib's locally nameless terms.
- The bridge is for
Term ℕ(natural numbers as atoms);cslib's theory is generic in the atom type and the generic statement is not given here. - Only β-reduction is bridged: η, the typed calculi, and
cslib's other languages are untouched. cslib_confluence_of_lambdaassumes local closure and an explicit boundDon the atoms; the bound is not a real restriction (free-variable sets are finite) but it is carried in the statement rather than eliminated.