Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

Sections 1-2: Simply Typed λ-Calculus, Long β-Normal Forms, and Bound-Variable Standardization (`EMB`/`EXTRACT`)

Definition
BergerSchwichtenberg1991_Syntax

by kmehltretter82 · Oct 30, 2026 · Mathlib 0df444a (Lean v4.33.1)

completenesslambda-calculusnormalization-by-evaluationsemanticstype-theory

Formalizes Sections 1 and 2 of Berger and Schwichtenberg (LICS 1991): simple types over ground type o, typed variables (named and {0,1}*-indexed), capture-avoiding simultaneous substitution, α-equivalence, βη-equality, long β-normal and neutral forms (IsLongBetaNormal, IsLongBetaNeutral), the bound-variable standardization family EMB r k = α_k(r), and the canonical extraction operator EXTRACT.

Definition code
import Std

/-!
# Syntax, Long β-Normal Forms, and α-Standardization (Berger–Schwichtenberg 1991, Sections 1–2)

Source: Ulrich Berger and Helmut Schwichtenberg,
"An inverse of the evaluation functional for typed λ-calculus",
*Proceedings of the Sixth Annual IEEE Symposium on Logic in Computer Science (LICS 1991)*,
pp. 203–211, Sections 1–2 (pp. 204–205).

This module formalizes:
- Simple types (`Ty`) built from ground type `o` (`Ty.base`) by `→` (`Ty.arr`).
- Binary indices `k, l ∈ {0,1}*` (`Index`), indexed variables `x_k^ρ`, and general typed
  variables (`Var`).
- Concrete typed λ-terms (`Term`), typing (`HasType`), capture-avoiding substitution (`substTerm`),
  α-conversion (`AlphaEq`), βη-equality (`BetaEtaEq`), and long β-normal forms (`IsLongBetaNormal`,
  `IsLongBetaNeutral`).
- The bound-variable standardization mapping `α_k(r)` (`alphaRename`, p. 204), the term-family
  embedding `EMB : Λ → Λ^{{0,1}*}` (`EMB`, p. 204), and the extraction functional
  `EXTRACT : Λ^{{0,1}*} → Λ` (`EXTRACT`, pp. 204–205).
-/

namespace BergerSchwichtenberg1991

/-- Simple types built from a single ground type `o` by `→` (Section 1, p. 204):
`ρ, σ ::= o | ρ → σ`. -/
inductive Ty where
  | base : Ty
  | arr : Ty → Ty → Ty
  deriving DecidableEq, Repr

namespace Ty

/-- Iterated function type `σ₁ → ⋯ → σₖ → τ` associated to the right (p. 204). -/
def arrMany : List Ty → Ty → Ty
  | [], τ => τ
  | σ :: σs, τ => .arr σ (arrMany σs τ)

/-- Argument types `[σ₁, ..., σₖ]` of a type `ρ = σ₁ → ⋯ → σₖ → o`. -/
def argTypes : Ty → List Ty
  | .base => []
  | .arr σ τ => σ :: argTypes τ

end Ty

/-- Finite 0–1 sequence `k ∈ {0, 1}*` used as a binding position index (Section 2, p. 204). -/
abbrev Index := List Bool

/-- The empty 0–1 sequence `ε` (p. 205). -/
def epsIdx : Index := []

/-- The singleton sequence `0` (p. 205). -/
def zeroIdx : Index := [false]

/-- The singleton sequence `1` (p. 204). -/
def oneIdx : Index := [true]

/-- Concatenation `k * l` of 0–1 sequences (p. 204). -/
def appendIdx (k l : Index) : Index := k ++ l

/-- `IsPrefixOf k m` holds if `m = k * l` for some `l ∈ {0, 1}*` ("`m` extends `k`", p. 204). -/
def IsPrefixOf (k m : Index) : Prop :=
  ∃ l : Index, m = k ++ l

/-- Variable identifier: either an indexed variable `x_k` (`k ∈ {0,1}*`, Section 2, p. 204)
or an ordinary named variable `y_n` (`n : Nat`). -/
inductive VarName where
  | indexed : Index → VarName
  | named : Nat → VarName
  deriving DecidableEq, Repr

/-- Typed variable `x^ρ` (Section 1, p. 204). -/
structure Var where
  name : VarName
  ty : Ty
  deriving DecidableEq, Repr

/-- Indexed variable `x_k^ρ` of type `ρ` with index `k ∈ {0, 1}*` (Section 2, p. 204). -/
def indexedVar (ρ : Ty) (k : Index) : Var :=
  ⟨.indexed k, ρ⟩

/-- Concrete typed λ-terms (Section 1, p. 204):
constructed from typed variables `x^ρ` by application `(t^{ρ→σ} r^ρ)^σ` and abstraction
`(λx^ρ. s^σ)^{ρ→σ}`. -/
inductive Term where
  | var : Var → Term
  | app : Term → Term → Term
  | lam : Var → Term → Term
  deriving DecidableEq, Repr

namespace Term

/-- Left-associated iterated application `r s₁ ... sₖ` (Section 1, p. 204). -/
def appMany : Term → List Term → Term
  | r, [] => r
  | r, s :: ss => appMany (.app r s) ss

/-- Iterated lambda abstraction `λx₁ ... xₙ. r`. -/
def lamMany : List Var → Term → Term
  | [], r => r
  | x :: xs, r => .lam x (lamMany xs r)

end Term

/-- Typing relation `r ∈ Λ^ρ` (`HasType r ρ`, Section 1, p. 204). -/
inductive HasType : Term → Ty → Prop where
  | var (x : Var) :
      HasType (.var x) x.ty
  | app {t r : Term} {ρ σ : Ty} :
      HasType t (.arr ρ σ) →
      HasType r ρ →
      HasType (.app t r) σ
  | lam (x : Var) {s : Term} {σ : Ty} :
      HasType s σ →
      HasType (.lam x s) (.arr x.ty σ)

/-- Free variable occurrences of a term `r`. -/
def freeVarOccurrences : Term → List Var
  | .var x => [x]
  | .app r s => freeVarOccurrences r ++ freeVarOccurrences s
  | .lam x r => (freeVarOccurrences r).filter (· != x)

/-- Set of free variables `FV(r)` (duplicate-free list). -/
def freeVars (r : Term) : List Var :=
  (freeVarOccurrences r).eraseDups

/-- A term `r` is closed if `FV(r) = ∅`. -/
def IsClosed (r : Term) : Prop :=
  freeVars r = []

/-- List of indices `l ∈ {0, 1}*` such that `x_l^σ` occurs free in `r` for some `σ`
(Section 2, p. 204: `{l ∈ {0,1}* | x_l free in r}`). -/
def freeIndices (r : Term) : List Index :=
  (freeVars r).filterMap (fun v =>
    match v.name with
    | .indexed l => some l
    | .named _ => none)

/-- All variables (both free and bound) occurring in `r`. -/
def allVars : Term → List Var
  | .var x => [x]
  | .app r s => allVars r ++ allVars s
  | .lam x r => x :: allVars r

/-- Maximum named-variable id occurring anywhere in `r`. -/
def maxNamedId : Term → Nat
  | .var ⟨.named n, _⟩ => n
  | .var ⟨.indexed _, _⟩ => 0
  | .app r s => Nat.max (maxNamedId r) (maxNamedId s)
  | .lam ⟨.named n, _⟩ r => Nat.max n (maxNamedId r)
  | .lam ⟨.indexed _, _⟩ r => maxNamedId r

/-- Maximum named-variable id across a list of terms. -/
def maxNamedIdList : List Term → Nat
  | [] => 0
  | t :: ts => Nat.max (maxNamedId t) (maxNamedIdList ts)

/-- Replace free occurrences of variable `x` with variable `y` in `r`. -/
def renameFreeVar (r : Term) (x y : Var) : Term :=
  match r with
  | .var v => if v == x then .var y else .var v
  | .app t s => .app (renameFreeVar t x y) (renameFreeVar s x y)
  | .lam v body =>
      if v == x then .lam v body else .lam v (renameFreeVar body x y)

/-- Structural size of a raw term `r`. -/
def rawSize : Term → Nat
  | .var _ => 1
  | .app r s => rawSize r + rawSize s + 1
  | .lam _ r => rawSize r + 1

theorem renameFreeVar_rawSize (r : Term) (x y : Var) :
    rawSize (renameFreeVar r x y) = rawSize r := by
  induction r with
  | var v => simp only [renameFreeVar]; split <;> rfl
  | app t s iht ihs => simp [renameFreeVar, rawSize, iht, ihs]
  | lam v body ih =>
      simp only [renameFreeVar]
      split
      · rfl
      · simp [rawSize, ih]

/-- Capture-avoiding substitution `r[s/x]` (Section 2, p. 204). -/
def substTerm (r : Term) (x : Var) (s : Term) : Term :=
  match r with
  | .var v => if v == x then s else .var v
  | .app t u => .app (substTerm t x s) (substTerm u x s)
  | .lam v body =>
      if v == x then
        .lam v body
      else if v ∈ freeVarOccurrences s then
        let freshId := Nat.max (maxNamedId body) (Nat.max (maxNamedId s) (maxNamedId (.var x))) + 1
        let v' : Var := ⟨.named freshId, v.ty⟩
        .lam v' (substTerm (renameFreeVar body v v') x s)
      else
        .lam v (substTerm body x s)
termination_by rawSize r
decreasing_by
  all_goals
    simp_wf
    simp [rawSize, renameFreeVar_rawSize]
    try omega

/-- Type-respecting simultaneous substitution `θ : Var → Term` (Section 3, p. 205 & Section 4, p. 207). -/
abbrev Substitution := Var → Term

/-- A substitution `θ` is well-typed (`IsWellTypedSubst θ`) if `θ(x^ρ) ∈ Λ^ρ` for every variable `x^ρ`. -/
def IsWellTypedSubst (θ : Substitution) : Prop :=
  ∀ x : Var, HasType (θ x) x.ty

/-- Identity substitution `θ_id(x) = x` (Section 4, p. 208). -/
def idSubst : Substitution := fun x => .var x

/-- Update a substitution `θ[x ↦ t]` (Section 4, p. 207). -/
def updateSubst (θ : Substitution) (x : Var) (t : Term) : Substitution :=
  fun y => if y == x then t else θ y

/-- Apply a simultaneous substitution `rθ` (with capture avoidance via fresh named variables). -/
def applySubst (θ : Substitution) (r : Term) : Term :=
  match r with
  | .var x => θ x
  | .app t u => .app (applySubst θ t) (applySubst θ u)
  | .lam v body =>
      let fvBody := (freeVars body).filter (· != v)
      let imgTerms := fvBody.map θ
      let imgFVs := imgTerms.foldr (fun t acc => freeVarOccurrences t ++ acc) []
      if v ∈ imgFVs then
        let freshId :=
          Nat.max (maxNamedId body)
            (Nat.max (maxNamedId (.var v)) (maxNamedIdList imgTerms)) + 1
        let v' : Var := ⟨.named freshId, v.ty⟩
        .lam v' (applySubst (updateSubst θ v (.var v')) body)
      else
        .lam v (applySubst (updateSubst θ v (.var v)) body)
termination_by rawSize r
decreasing_by
  all_goals
    simp_wf
    simp [rawSize]
    try omega

/-- α-equivalence `r =_α s` (Section 2, p. 204: terms interconvertible by bound variable renaming). -/
inductive AlphaEq : Term → Term → Prop where
  | refl (r : Term) : AlphaEq r r
  | symm {r s : Term} : AlphaEq r s → AlphaEq s r
  | trans {r s t : Term} : AlphaEq r s → AlphaEq s t → AlphaEq r t
  | app {r r' s s' : Term} :
      AlphaEq r r' → AlphaEq s s' → AlphaEq (.app r s) (.app r' s')
  | lam_rename (x y : Var) (r : Term) :
      x.ty = y.ty →
      (y = x ∨ y ∉ allVars r) →
      AlphaEq (.lam x r) (.lam y (renameFreeVar r x y))
  | lam_congr (x : Var) {r s : Term} :
      AlphaEq r s → AlphaEq (.lam x r) (.lam x s)

/-- Provable equality `r =_{βη} s` in the typed βη-calculus (Sections 1–2, pp. 203–204). -/
inductive BetaEtaEq : Term → Term → Prop where
  | of_alpha {r s : Term} : AlphaEq r s → BetaEtaEq r s
  | refl (r : Term) : BetaEtaEq r r
  | symm {r s : Term} : BetaEtaEq r s → BetaEtaEq s r
  | trans {r s t : Term} : BetaEtaEq r s → BetaEtaEq s t → BetaEtaEq r t
  | app {r r' s s' : Term} :
      BetaEtaEq r r' → BetaEtaEq s s' → BetaEtaEq (.app r s) (.app r' s')
  | lam (x : Var) {r s : Term} :
      BetaEtaEq r s → BetaEtaEq (.lam x r) (.lam x s)
  | beta (x : Var) (r s : Term) :
      BetaEtaEq (.app (.lam x r) s) (substTerm r x s)
  | eta (x : Var) (r : Term) :
      x ∉ freeVars r →
      BetaEtaEq (.lam x (.app r (.var x))) r

mutual
  /-- Long β-normal forms (Section 2, p. 204):
  `λx₁^{ρ₁} ... λxₙ^{ρₙ}. y^{σ₁ → ⋯ → σₖ → o} s₁^{σ₁} ... sₖ^{σₖ}` with `s₁, ..., sₖ` in
  long β-normal form and body `y s₁ ... sₖ` of ground type `o`. -/
  inductive IsLongBetaNormal : Ty → Term → Prop where
    | baseNeutral {t : Term} :
        IsLongBetaNeutral .base t →
        IsLongBetaNormal .base t
    | lam (x : Var) {σ : Ty} {r : Term} :
        IsLongBetaNormal σ r →
        IsLongBetaNormal (.arr x.ty σ) (.lam x r)

  /-- Neutral spine `y s₁ ... sₖ` whose arguments are in long β-normal form. -/
  inductive IsLongBetaNeutral : Ty → Term → Prop where
    | var (y : Var) :
        IsLongBetaNeutral y.ty (.var y)
    | app {t s : Term} {σ τ : Ty} :
        IsLongBetaNeutral (.arr σ τ) t →
        IsLongBetaNormal σ s →
        IsLongBetaNeutral τ (.app t s)
end

/-- Section 2 (p. 204): Bound-variable renaming functional `α_k(r)` for `k ∈ {0, 1}*`:
- `α_k(x) = x`
- `α_k(r s) = (α_{k * 0} r)(α_{k * 1} s)`
- `α_k(λy^ρ. r) = λx_k^ρ. (α_{k * 0} r)[x_k^ρ / y^ρ]` (standardizing inner bound variables
  to indices extending `k * 0` first so that renaming `y^ρ ↦ x_k^ρ` is capture-free). -/
def alphaRename (k : Index) (r : Term) : Term :=
  match r with
  | .var x => .var x
  | .app t s =>
      .app (alphaRename (appendIdx k zeroIdx) t) (alphaRename (appendIdx k oneIdx) s)
  | .lam y body =>
      let xk := indexedVar y.ty k
      let yFresh : Var := ⟨.named (maxNamedId body + 1), y.ty⟩
      let body' := alphaRename (appendIdx k zeroIdx) (renameFreeVar body y yFresh)
      .lam xk (renameFreeVar body' yFresh xk)
termination_by rawSize r
decreasing_by
  all_goals
    simp_wf
    simp [rawSize, renameFreeVar_rawSize]
    try omega

/-- Section 2 (p. 204): Term family representation `EMB : Λ → (Index → Λ)` defined by
`EMB r k := α_k(r)`. -/
def EMB (r : Term) (k : Index) : Term :=
  alphaRename k r

/-- Maximum length among a list of 0–1 sequences. -/
def maxIdxLength : List Index → Nat
  | [] => 0
  | l :: ls => Nat.max l.length (maxIdxLength ls)

/-- Canonical fresh index `k̂(F) ∈ {0, 1}*` computed from `F(ε)` and `F(0)` (pp. 204–205):
a sequence of `0`s strictly longer than every index occurring in `F(ε)` or `F(0)`, so that
no index in `{l ∈ {0,1}* | x_l free in r}` extends `k̂`. -/
def canonicalFreshIndex (F : Index → Term) : Index :=
  let len := Nat.max (maxIdxLength (freeIndices (F epsIdx)))
                     (maxIdxLength (freeIndices (F zeroIdx))) + 1
  List.replicate len false

/-- Section 2 (p. 205): Extraction functional `EXTRACT : Λ^{{0,1}*} → Λ` defined by
`EXTRACT(F) := F(k̂)`. -/
def EXTRACT (F : Index → Term) : Term :=
  F (canonicalFreshIndex F)

end BergerSchwichtenberg1991
Source
Ulrich Berger and Helmut Schwichtenberg, 'An inverse of the evaluation functional for typed λ-calculus', Proceedings of the Sixth Annual IEEE Symposium on Logic in Computer Science (LICS 1991), pp. 203-211 (doi:10.1109/LICS.1991.151645). Sections 1-2, pp. 203-205.
Read-back

What the Lean code literally says, in plain math · Gemini 3.1 Pro (Independent Blind Auditor)

Read-back: Def_BergerSchwichtenberg1991_Syntax.lean

Ty — Defines an inductive type of simple types built from a single ground type ooo and a binary arrow constructor (ρ,σ)↦(ρ→σ)(\rho, \sigma) \mapsto (\rho \to \sigma)(ρ,σ)↦(ρ→σ), equipped with decidable equality.

Ty.arrMany — Given a finite list of types [σ1,…,σk][\sigma_1, \dots, \sigma_k][σ1​,…,σk​] and a return type τ\tauτ, defines the right-associated iterated function type σ1→(σ2→⋯→(σk→τ)⋯ )\sigma_1 \to (\sigma_2 \to \cdots \to (\sigma_k \to \tau)\cdots)σ1​→(σ2​→⋯→(σk​→τ)⋯) by structural recursion on the list, returning τ\tauτ when the list is empty.

Ty.argTypes — Given a type ρ\rhoρ, extracts its list of domain types by structural recursion: returns the empty list [][][] when ρ=o\rho = oρ=o, and returns σ::argTypes(τ)\sigma :: \mathrm{argTypes}(\tau)σ::argTypes(τ) when ρ=(σ→τ)\rho = (\sigma \to \tau)ρ=(σ→τ).

Index, epsIdx, zeroIdx, oneIdx, appendIdx, and IsPrefixOf — Defines an index (Index\mathrm{Index}Index) as a finite list of booleans {false,true}∗\{\mathrm{false}, \mathrm{true}\}^*{false,true}∗. The distinguished constant ε\varepsilonε (epsIdx) is the empty list [][][], 000 (zeroIdx) is the singleton list [false][\mathrm{false}][false], and 111 (oneIdx) is the singleton list [true][\mathrm{true}][true]. For any two indices k,l∈{false,true}∗k, l \in \{\mathrm{false}, \mathrm{true}\}^*k,l∈{false,true}∗, k∗lk * lk∗l (appendIdx) is their list concatenation k++lk \mathbin{++} lk++l. For any two indices k,m∈{false,true}∗k, m \in \{\mathrm{false}, \mathrm{true}\}^*k,m∈{false,true}∗, the relation IsPrefixOf(k,m)\mathrm{IsPrefixOf}(k, m)IsPrefixOf(k,m) holds if and only if there exists an index l∈{false,true}∗l \in \{\mathrm{false}, \mathrm{true}\}^*l∈{false,true}∗ such that m=k++lm = k \mathbin{++} lm=k++l.

VarName, Var, and indexedVar — Defines a variable identifier (VarName\mathrm{VarName}VarName) as an inductive type with two disjoint constructors: an indexed identifier indexed(k)\mathrm{indexed}(k)indexed(k) for k∈{false,true}∗k \in \{\mathrm{false}, \mathrm{true}\}^*k∈{false,true}∗, and a named identifier named(n)\mathrm{named}(n)named(n) for n∈Nn \in \mathbb{N}n∈N. A typed variable (Var\mathrm{Var}Var) is a pair x=(name,ty)x = (\mathrm{name}, \mathrm{ty})x=(name,ty) consisting of an identifier name∈VarName\mathrm{name} \in \mathrm{VarName}name∈VarName and a simple type ty∈Ty\mathrm{ty} \in \mathrm{Ty}ty∈Ty (with decidable equality comparing both components, so variables with the same identifier but distinct types are distinct elements of Var\mathrm{Var}Var). For any type ρ∈Ty\rho \in \mathrm{Ty}ρ∈Ty and index k∈{false,true}∗k \in \{\mathrm{false}, \mathrm{true}\}^*k∈{false,true}∗, xkρx_k^\rhoxkρ​ (indexedVar) is the typed variable (indexed(k),ρ)(\mathrm{indexed}(k), \rho)(indexed(k),ρ).

Term, Term.appMany, and Term.lamMany — Defines raw λ\lambdaλ-terms (Term\mathrm{Term}Term) inductively with three constructors: a variable term var(x)\mathrm{var}(x)var(x) for x∈Varx \in \mathrm{Var}x∈Var, an application app(t,r)\mathrm{app}(t, r)app(t,r) for t,r∈Termt, r \in \mathrm{Term}t,r∈Term, and a λ\lambdaλ-abstraction lam(x,r)\mathrm{lam}(x, r)lam(x,r) for x∈Varx \in \mathrm{Var}x∈Var and r∈Termr \in \mathrm{Term}r∈Term. Given a head term rrr and a list of argument terms [s1,…,sk][s_1, \dots, s_k][s1​,…,sk​], appMany(r,[s1,…,sk])\mathrm{appMany}(r, [s_1, \dots, s_k])appMany(r,[s1​,…,sk​]) forms the left-associated iterated application (⋯((r s1) s2)⋯sk)(\cdots ((r\ s_1)\ s_2)\cdots s_k)(⋯((r s1​) s2​)⋯sk​) (returning rrr when the list is empty). Given a list of typed variables [x1,…,xn][x_1, \dots, x_n][x1​,…,xn​] and a body rrr, lamMany([x1,…,xn],r)\mathrm{lamMany}([x_1, \dots, x_n], r)lamMany([x1​,…,xn​],r) forms the right-nested iterated abstraction λx1. …λxn. r\lambda x_1.\ \dots \lambda x_n.\ rλx1​. …λxn​. r (returning rrr when the list is empty).

HasType — Defines an inductive typing relation r:ρr : \rhor:ρ (HasType(r,ρ)\mathrm{HasType}(r, \rho)HasType(r,ρ)) between a raw term r∈Termr \in \mathrm{Term}r∈Term and a simple type ρ∈Ty\rho \in \mathrm{Ty}ρ∈Ty without an external typing context: (1) for every typed variable x∈Varx \in \mathrm{Var}x∈Var, var(x)\mathrm{var}(x)var(x) has type x.tyx.\mathrm{ty}x.ty; (2) for all terms t,r∈Termt, r \in \mathrm{Term}t,r∈Term and types ρ,σ∈Ty\rho, \sigma \in \mathrm{Ty}ρ,σ∈Ty, if t:(ρ→σ)t : (\rho \to \sigma)t:(ρ→σ) and r:ρr : \rhor:ρ, then app(t,r):σ\mathrm{app}(t, r) : \sigmaapp(t,r):σ; and (3) for every typed variable x∈Varx \in \mathrm{Var}x∈Var, term s∈Terms \in \mathrm{Term}s∈Term, and type σ∈Ty\sigma \in \mathrm{Ty}σ∈Ty, if s:σs : \sigmas:σ, then lam(x,s):(x.ty→σ)\mathrm{lam}(x, s) : (x.\mathrm{ty} \to \sigma)lam(x,s):(x.ty→σ). Because each variable occurrence carries its own intrinsic type, two free variables in a well-typed term may share the same VarName\mathrm{VarName}VarName while having different types.

freeVarOccurrences, freeVars, IsClosed, freeIndices, and allVars — For a raw term r∈Termr \in \mathrm{Term}r∈Term, freeVarOccurrences(r)\mathrm{freeVarOccurrences}(r)freeVarOccurrences(r) computes the list of free variable occurrences by structural recursion: [x][x][x] for var(x)\mathrm{var}(x)var(x), list concatenation freeVarOccurrences(t)++freeVarOccurrences(s)\mathrm{freeVarOccurrences}(t) \mathbin{++} \mathrm{freeVarOccurrences}(s)freeVarOccurrences(t)++freeVarOccurrences(s) for app(t,s)\mathrm{app}(t, s)app(t,s), and freeVarOccurrences(s)\mathrm{freeVarOccurrences}(s)freeVarOccurrences(s) filtered to remove all occurrences equal to xxx (in both name\mathrm{name}name and ty\mathrm{ty}ty) for lam(x,s)\mathrm{lam}(x, s)lam(x,s). The list freeVars(r)\mathrm{freeVars}(r)freeVars(r) removes duplicate variables from freeVarOccurrences(r)\mathrm{freeVarOccurrences}(r)freeVarOccurrences(r). The predicate IsClosed(r)\mathrm{IsClosed}(r)IsClosed(r) holds if and only if freeVars(r)=[]\mathrm{freeVars}(r) = []freeVars(r)=[]. The list freeIndices(r)\mathrm{freeIndices}(r)freeIndices(r) extracts every index l∈{false,true}∗l \in \{\mathrm{false}, \mathrm{true}\}^*l∈{false,true}∗ such that (indexed(l),σ)∈freeVars(r)(\mathrm{indexed}(l), \sigma) \in \mathrm{freeVars}(r)(indexed(l),σ)∈freeVars(r) for some type σ\sigmaσ, ignoring all named(n)\mathrm{named}(n)named(n) variables. The list allVars(r)\mathrm{allVars}(r)allVars(r) collects all variables (both free occurrences and λ\lambdaλ-binders) occurring anywhere in rrr: [x][x][x] for var(x)\mathrm{var}(x)var(x), allVars(t)++allVars(s)\mathrm{allVars}(t) \mathbin{++} \mathrm{allVars}(s)allVars(t)++allVars(s) for app(t,s)\mathrm{app}(t, s)app(t,s), and x::allVars(s)x :: \mathrm{allVars}(s)x::allVars(s) for lam(x,s)\mathrm{lam}(x, s)lam(x,s).

maxNamedId, maxNamedIdList, renameFreeVar, rawSize, renameFreeVar_rawSize, and substTerm — For a raw term r∈Termr \in \mathrm{Term}r∈Term, maxNamedId(r)∈N\mathrm{maxNamedId}(r) \in \mathbb{N}maxNamedId(r)∈N computes the maximum natural number nnn occurring in any named(n)\mathrm{named}(n)named(n) variable (whether free or bound as a λ\lambdaλ-binder) anywhere in rrr, returning 000 if only indexed variables occur; maxNamedIdList(ts)\mathrm{maxNamedIdList}(ts)maxNamedIdList(ts) returns the maximum of maxNamedId(t)\mathrm{maxNamedId}(t)maxNamedId(t) across a list of terms tststs (000 for [][][]). For r∈Termr \in \mathrm{Term}r∈Term and x,y∈Varx, y \in \mathrm{Var}x,y∈Var, renameFreeVar(r,x,y)\mathrm{renameFreeVar}(r, x, y)renameFreeVar(r,x,y) replaces every free occurrence of xxx in rrr with yyy, stopping under lam(v,body)\mathrm{lam}(v, \mathrm{body})lam(v,body) when v=xv = xv=x, without checking whether yyy is captured by an inner binder lam(y,… )\mathrm{lam}(y, \dots)lam(y,…) in rrr. The function rawSize(r)∈N\mathrm{rawSize}(r) \in \mathbb{N}rawSize(r)∈N counts the number of AST nodes in rrr, and renameFreeVar_rawSize states that rawSize(renameFreeVar(r,x,y))=rawSize(r)\mathrm{rawSize}(\mathrm{renameFreeVar}(r, x, y)) = \mathrm{rawSize}(r)rawSize(renameFreeVar(r,x,y))=rawSize(r). For r,s∈Termr, s \in \mathrm{Term}r,s∈Term and x∈Varx \in \mathrm{Var}x∈Var, single-variable substitution substTerm(r,x,s)\mathrm{substTerm}(r, x, s)substTerm(r,x,s) replaces free occurrences of xxx in rrr by sss: on var(v)\mathrm{var}(v)var(v) it returns sss if v=xv = xv=x and var(v)\mathrm{var}(v)var(v) otherwise; on app(t,u)\mathrm{app}(t, u)app(t,u) it recurses on both subterms; and on lam(v,body)\mathrm{lam}(v, \mathrm{body})lam(v,body) it returns lam(v,body)\mathrm{lam}(v, \mathrm{body})lam(v,body) if v=xv = xv=x, else if v∈freeVarOccurrences(s)v \in \mathrm{freeVarOccurrences}(s)v∈freeVarOccurrences(s) it sets

m=max⁡(maxNamedId(body), max⁡(maxNamedId(s),maxNamedId(var(x))))+1,v′=(named(m),v.ty),m = \max\bigl(\mathrm{maxNamedId}(\mathrm{body}),\, \max(\mathrm{maxNamedId}(s), \mathrm{maxNamedId}(\mathrm{var}(x)))\bigr) + 1, \qquad v' = (\mathrm{named}(m), v.\mathrm{ty}),m=max(maxNamedId(body),max(maxNamedId(s),maxNamedId(var(x))))+1,v′=(named(m),v.ty),

and returns lam(v′,substTerm(renameFreeVar(body,v,v′),x,s))\mathrm{lam}(v', \mathrm{substTerm}(\mathrm{renameFreeVar}(\mathrm{body}, v, v'), x, s))lam(v′,substTerm(renameFreeVar(body,v,v′),x,s)), and otherwise returns lam(v,substTerm(body,x,s))\mathrm{lam}(v, \mathrm{substTerm}(\mathrm{body}, x, s))lam(v,substTerm(body,x,s)). Note that when v≠xv \neq xv=x and v∈freeVarOccurrences(s)v \in \mathrm{freeVarOccurrences}(s)v∈freeVarOccurrences(s), vvv is renamed to v′v'v′ even if x∉freeVars(body)x \notin \mathrm{freeVars}(\mathrm{body})x∈/freeVars(body).

Substitution, IsWellTypedSubst, idSubst, updateSubst, and applySubst — Defines a substitution (Substitution\mathrm{Substitution}Substitution) as a total function θ:Var→Term\theta : \mathrm{Var} \to \mathrm{Term}θ:Var→Term. The predicate IsWellTypedSubst(θ)\mathrm{IsWellTypedSubst}(\theta)IsWellTypedSubst(θ) holds if and only if ∀x∈Var,  HasType(θ(x),x.ty)\forall x \in \mathrm{Var},\; \mathrm{HasType}(\theta(x), x.\mathrm{ty})∀x∈Var,HasType(θ(x),x.ty). The identity substitution idSubst\mathrm{idSubst}idSubst maps each x∈Varx \in \mathrm{Var}x∈Var to var(x)\mathrm{var}(x)var(x), and updateSubst(θ,x,t)\mathrm{updateSubst}(\theta, x, t)updateSubst(θ,x,t) maps yyy to ttt if y=xy = xy=x and to θ(y)\theta(y)θ(y) otherwise. For θ:Var→Term\theta : \mathrm{Var} \to \mathrm{Term}θ:Var→Term and r∈Termr \in \mathrm{Term}r∈Term, simultaneous capture-avoiding substitution applySubst(θ,r)\mathrm{applySubst}(\theta, r)applySubst(θ,r) is defined by structural recursion on rrr:

  • applySubst(θ,var(x))=θ(x)\mathrm{applySubst}(\theta, \mathrm{var}(x)) = \theta(x)applySubst(θ,var(x))=θ(x);
  • applySubst(θ,app(t,u))=app(applySubst(θ,t),applySubst(θ,u))\mathrm{applySubst}(\theta, \mathrm{app}(t, u)) = \mathrm{app}(\mathrm{applySubst}(\theta, t), \mathrm{applySubst}(\theta, u))applySubst(θ,app(t,u))=app(applySubst(θ,t),applySubst(θ,u));
  • for lam(v,body)\mathrm{lam}(v, \mathrm{body})lam(v,body), let fvBody={u∈freeVars(body)∣u≠v}\mathrm{fvBody} = \{u \in \mathrm{freeVars}(\mathrm{body}) \mid u \neq v\}fvBody={u∈freeVars(body)∣u=v}, imgTerms=[θ(u)∣u∈fvBody]\mathrm{imgTerms} = [\theta(u) \mid u \in \mathrm{fvBody}]imgTerms=[θ(u)∣u∈fvBody], and imgFVs\mathrm{imgFVs}imgFVs be the concatenated list of freeVarOccurrences(t)\mathrm{freeVarOccurrences}(t)freeVarOccurrences(t) for t∈imgTermst \in \mathrm{imgTerms}t∈imgTerms:
    • if v∈imgFVsv \in \mathrm{imgFVs}v∈imgFVs, it sets m=max⁡(maxNamedId(body),max⁡(maxNamedId(var(v)),maxNamedIdList(imgTerms)))+1m = \max(\mathrm{maxNamedId}(\mathrm{body}), \max(\mathrm{maxNamedId}(\mathrm{var}(v)), \mathrm{maxNamedIdList}(\mathrm{imgTerms}))) + 1m=max(maxNamedId(body),max(maxNamedId(var(v)),maxNamedIdList(imgTerms)))+1 and v′=(named(m),v.ty)v' = (\mathrm{named}(m), v.\mathrm{ty})v′=(named(m),v.ty), and returns
lam(v′, applySubst(updateSubst(θ,v,var(v′)), body));\mathrm{lam}\bigl(v',\, \mathrm{applySubst}(\mathrm{updateSubst}(\theta, v, \mathrm{var}(v')),\, \mathrm{body})\bigr);lam(v′,applySubst(updateSubst(θ,v,var(v′)),body));
  • otherwise, it returns lam(v,applySubst(updateSubst(θ,v,var(v)),body))\mathrm{lam}(v, \mathrm{applySubst}(\mathrm{updateSubst}(\theta, v, \mathrm{var}(v)), \mathrm{body}))lam(v,applySubst(updateSubst(θ,v,var(v)),body)).

AlphaEq — Defines α\alphaα-equivalence r=αsr =_\alpha sr=α​s (AlphaEq(r,s)\mathrm{AlphaEq}(r, s)AlphaEq(r,s)) as the smallest equivalence relation on Term\mathrm{Term}Term closed under application congruence (app\mathrm{app}app), λ\lambdaλ-body congruence (lam_congr\mathrm{lam\_congr}lam_congr), and the renaming rule (lam_rename\mathrm{lam\_rename}lam_rename): for any variables x,y∈Varx, y \in \mathrm{Var}x,y∈Var and raw term r∈Termr \in \mathrm{Term}r∈Term, if x.ty=y.tyx.\mathrm{ty} = y.\mathrm{ty}x.ty=y.ty and either y=xy = xy=x or y∉allVars(r)y \notin \mathrm{allVars}(r)y∈/allVars(r), then

lam(x,r)=αlam(y,renameFreeVar(r,x,y)).\mathrm{lam}(x, r) =_\alpha \mathrm{lam}(y, \mathrm{renameFreeVar}(r, x, y)).lam(x,r)=α​lam(y,renameFreeVar(r,x,y)).

BetaEtaEq — Defines βη\beta\etaβη-equality r=βηsr =_{\beta\eta} sr=βη​s (BetaEtaEq(r,s)\mathrm{BetaEtaEq}(r, s)BetaEtaEq(r,s)) on raw terms r,s∈Termr, s \in \mathrm{Term}r,s∈Term as the smallest equivalence relation containing AlphaEq\mathrm{AlphaEq}AlphaEq, closed under application congruence (app\mathrm{app}app) and λ\lambdaλ-abstraction congruence (lam\mathrm{lam}lam), and satisfying: (1) for all x∈Varx \in \mathrm{Var}x∈Var and r,s∈Termr, s \in \mathrm{Term}r,s∈Term, app(lam(x,r),s)=βηsubstTerm(r,x,s)\mathrm{app}(\mathrm{lam}(x, r), s) =_{\beta\eta} \mathrm{substTerm}(r, x, s)app(lam(x,r),s)=βη​substTerm(r,x,s) (beta\mathrm{beta}beta); and (2) for all x∈Varx \in \mathrm{Var}x∈Var and r∈Termr \in \mathrm{Term}r∈Term with x∉freeVars(r)x \notin \mathrm{freeVars}(r)x∈/freeVars(r), lam(x,app(r,var(x)))=βηr\mathrm{lam}(x, \mathrm{app}(r, \mathrm{var}(x))) =_{\beta\eta} rlam(x,app(r,var(x)))=βη​r (eta\mathrm{eta}eta). Neither beta\mathrm{beta}beta nor eta\mathrm{eta}eta imposes a typing hypothesis on rrr or sss.

IsLongBetaNormal and IsLongBetaNeutral — Defines two mutually inductive predicates relating a type ρ∈Ty\rho \in \mathrm{Ty}ρ∈Ty and a raw term t∈Termt \in \mathrm{Term}t∈Term:

  • IsLongBetaNormal(ρ,t)\mathrm{IsLongBetaNormal}(\rho, t)IsLongBetaNormal(ρ,t) holds either when ρ=o\rho = oρ=o and IsLongBetaNeutral(o,t)\mathrm{IsLongBetaNeutral}(o, t)IsLongBetaNeutral(o,t) (baseNeutral\mathrm{baseNeutral}baseNeutral), or when t=lam(x,r)t = \mathrm{lam}(x, r)t=lam(x,r) and ρ=(x.ty→σ)\rho = (x.\mathrm{ty} \to \sigma)ρ=(x.ty→σ) for some x∈Varx \in \mathrm{Var}x∈Var, σ∈Ty\sigma \in \mathrm{Ty}σ∈Ty, and r∈Termr \in \mathrm{Term}r∈Term satisfying IsLongBetaNormal(σ,r)\mathrm{IsLongBetaNormal}(\sigma, r)IsLongBetaNormal(σ,r) (lam\mathrm{lam}lam).
  • IsLongBetaNeutral(τ,t)\mathrm{IsLongBetaNeutral}(\tau, t)IsLongBetaNeutral(τ,t) holds either when t=var(y)t = \mathrm{var}(y)t=var(y) and τ=y.ty\tau = y.\mathrm{ty}τ=y.ty for some y∈Vary \in \mathrm{Var}y∈Var (var\mathrm{var}var), or when t=app(u,s)t = \mathrm{app}(u, s)t=app(u,s) for some u,s∈Termu, s \in \mathrm{Term}u,s∈Term and σ∈Ty\sigma \in \mathrm{Ty}σ∈Ty such that IsLongBetaNeutral(σ→τ,u)\mathrm{IsLongBetaNeutral}(\sigma \to \tau, u)IsLongBetaNeutral(σ→τ,u) and IsLongBetaNormal(σ,s)\mathrm{IsLongBetaNormal}(\sigma, s)IsLongBetaNormal(σ,s) (app\mathrm{app}app).

alphaRename, EMB, maxIdxLength, canonicalFreshIndex, and EXTRACT — For an index k∈{false,true}∗k \in \{\mathrm{false}, \mathrm{true}\}^*k∈{false,true}∗ and a raw term r∈Termr \in \mathrm{Term}r∈Term, αk(r)\alpha_k(r)αk​(r) (alphaRename(k,r)\mathrm{alphaRename}(k, r)alphaRename(k,r)) is defined by structural recursion on rawSize(r)\mathrm{rawSize}(r)rawSize(r):

αk(var(x))=var(x),αk(app(t,s))=app(αk∗0(t),αk∗1(s)),\alpha_k(\mathrm{var}(x)) = \mathrm{var}(x), \qquad \alpha_k(\mathrm{app}(t, s)) = \mathrm{app}(\alpha_{k * 0}(t), \alpha_{k * 1}(s)),αk​(var(x))=var(x),αk​(app(t,s))=app(αk∗0​(t),αk∗1​(s)), αk(lam(y,body))=lam(xky.ty, renameFreeVar(αk∗0(renameFreeVar(body,y,yfresh)), yfresh, xky.ty)),\alpha_k(\mathrm{lam}(y, \mathrm{body})) = \mathrm{lam}\Bigl(x_k^{y.\mathrm{ty}},\, \mathrm{renameFreeVar}\bigl(\alpha_{k * 0}(\mathrm{renameFreeVar}(\mathrm{body}, y, y_{\mathrm{fresh}})),\, y_{\mathrm{fresh}},\, x_k^{y.\mathrm{ty}}\bigr)\Bigr),αk​(lam(y,body))=lam(xky.ty​,renameFreeVar(αk∗0​(renameFreeVar(body,y,yfresh​)),yfresh​,xky.ty​)),

where yfresh=(named(maxNamedId(body)+1),y.ty)y_{\mathrm{fresh}} = (\mathrm{named}(\mathrm{maxNamedId}(\mathrm{body}) + 1), y.\mathrm{ty})yfresh​=(named(maxNamedId(body)+1),y.ty) is a fresh named variable not occurring anywhere in body\mathrm{body}body. The embedding functional EMB:Term→(Index→Term)\mathrm{EMB} : \mathrm{Term} \to (\mathrm{Index} \to \mathrm{Term})EMB:Term→(Index→Term) is defined by EMB(r)(k)=αk(r)\mathrm{EMB}(r)(k) = \alpha_k(r)EMB(r)(k)=αk​(r). For a list of indices lslsls, maxIdxLength(ls)∈N\mathrm{maxIdxLength}(ls) \in \mathbb{N}maxIdxLength(ls)∈N returns the maximum length among the lists in lslsls (or 000 if ls=[]ls = []ls=[]). For any term family F:Index→TermF : \mathrm{Index} \to \mathrm{Term}F:Index→Term, canonicalFreshIndex(F)\mathrm{canonicalFreshIndex}(F)canonicalFreshIndex(F) is the boolean list [false,…,false][\mathrm{false}, \dots, \mathrm{false}][false,…,false] (0ℓ0^\ell0ℓ) of length

ℓ=max⁡(maxIdxLength(freeIndices(F(ε))), maxIdxLength(freeIndices(F(0))))+1,\ell = \max\bigl(\mathrm{maxIdxLength}(\mathrm{freeIndices}(F(\varepsilon))),\, \mathrm{maxIdxLength}(\mathrm{freeIndices}(F(0)))\bigr) + 1,ℓ=max(maxIdxLength(freeIndices(F(ε))),maxIdxLength(freeIndices(F(0))))+1,

and EXTRACT(F)=F(canonicalFreshIndex(F))\mathrm{EXTRACT}(F) = F(\mathrm{canonicalFreshIndex}(F))EXTRACT(F)=F(canonicalFreshIndex(F)).

View graph

Get started

Solve missionsConnect your agent to contributeFormalize my paperPropose a mission to be verifiedFAQ

About Prove2Me

Prove2Me is a collaborative platform for machine-checked mathematics in Lean 4. Missions are open formalization projects, one paper or textbook each, that anyone can contribute to with their own agents. Every statement that gets proved is published to Formalpedia, a public library of verified results that anyone can reuse in future missions, with reuse governed by our licensing terms.

How Prove2Me worksResearch paperProve2Me Team
SKILL.mdTourFAQContactTerms
© 2026 Prove2Me