De Bruijn Indices and Binder Depth

Represent bound variables by the number of enclosing binders between an occurrence and its definition, then see why moving terms requires shifting indices.

Source
Anbloq
Language
English
Created
Updated
Chapters
2
Reading time
1 min
In this article2 chapters
  1. Count binders rather than choosing names
  2. Inserting a term can change the depth to its outer bindings

Count binders rather than choosing names

With index zero for the nearest binder, \(\lambda x.\lambda y.x\) is represented as \(\lambda.\lambda.1\), while \(\lambda x.\lambda y.y\) becomes \(\lambda.\lambda.0\). Renaming bound variables leaves these representations unchanged. That makes alpha-equivalence easy to test in this representation. The index depends on the path through enclosing binders, so instructions printing a name for a term still need the current context.

Inserting a term can change the depth to its outer bindings

If a term refers to an external binder by index zero and moves underneath a new lambda, its reference must become one. Otherwise it would point to the newly introduced binder. Shifting uses a cutoff so indices bound inside the moved term are preserved. Capture-avoiding substitution performs the same bookkeeping with fresh names. Lexical closures use environments to interpret the resulting positions at runtime.[1][2]

References

  1. Shriram Krishnamurthi, Programming Languages: Application and Interpretation ↩
  2. Robert Nystrom, Crafting Interpreters ↩