Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

Sections 2-3: Simple Types, Binary Word Constants, Higher-Type Recursion, and the Bellantoni-Cook Algebra BC

Definition
BNS2000_Syntax

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

implicit-computational-complexitypolynomial-timeproof-theoryramified-recursiontype-theory

Formalizes Sections 2-3 of Bellantoni, Niggl, and Schwichtenberg (2000): ground types ι_j and arrow types, binary word constants 0, S₀, S₁, P, C, R_σ, capture-avoiding substitution, conversion rules (β_ι), (β_→), (C), (R), term equivalence ≃, the Bellantoni-Cook safe recursion algebra BC (BCFunc, evalBC), and polynomials (IsPolyBound, IsUnivariatePoly).

Definition code
import Std

/-!
# Types, Constants, Terms, Conversions, and Semantics (Bellantoni–Niggl–Schwichtenberg 2000, Section 2)

Source: Stephen Bellantoni, Karl-Heinz Niggl, and Helmut Schwichtenberg,
"Higher type recursion, ramification and polynomial time",
*Annals of Pure and Applied Logic* 104, 2000, pp. 17–30 (Section 2, pp. 18–19 / preprint pp. 2–3).

This module formalizes:
- Ramified types `Ty`: ground type `ι` (`Ty.iota`), function types `σ → τ` (`Ty.arr`),
  and complete types `!σ` (`Ty.bang`).
- Type classifications: `IsComplete`, `IsIncomplete`, `level`, `IsGround`, `IsHigherType`,
  and `IsSafe` (`!`-free types).
- Constants `Const`: `0`, `s₀`, `s₁`, `p`, `c_σ` (for safe `σ`), and `R_σ` (for safe `σ`).
- Typed terms `Term` with `λ`-abstraction, application, `!r`, and `rκ`, along with typing
  `HasType`, term length `|t|` (`termLength`), free variables `FV(t)` (`freeVars`), and
  free occurrence count `FO(x, t)` (`freeOccCount`).
- Binary numerals (`IsBinaryNumeral`, `IsPositiveBinaryNumeral`), general numerals
  (`IsNumeral`, preceded by any number of `!`'s), and the conversion rules (`ConvStep`),
  one-step reduction (`Step`), multi-step reduction (`ReducesTo`), normal forms (`IsNormal`,
  `IsNormalFormOf`), and term equivalence (`TermEquiv`).
- Set-theoretic denotational semantics `⟦t⟧_φ` (`SemTy`, `evalTerm`) identifying `⟦!σ⟧` with `⟦σ⟧`
  and `⟦ι⟧` with `Nat`.
-/

namespace BNS2000

/-- Ramified finite types (Section 2, p. 18):
`σ, τ ::= ι | σ → τ | !σ`. -/
inductive Ty where
  | iota : Ty
  | arr : Ty → Ty → Ty
  | bang : Ty → Ty
  deriving DecidableEq, Repr

namespace Ty

/-- Types of the form `!σ` are *complete*; all others are *incomplete* (Section 2, p. 18). -/
def isComplete : Ty → Bool
  | .bang _ => true
  | _ => false

/-- Incomplete types (Section 2, p. 18). -/
def isIncomplete (σ : Ty) : Bool :=
  !isComplete σ

/-- Type level `l(σ)` (Section 2, p. 18):
- `l(ι) = 0`
- `l(!σ) = l(σ)`
- `l(σ → τ) = max(l(τ), 1 + l(σ))`. -/
def level : Ty → Nat
  | .iota => 0
  | .bang σ => level σ
  | .arr σ τ => Nat.max (level τ) (1 + level σ)

/-- *Ground types* are the types of level `0` (Section 2, p. 18). -/
def isGround (σ : Ty) : Bool :=
  level σ == 0

/-- A *higher type* is any type of level at least `1` (Section 2, pp. 18–19). -/
def isHigherType (σ : Ty) : Bool :=
  decide (level σ ≥ 1)

/-- `!`-free types are called *safe* (Section 2, p. 19). -/
def isSafe : Ty → Bool
  | .iota => true
  | .arr σ τ => isSafe σ && isSafe τ
  | .bang _ => false

/-- Iterated arrow type `σ₁ → ⋯ → σₖ → τ`. -/
def arrMany : List Ty → Ty → Ty
  | [], τ => τ
  | σ :: σs, τ => .arr σ (arrMany σs τ)

end Ty

/-- Constant symbols (Section 2, p. 19):
- `0 : ι`
- `s₀ : ι → ι` (`x ↦ 2 · x`)
- `s₁ : ι → ι` (`x ↦ 2 · x + 1`)
- `p : ι → ι` (`x ↦ ⌊x / 2⌋`)
- `c_σ : ι → σ → σ → σ → σ` for `σ` safe (cases in type `σ`)
- `R_σ : σ → !(!ι → σ → σ) → !ι → σ` for `σ` safe (recursion on notation in type `σ`). -/
inductive Const where
  | zero : Const
  | s0 : Const
  | s1 : Const
  | pred : Const
  | cases : Ty → Const
  | recursor : Ty → Const
  deriving DecidableEq, Repr

namespace Const

/-- Side condition that `c_σ` and `R_σ` are admitted for safe (`!`-free) types `σ` only (p. 19). -/
def isWellFormed : Const → Bool
  | .zero | .s0 | .s1 | .pred => true
  | .cases σ | .recursor σ => σ.isSafe

/-- Type of a constant symbol (Section 2, p. 19). -/
def ty : Const → Ty
  | .zero => .iota
  | .s0 => .arr .iota .iota
  | .s1 => .arr .iota .iota
  | .pred => .arr .iota .iota
  | .cases σ => .arr .iota (.arr σ (.arr σ (.arr σ σ)))
  | .recursor σ => .arr σ (.arr (.bang (.arr (.bang .iota) (.arr σ σ))) (.arr (.bang .iota) σ))

end Const

/-- Typed variable `x^σ` (Section 2, p. 19). -/
structure Var where
  id : Nat
  ty : Ty
  deriving DecidableEq, Repr

/-- Terms built from constants and typed variables by introduction and elimination rules
for `σ → τ` and `!σ` (Section 2, p. 19):
`x^σ`, `c`, `(λx^σ. r^τ)^{σ→τ}`, `(r^{σ→τ} s^σ)^τ`, `(!r^σ)^{!σ}`, `(r^{!σ} κ)^σ`. -/
inductive Term where
  | var : Var → Term
  | const : Const → Term
  | lam : Var → Term → Term
  | app : Term → Term → Term
  | bang : Term → Term
  | kappa : Term → Term
  deriving DecidableEq, Repr

namespace Term

/-- Left-associated application of a term `t` to a list of argument terms `s₁ ... sₙ`. -/
def appMany : Term → List Term → Term
  | t, [] => t
  | t, s :: ss => appMany (.app t s) ss

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

end Term

/-- Typing relation `HasType r τ` (Section 2, p. 19), including the requirement that all
`c_σ` and `R_σ` constants have safe `σ`. -/
inductive HasType : Term → Ty → Prop where
  | var (x : Var) :
      HasType (.var x) x.ty
  | const (c : Const) :
      c.isWellFormed = true →
      HasType (.const c) c.ty
  | lam (x : Var) {r : Term} {τ : Ty} :
      HasType r τ →
      HasType (.lam x r) (.arr x.ty τ)
  | app {r s : Term} {σ τ : Ty} :
      HasType r (.arr σ τ) →
      HasType s σ →
      HasType (.app r s) τ
  | bang {r : Term} {σ : Ty} :
      HasType r σ →
      HasType (.bang r) (.bang σ)
  | kappa {r : Term} {σ : Ty} :
      HasType r (.bang σ) →
      HasType (.kappa r) σ

/-- Every well-typed term `t` has a unique intrinsic type `inferTy t`. -/
def inferTy : Term → Ty
  | .var x => x.ty
  | .const c => c.ty
  | .lam x r => .arr x.ty (inferTy r)
  | .app r _ =>
      match inferTy r with
      | .arr _ τ => τ
      | τ => τ
  | .bang r => .bang (inferTy r)
  | .kappa r =>
      match inferTy r with
      | .bang σ => σ
      | σ => σ

/-- The length `|t|` of a term `t` (Section 2, p. 19):
- `|x| = |c| = 1`
- `|λx. r| = |!r| = |rκ| = |r| + 1`
- `|r s| = |r| + |s| + 1`. -/
def termLength : Term → Nat
  | .var _ => 1
  | .const _ => 1
  | .lam _ r => termLength r + 1
  | .app r s => termLength r + termLength s + 1
  | .bang r => termLength r + 1
  | .kappa r => termLength r + 1

/-- Number of free occurrences `FO(x, t)` of `x` in `t` (Section 2, p. 19). -/
def freeOccCount (x : Var) : Term → Nat
  | .var y => if y == x then 1 else 0
  | .const _ => 0
  | .lam y r => if y == x then 0 else freeOccCount x r
  | .app r s => freeOccCount x r + freeOccCount x s
  | .bang r => freeOccCount x r
  | .kappa r => freeOccCount x r

/-- List of all free variable occurrences in `t`. -/
def freeVarOccurrences : Term → List Var
  | .var x => [x]
  | .const _ => []
  | .lam x r => (freeVarOccurrences r).filter (· != x)
  | .app r s => freeVarOccurrences r ++ freeVarOccurrences s
  | .bang r => freeVarOccurrences r
  | .kappa r => freeVarOccurrences r

/-- Set of free variables `FV(t)` (duplicate-free list, Section 2, p. 19). -/
def freeVars (t : Term) : List Var :=
  (freeVarOccurrences t).eraseDups

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

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

/-- Maximum variable `id` occurring in `t`. -/
def maxVarId : Term → Nat
  | .var x => x.id
  | .const _ => 0
  | .lam x r => Nat.max x.id (maxVarId r)
  | .app r s => Nat.max (maxVarId r) (maxVarId s)
  | .bang r => maxVarId r
  | .kappa r => maxVarId r

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

theorem renameFreeVar_termLength (t : Term) (x y : Var) :
    termLength (renameFreeVar t x y) = termLength t := by
  induction t with
  | var v => simp only [renameFreeVar]; split <;> rfl
  | const c => rfl
  | lam v body ih =>
      simp only [renameFreeVar]
      split
      · rfl
      · simp [termLength, ih]
  | app r s ihr ihs => simp [renameFreeVar, termLength, ihr, ihs]
  | bang r ih => simp [renameFreeVar, termLength, ih]
  | kappa r ih => simp [renameFreeVar, termLength, ih]

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

/-- Simultaneous environment substitution `t[n⃗/x⃗]` for closed/disjoint environment terms `n⃗` (p. 23). -/
def substEnv : Term → List (Var × Term) → Term
  | t, [] => t
  | t, (x, n) :: rest => substEnv (substTerm t x n) rest

/-- Non-zero binary numerals `s_{i₁} ... s_{iₖ} s₁ 0` where `iⱼ ∈ {0, 1}` (Section 2, p. 19). -/
def isPositiveBinaryNumeral : Term → Bool
  | .app (.const .s1) (.const .zero) => true
  | .app (.const .s0) n => isPositiveBinaryNumeral n
  | .app (.const .s1) n => isPositiveBinaryNumeral n
  | _ => false

/-- A *binary numeral* is either `0` or `s_{i₁} ... s_{iₖ} s₁ 0` where `iⱼ ∈ {0, 1}` (Section 2, p. 19). -/
def isBinaryNumeral (t : Term) : Bool :=
  (t == .const .zero) || isPositiveBinaryNumeral t

/-- A *numeral* is a binary numeral preceded by any number of `!`'s (Section 4, p. 22). -/
def isNumeral : Term → Bool
  | .bang r => isNumeral r
  | t => isBinaryNumeral t

/-- Prepend `m` applications of `!` to a term `t`. -/
def bangRepeat : Nat → Term → Term
  | 0, t => t
  | m + 1, t => .bang (bangRepeat m t)

/-- Canonical positive binary numeral for `n + 1` via binary digits. -/
def posBinaryNumeralOfNat (n : Nat) : Term :=
  if h : n == 0 then
    .app (.const .s1) (.const .zero)
  else
    let m := n + 1
    let half := m / 2 - 1
    if m % 2 == 0 then
      .app (.const .s0) (posBinaryNumeralOfNat half)
    else
      .app (.const .s1) (posBinaryNumeralOfNat half)
termination_by n
decreasing_by
  all_goals
    simp_wf
    have hne : n ≠ 0 := by
      intro h0
      subst h0
      contradiction
    omega

/-- Canonical binary numeral for `n : Nat` (Section 2, p. 19). -/
def binaryNumeralOfNat : Nat → Term
  | 0 => .const .zero
  | n + 1 => posBinaryNumeralOfNat n

/-- Value `natOfBinaryNumeral t` of a binary numeral `t` (or arbitrary ground term built from `0, s₀, s₁`). -/
def natOfBinaryNumeral : Term → Nat
  | .const .zero => 0
  | .app (.const .s0) n => 2 * natOfBinaryNumeral n
  | .app (.const .s1) n => 2 * natOfBinaryNumeral n + 1
  | .bang r => natOfBinaryNumeral r
  | .kappa r => natOfBinaryNumeral r
  | _ => 0

/-- Binary length `|n| = ⌈log₂(n + 1)⌉` of a natural number `n` (Section 1, p. 17). -/
def binaryBitLength (n : Nat) : Nat :=
  if h : n == 0 then 0 else binaryBitLength (n / 2) + 1
termination_by n
decreasing_by
  simp_wf
  have hne : n ≠ 0 := by intro h0; subst h0; contradiction
  omega

/-- Conversion rules `redex ↦ contractum` (Section 2, p. 19):
- `(λx. r) s ↦ r[s/x]`
- `(!r) κ ↦ r`
- `s₀ 0 ↦ 0`
- `p 0 ↦ 0`
- `p (sᵢ n) ↦ n` (where `sᵢ n` is a binary numeral, hence distinct from `0`)
- `c 0 r t₀ t₁ ↦ r`
- `c (sᵢ n) r t₀ t₁ ↦ tᵢ` (where `sᵢ n` is a binary numeral)
- `R g h !0 ↦ g`
- `R g h !(sᵢ n) ↦ h κ !(sᵢ n) (R g h !n)` (where `sᵢ n` is a binary numeral). -/
inductive ConvStep : Term → Term → Prop where
  | beta (x : Var) (r s : Term) :
      ConvStep (.app (.lam x r) s) (substTerm r x s)
  | bangKappa (r : Term) :
      ConvStep (.kappa (.bang r)) r
  | s0Zero :
      ConvStep (.app (.const .s0) (.const .zero)) (.const .zero)
  | predZero :
      ConvStep (.app (.const .pred) (.const .zero)) (.const .zero)
  | predSucc0 (n : Term) :
      isPositiveBinaryNumeral (.app (.const .s0) n) = true →
      ConvStep (.app (.const .pred) (.app (.const .s0) n)) n
  | predSucc1 (n : Term) :
      isPositiveBinaryNumeral (.app (.const .s1) n) = true →
      ConvStep (.app (.const .pred) (.app (.const .s1) n)) n
  | casesZero (σ : Ty) (r t₀ t₁ : Term) :
      ConvStep
        (.app (.app (.app (.app (.const (.cases σ)) (.const .zero)) r) t₀) t₁)
        r
  | casesSucc0 (σ : Ty) (n r t₀ t₁ : Term) :
      isPositiveBinaryNumeral (.app (.const .s0) n) = true →
      ConvStep
        (.app (.app (.app (.app (.const (.cases σ)) (.app (.const .s0) n)) r) t₀) t₁)
        t₀
  | casesSucc1 (σ : Ty) (n r t₀ t₁ : Term) :
      isPositiveBinaryNumeral (.app (.const .s1) n) = true →
      ConvStep
        (.app (.app (.app (.app (.const (.cases σ)) (.app (.const .s1) n)) r) t₀) t₁)
        t₁
  | recZero (σ : Ty) (g h : Term) :
      ConvStep
        (.app (.app (.app (.const (.recursor σ)) g) h) (.bang (.const .zero)))
        g
  | recSucc0 (σ : Ty) (g h n : Term) :
      isPositiveBinaryNumeral (.app (.const .s0) n) = true →
      ConvStep
        (.app (.app (.app (.const (.recursor σ)) g) h) (.bang (.app (.const .s0) n)))
        (.app (.app (.kappa h) (.bang (.app (.const .s0) n)))
          (.app (.app (.app (.const (.recursor σ)) g) h) (.bang n)))
  | recSucc1 (σ : Ty) (g h n : Term) :
      isPositiveBinaryNumeral (.app (.const .s1) n) = true →
      ConvStep
        (.app (.app (.app (.const (.recursor σ)) g) h) (.bang (.app (.const .s1) n)))
        (.app (.app (.kappa h) (.bang (.app (.const .s1) n)))
          (.app (.app (.app (.const (.recursor σ)) g) h) (.bang n)))

/-- One-step contextual reduction `r → r'` (Section 2, p. 19 & Section 3, p. 20). -/
inductive Step : Term → Term → Prop where
  | conv {r r' : Term} : ConvStep r r' → Step r r'
  | lam (x : Var) {r r' : Term} : Step r r' → Step (.lam x r) (.lam x r')
  | appLeft {r r' s : Term} : Step r r' → Step (.app r s) (.app r' s)
  | appRight {r s s' : Term} : Step s s' → Step (.app r s) (.app r s')
  | bang {r r' : Term} : Step r r' → Step (.bang r) (.bang r')
  | kappa {r r' : Term} : Step r r' → Step (.kappa r) (.kappa r')

/-- Reflexive-transitive reduction `r →* r'`. -/
inductive ReducesTo : Term → Term → Prop where
  | refl (r : Term) : ReducesTo r r
  | step {r s t : Term} : Step r s → ReducesTo s t → ReducesTo r t

/-- A term `t` is a redex if `ConvStep t t'` holds for some `t'` (Section 2, p. 19). -/
def IsRedex (t : Term) : Prop :=
  ∃ t' : Term, ConvStep t t'

/-- A term `t` is in *normal form* if it contains no redex, i.e. cannot be reduced (Section 2, p. 19). -/
def IsNormal (t : Term) : Prop :=
  ¬∃ t' : Term, Step t t'

/-- `nf_t` is the normal form `nf(t)` of `t` (Section 2, p. 19). -/
def IsNormalFormOf (t nf_t : Term) : Prop :=
  ReducesTo t nf_t ∧ IsNormal nf_t

/-- Two terms are *equivalent* if they have the same normal form (Section 2, p. 19). -/
def TermEquiv (t s : Term) : Prop :=
  ∃ u : Term, IsNormalFormOf t u ∧ IsNormalFormOf s u

/-- Set-theoretic interpretation of types (Section 2, p. 19):
- `⟦ι⟧ = Nat`
- `⟦!σ⟧ = ⟦σ⟧`
- `⟦σ → τ⟧ = ⟦σ⟧ → ⟦τ⟧`. -/
def SemTy : Ty → Type
  | .iota => Nat
  | .bang σ => SemTy σ
  | .arr σ τ => SemTy σ → SemTy τ

/-- Binary recursion on notation `recNat G H n` over `Nat`:
- `recNat G H 0 = G`
- `recNat G H m = H m (recNat G H (m / 2))` for `m > 0`. -/
def recNat {α : Type} (G : α) (H : Nat → α → α) (n : Nat) : α :=
  if h : n == 0 then
    G
  else
    H n (recNat G H (n / 2))
termination_by n
decreasing_by
  simp_wf
  have hne : n ≠ 0 := by intro h0; subst h0; contradiction
  omega

/-- Set-theoretic interpretation `⟦c⟧` of constants (Section 2, p. 19). -/
def evalConst : (c : Const) → SemTy c.ty
  | .zero => (0 : Nat)
  | .s0 => fun (x : Nat) => 2 * x
  | .s1 => fun (x : Nat) => 2 * x + 1
  | .pred => fun (x : Nat) => x / 2
  | .cases _ => fun (n : Nat) r t₀ t₁ =>
      if n == 0 then r else if n % 2 == 0 then t₀ else t₁
  | .recursor _ => fun g h n => recNat g h n

/-- Set-theoretic environment `φ` mapping each variable `x^σ` to `SemTy σ`. -/
abbrev SemEnv := (x : Var) → SemTy x.ty

/-- Update a semantic environment `φ[x ↦ v]`. -/
def updateSemEnv (φ : SemEnv) (x : Var) (v : SemTy x.ty) : SemEnv :=
  fun y => if h : y = x then h ▸ v else φ y

/-- Default inhabitant of `SemTy τ` (with `0 : Nat` at base type `ι`). -/
def defaultSem : (τ : Ty) → SemTy τ
  | .iota => (0 : Nat)
  | .bang σ => defaultSem σ
  | .arr _ τ => fun _ => defaultSem τ

/-- Coerce a semantic value from `SemTy σ` to `SemTy τ` when `σ = τ`. -/
def castSem (σ τ : Ty) (v : SemTy σ) : SemTy τ :=
  if h : σ = τ then h ▸ v else defaultSem τ

/-- Set-theoretic value `⟦t⟧_φ` of a term `t` at type `τ` in environment `φ` (Section 2, p. 19),
where `⟦!r⟧_φ := ⟦r⟧_φ` and `⟦rκ⟧_φ := ⟦r⟧_φ`. -/
def evalTerm (t : Term) (τ : Ty) (φ : SemEnv) : SemTy τ :=
  match t, τ with
  | .var x, τ => castSem x.ty τ (φ x)
  | .const c, τ => castSem c.ty τ (evalConst c)
  | .lam x r, .arr σ ρ =>
      fun v => evalTerm r ρ (updateSemEnv φ x (castSem σ x.ty v))
  | .app r s, τ =>
      let σ := inferTy s
      (evalTerm r (.arr σ τ) φ) (evalTerm s σ φ)
  | .bang r, .bang σ => evalTerm r σ φ
  | .kappa r, τ => evalTerm r (.bang τ) φ
  | _, τ => defaultSem τ

end BNS2000
Source
Stephen Bellantoni, Karl-Heinz Niggl, and Helmut Schwichtenberg, 'Higher type recursion, ramification and polynomial time', Annals of Pure and Applied Logic 104, 2000, pp. 17-30 (doi:10.1016/S0168-0072(00)00006-3). Sections 2-3, pp. 18-21.
Read-back

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

Read-back: Def_BNS2000_Syntax.lean

1. Types and Type Classifications (Ty)

Ty is an inductive type of syntax trees generated by three constructors:

  • ι\iotaι (Ty.iota), a base constant;
  • σ→τ\sigma \to \tauσ→τ (Ty.arr σ τ), a binary constructor for σ,τ∈Ty\sigma, \tau \in \mathsf{Ty}σ,τ∈Ty;
  • !σ!\sigma!σ (Ty.bang σ), a unary constructor for σ∈Ty\sigma \in \mathsf{Ty}σ∈Ty.

On σ∈Ty\sigma \in \mathsf{Ty}σ∈Ty, the module defines the following total functions:

  • Ty.isComplete: returns true\mathsf{true}true if σ\sigmaσ has the form !ρ!\rho!ρ for some ρ∈Ty\rho \in \mathsf{Ty}ρ∈Ty, and false\mathsf{false}false if σ=ι\sigma = \iotaσ=ι or σ=ρ1→ρ2\sigma = \rho_1 \to \rho_2σ=ρ1​→ρ2​.

  • Ty.isIncomplete: returns the boolean negation ¬ isComplete(σ)\neg\,\mathsf{isComplete}(\sigma)¬isComplete(σ) (i.e., true\mathsf{true}true when σ=ι\sigma = \iotaσ=ι or σ=ρ1→ρ2\sigma = \rho_1 \to \rho_2σ=ρ1​→ρ2​, and false\mathsf{false}false when σ=!ρ\sigma = !\rhoσ=!ρ).

  • Ty.level: recursively computes a natural number l(σ)∈Nl(\sigma) \in \mathbb{N}l(σ)∈N by

l(ι)=0,l(!σ)=l(σ),l(σ→τ)=max⁡(l(τ), 1+l(σ)).l(\iota) = 0, \qquad l(!\sigma) = l(\sigma), \qquad l(\sigma \to \tau) = \max\bigl(l(\tau),\, 1 + l(\sigma)\bigr).l(ι)=0,l(!σ)=l(σ),l(σ→τ)=max(l(τ),1+l(σ)).
  • Ty.isGround: returns true\mathsf{true}true if and only if l(σ)=0l(\sigma) = 0l(σ)=0. By the definition of lll, the types satisfying isGround(σ)=true\mathsf{isGround}(\sigma) = \mathsf{true}isGround(σ)=true are precisely those of the form !⋯!⏟mι\underbrace{!\cdots!}_{m}\iotam!⋯!​​ι for m≥0m \ge 0m≥0 (no →\to→ constructors).

  • Ty.isHigherType: returns true\mathsf{true}true if and only if l(σ)≥1l(\sigma) \ge 1l(σ)≥1 (equivalently, σ\sigmaσ contains at least one →\to→ constructor).

  • Ty.isSafe: returns true\mathsf{true}true if and only if σ\sigmaσ contains no !!! constructor anywhere, defined recursively by

isSafe(ι)=true,isSafe(σ→τ)=isSafe(σ)∧isSafe(τ),isSafe(!σ)=false.\mathsf{isSafe}(\iota) = \mathsf{true}, \qquad \mathsf{isSafe}(\sigma \to \tau) = \mathsf{isSafe}(\sigma) \wedge \mathsf{isSafe}(\tau), \qquad \mathsf{isSafe}(!\sigma) = \mathsf{false}.isSafe(ι)=true,isSafe(σ→τ)=isSafe(σ)∧isSafe(τ),isSafe(!σ)=false.

Edge case note: isGround\mathsf{isGround}isGround and isSafe\mathsf{isSafe}isSafe are orthogonal above ι\iotaι: for example, !ι!\iota!ι is ground (l(!ι)=0l(!\iota) = 0l(!ι)=0) but not safe, whereas ι→ι\iota \to \iotaι→ι is safe but not ground (l(ι→ι)=1l(\iota \to \iota) = 1l(ι→ι)=1). Only ι\iotaι is both safe and ground.

  • Ty.arrMany: given a list of types [σ1,…,σk][\sigma_1, \dots, \sigma_k][σ1​,…,σk​] and a type τ\tauτ, returns the right-associated iterated arrow type
arrMany([σ1,…,σk],τ)=σ1→(σ2→⋯→(σk→τ)⋯ ),\mathsf{arrMany}([\sigma_1, \dots, \sigma_k], \tau) = \sigma_1 \to (\sigma_2 \to \cdots \to (\sigma_k \to \tau)\cdots),arrMany([σ1​,…,σk​],τ)=σ1​→(σ2​→⋯→(σk​→τ)⋯),

with arrMany([],τ)=τ\mathsf{arrMany}([], \tau) = \tauarrMany([],τ)=τ.


2. Constants (Const)

Const is an inductive type with six constructors:

  • Const.zero (000),

  • Const.s0 (s0s_0s0​),

  • Const.s1 (s1s_1s1​),

  • Const.pred (ppp),

  • Const.cases σ (cσc_\sigmacσ​) for any σ∈Ty\sigma \in \mathsf{Ty}σ∈Ty,

  • Const.recursor σ (RσR_\sigmaRσ​) for any σ∈Ty\sigma \in \mathsf{Ty}σ∈Ty.

  • Const.isWellFormed: for c∈Constc \in \mathsf{Const}c∈Const, returns true\mathsf{true}true when c∈{0,s0,s1,p}c \in \{0, s_0, s_1, p\}c∈{0,s0​,s1​,p}, and returns isSafe(σ)\mathsf{isSafe}(\sigma)isSafe(σ) when c=cσc = c_\sigmac=cσ​ or c=Rσc = R_\sigmac=Rσ​. (Note that the type parameter σ\sigmaσ is unconstrained in the syntax of Const, and the restriction to !!!-free σ\sigmaσ is enforced separately via isWellFormed inside the typing relation HasType.)

  • Const.ty: assigns a type c.ty∈Tyc.\mathsf{ty} \in \mathsf{Ty}c.ty∈Ty to every c∈Constc \in \mathsf{Const}c∈Const:

0.ty=ι,s0.ty=s1.ty=p.ty=ι→ι,cσ.ty=ι→(σ→(σ→(σ→σ))),Rσ.ty=σ→(!(!ι→(σ→σ))→(!ι→σ)).\begin{aligned} 0.\mathsf{ty} &= \iota, \\ s_0.\mathsf{ty} = s_1.\mathsf{ty} = p.\mathsf{ty} &= \iota \to \iota, \\ c_\sigma.\mathsf{ty} &= \iota \to (\sigma \to (\sigma \to (\sigma \to \sigma))), \\ R_\sigma.\mathsf{ty} &= \sigma \to \bigl(!(!\iota \to (\sigma \to \sigma)) \to (!\iota \to \sigma)\bigr). \end{aligned}0.tys0​.ty=s1​.ty=p.tycσ​.tyRσ​.ty​=ι,=ι→ι,=ι→(σ→(σ→(σ→σ))),=σ→(!(!ι→(σ→σ))→(!ι→σ)).​

3. Variables, Terms, Typing, and Term Metrics (Var, Term, HasType, inferTy, termLength)

  • Var: a structure consisting of a natural-number identifier id∈N\mathsf{id} \in \mathbb{N}id∈N and an intrinsic type ty∈Ty\mathsf{ty} \in \mathsf{Ty}ty∈Ty, written x=(id,ty)x = (\mathsf{id}, \mathsf{ty})x=(id,ty). Equality of variables (DecidableEq) compares both id\mathsf{id}id and ty\mathsf{ty}ty; thus (id,σ)(\mathsf{id}, \sigma)(id,σ) and (id,τ)(\mathsf{id}, \tau)(id,τ) with σ≠τ\sigma \ne \tauσ=τ are distinct Var values.

  • Term: an inductive type of syntax trees with six constructors:

    • var(x)\mathsf{var}(x)var(x) for x∈Varx \in \mathsf{Var}x∈Var,
    • const(c)\mathsf{const}(c)const(c) for c∈Constc \in \mathsf{Const}c∈Const,
    • λx. r\lambda x.\, rλx.r (Term.lam x r) for x∈Varx \in \mathsf{Var}x∈Var and r∈Termr \in \mathsf{Term}r∈Term,
    • r sr\, srs (Term.app r s) for r,s∈Termr, s \in \mathsf{Term}r,s∈Term,
    • !r!r!r (Term.bang r) for r∈Termr \in \mathsf{Term}r∈Term,
    • rκr\kapparκ (Term.kappa r) for r∈Termr \in \mathsf{Term}r∈Term.
  • Term.appMany: given t∈Termt \in \mathsf{Term}t∈Term and a list [s1,…,sn][s_1, \dots, s_n][s1​,…,sn​], returns the left-associated application (⋯((t s1) s2)⋯sn)(\cdots((t\, s_1)\, s_2)\cdots s_n)(⋯((ts1​)s2​)⋯sn​), with appMany(t,[])=t\mathsf{appMany}(t, []) = tappMany(t,[])=t.

  • Term.lamMany: given a list of variables [x1,…,xk][x_1, \dots, x_k][x1​,…,xk​] and r∈Termr \in \mathsf{Term}r∈Term, returns λx1. (⋯(λxk. r)⋯ )\lambda x_1.\, (\cdots(\lambda x_k.\, r)\cdots)λx1​.(⋯(λxk​.r)⋯), with lamMany([],r)=r\mathsf{lamMany}([], r) = rlamMany([],r)=r.

  • HasType: an inductive binary relation HasType(t,τ)\mathsf{HasType}(t, \tau)HasType(t,τ) on Term×Ty\mathsf{Term} \times \mathsf{Ty}Term×Ty generated by:

    1. Variable: for every x∈Varx \in \mathsf{Var}x∈Var, HasType(var(x),x.ty)\mathsf{HasType}(\mathsf{var}(x), x.\mathsf{ty})HasType(var(x),x.ty).
    2. Constant: for every c∈Constc \in \mathsf{Const}c∈Const, if c.isWellFormed=truec.\mathsf{isWellFormed} = \mathsf{true}c.isWellFormed=true, then HasType(const(c),c.ty)\mathsf{HasType}(\mathsf{const}(c), c.\mathsf{ty})HasType(const(c),c.ty).
    3. Abstraction: for every x∈Varx \in \mathsf{Var}x∈Var, r∈Termr \in \mathsf{Term}r∈Term, and τ∈Ty\tau \in \mathsf{Ty}τ∈Ty, if HasType(r,τ)\mathsf{HasType}(r, \tau)HasType(r,τ), then HasType(λx. r,x.ty→τ)\mathsf{HasType}(\lambda x.\, r, x.\mathsf{ty} \to \tau)HasType(λx.r,x.ty→τ).
    4. Application: for every r,s∈Termr, s \in \mathsf{Term}r,s∈Term and σ,τ∈Ty\sigma, \tau \in \mathsf{Ty}σ,τ∈Ty, if HasType(r,σ→τ)\mathsf{HasType}(r, \sigma \to \tau)HasType(r,σ→τ) and HasType(s,σ)\mathsf{HasType}(s, \sigma)HasType(s,σ), then HasType(r s,τ)\mathsf{HasType}(r\, s, \tau)HasType(rs,τ).
    5. Bang introduction: for every r∈Termr \in \mathsf{Term}r∈Term and σ∈Ty\sigma \in \mathsf{Ty}σ∈Ty, if HasType(r,σ)\mathsf{HasType}(r, \sigma)HasType(r,σ), then HasType(!r,!σ)\mathsf{HasType}(!r, !\sigma)HasType(!r,!σ).
    6. Kappa elimination: for every r∈Termr \in \mathsf{Term}r∈Term and σ∈Ty\sigma \in \mathsf{Ty}σ∈Ty, if HasType(r,!σ)\mathsf{HasType}(r, !\sigma)HasType(r,!σ), then HasType(rκ,σ)\mathsf{HasType}(r\kappa, \sigma)HasType(rκ,σ).

    Edge case note: Because each Var carries its own type field x.tyx.\mathsf{ty}x.ty, there is no separate typing context Γ\GammaΓ; a variable occurrence is typed solely by its embedded field x.tyx.\mathsf{ty}x.ty.

  • inferTy: a total function inferTy:Term→Ty\mathsf{inferTy} : \mathsf{Term} \to \mathsf{Ty}inferTy:Term→Ty defined by:

    • inferTy(var(x))=x.ty\mathsf{inferTy}(\mathsf{var}(x)) = x.\mathsf{ty}inferTy(var(x))=x.ty,
    • inferTy(const(c))=c.ty\mathsf{inferTy}(\mathsf{const}(c)) = c.\mathsf{ty}inferTy(const(c))=c.ty,
    • inferTy(λx. r)=x.ty→inferTy(r)\mathsf{inferTy}(\lambda x.\, r) = x.\mathsf{ty} \to \mathsf{inferTy}(r)inferTy(λx.r)=x.ty→inferTy(r),
    • inferTy(r s)=τ\mathsf{inferTy}(r\, s) = \tauinferTy(rs)=τ if inferTy(r)=σ→τ\mathsf{inferTy}(r) = \sigma \to \tauinferTy(r)=σ→τ for some σ,τ\sigma, \tauσ,τ, and inferTy(r)\mathsf{inferTy}(r)inferTy(r) otherwise (fallback value on ill-typed applications),
    • inferTy(!r)=!(inferTy(r))\mathsf{inferTy}(!r) = !(\mathsf{inferTy}(r))inferTy(!r)=!(inferTy(r)),
    • inferTy(rκ)=σ\mathsf{inferTy}(r\kappa) = \sigmainferTy(rκ)=σ if inferTy(r)=!σ\mathsf{inferTy}(r) = !\sigmainferTy(r)=!σ for some σ\sigmaσ, and inferTy(r)\mathsf{inferTy}(r)inferTy(r) otherwise (fallback value on ill-typed κ\kappaκ-terms).
  • termLength: a total function ∣t∣∈N|t| \in \mathbb{N}∣t∣∈N defined recursively by

∣var(x)∣=∣const(c)∣=1,∣λx. r∣=∣!r∣=∣rκ∣=∣r∣+1,∣r s∣=∣r∣+∣s∣+1.|\mathsf{var}(x)| = |\mathsf{const}(c)| = 1, \qquad |\lambda x.\, r| = |!r| = |r\kappa| = |r| + 1, \qquad |r\, s| = |r| + |s| + 1.∣var(x)∣=∣const(c)∣=1,∣λx.r∣=∣!r∣=∣rκ∣=∣r∣+1,∣rs∣=∣r∣+∣s∣+1.

Every term ttt satisfies ∣t∣≥1|t| \ge 1∣t∣≥1.


4. Variable Occurrences and Capture-Avoiding Substitution

  • freeOccCount: for x∈Varx \in \mathsf{Var}x∈Var and t∈Termt \in \mathsf{Term}t∈Term, computes the number of free occurrences FO(x,t)∈N\mathsf{FO}(x, t) \in \mathbb{N}FO(x,t)∈N of xxx in ttt:
FO(x,var(y))={1if y=x,0if y≠x,FO(x,const(c))=0,FO(x,λy. r)={0if y=x,FO(x,r)if y≠x,FO(x,r s)=FO(x,r)+FO(x,s),FO(x,!r)=FO(x,rκ)=FO(x,r).\begin{aligned} \mathsf{FO}(x, \mathsf{var}(y)) &= \begin{cases} 1 & \text{if } y = x, \\ 0 & \text{if } y \ne x, \end{cases} \\ \mathsf{FO}(x, \mathsf{const}(c)) &= 0, \\ \mathsf{FO}(x, \lambda y.\, r) &= \begin{cases} 0 & \text{if } y = x, \\ \mathsf{FO}(x, r) & \text{if } y \ne x, \end{cases} \\ \mathsf{FO}(x, r\, s) &= \mathsf{FO}(x, r) + \mathsf{FO}(x, s), \\ \mathsf{FO}(x, !r) &= \mathsf{FO}(x, r\kappa) = \mathsf{FO}(x, r). \end{aligned}FO(x,var(y))FO(x,const(c))FO(x,λy.r)FO(x,rs)FO(x,!r)​={10​if y=x,if y=x,​=0,={0FO(x,r)​if y=x,if y=x,​=FO(x,r)+FO(x,s),=FO(x,rκ)=FO(x,r).​
  • freeVarOccurrences and freeVars: freeVarOccurrences t returns the list of free variable occurrences in ttt in left-to-right order, filtering out xxx under λx\lambda xλx. freeVars t returns (freeVarOccurrences t).eraseDups (removing later duplicates).

  • allVars: returns the list of all variables occurring in ttt, including both binder variables xxx at every λx. r\lambda x.\, rλx.r and leaf variables xxx at every var(x)\mathsf{var}(x)var(x).

  • IsClosed: for t∈Termt \in \mathsf{Term}t∈Term, the proposition freeVars(t)=[]\mathsf{freeVars}(t) = []freeVars(t)=[].

  • maxVarId: returns the maximum id\mathsf{id}id among all variables (free or bound) in ttt, or 000 if ttt contains only constants.

  • renameFreeVar and renameFreeVar_termLength: renameFreeVar(t,x,y)\mathsf{renameFreeVar}(t, x, y)renameFreeVar(t,x,y) replaces every free occurrence of xxx in ttt by var(y)\mathsf{var}(y)var(y) (stopping at λv\lambda vλv if v=xv = xv=x, without renaming other binders). The theorem renameFreeVar_termLength states that for all t∈Termt \in \mathsf{Term}t∈Term and x,y∈Varx, y \in \mathsf{Var}x,y∈Var, ∣renameFreeVar(t,x,y)∣=∣t∣|\mathsf{renameFreeVar}(t, x, y)| = |t|∣renameFreeVar(t,x,y)∣=∣t∣.

  • substTerm: for r,s∈Termr, s \in \mathsf{Term}r,s∈Term and x∈Varx \in \mathsf{Var}x∈Var, computes r[s/x]r[s/x]r[s/x] recursively on ∣r∣|r|∣r∣:

    • var(v)[s/x]=s\mathsf{var}(v)[s/x] = svar(v)[s/x]=s if v=xv = xv=x, and var(v)\mathsf{var}(v)var(v) if v≠xv \ne xv=x;
    • const(c)[s/x]=const(c)\mathsf{const}(c)[s/x] = \mathsf{const}(c)const(c)[s/x]=const(c);
    • (λv. body)[s/x](\lambda v.\, \mathsf{body})[s/x](λv.body)[s/x]:
      • if v=xv = xv=x, returns λv. body\lambda v.\, \mathsf{body}λv.body;
      • if v≠xv \ne xv=x and v∈freeVarOccurrences(s)v \in \mathsf{freeVarOccurrences}(s)v∈freeVarOccurrences(s), defines v′=(max⁡(maxVarId(body),max⁡(maxVarId(s),x.id))+1, v.ty)v' = (\max(\mathsf{maxVarId}(\mathsf{body}), \max(\mathsf{maxVarId}(s), x.\mathsf{id})) + 1,\, v.\mathsf{ty})v′=(max(maxVarId(body),max(maxVarId(s),x.id))+1,v.ty) and returns λv′. (renameFreeVar(body,v,v′)[s/x])\lambda v'.\, (\mathsf{renameFreeVar}(\mathsf{body}, v, v')[s/x])λv′.(renameFreeVar(body,v,v′)[s/x]);
      • otherwise, returns λv. (body[s/x])\lambda v.\, (\mathsf{body}[s/x])λv.(body[s/x]);
    • (t u)[s/x]=(t[s/x]) (u[s/x])(t\, u)[s/x] = (t[s/x])\, (u[s/x])(tu)[s/x]=(t[s/x])(u[s/x]), (!t)[s/x]=!(t[s/x])(!t)[s/x] = !(t[s/x])(!t)[s/x]=!(t[s/x]), and (tκ)[s/x]=(t[s/x])κ(t\kappa)[s/x] = (t[s/x])\kappa(tκ)[s/x]=(t[s/x])κ. Edge case note: In the λv. body\lambda v.\, \mathsf{body}λv.body branch, if v≠xv \ne xv=x and v∈freeVarOccurrences(s)v \in \mathsf{freeVarOccurrences}(s)v∈freeVarOccurrences(s), the binder vvv is α\alphaα-renamed to v′v'v′ even when x∉freeVars(body)x \notin \mathsf{freeVars}(\mathsf{body})x∈/freeVars(body).
  • substEnv: given t∈Termt \in \mathsf{Term}t∈Term and a list of pairs [(x1,n1),…,(xk,nk)][(x_1, n_1), \dots, (x_k, n_k)][(x1​,n1​),…,(xk​,nk​)], performs sequential left-to-right substitution:

substEnv(t,[])=t,substEnv(t,(x1,n1)::rest)=substEnv(t[n1/x1],rest).\mathsf{substEnv}(t, []) = t, \qquad \mathsf{substEnv}\bigl(t, (x_1, n_1) :: \mathsf{rest}\bigr) = \mathsf{substEnv}\bigl(t[n_1/x_1], \mathsf{rest}\bigr).substEnv(t,[])=t,substEnv(t,(x1​,n1​)::rest)=substEnv(t[n1​/x1​],rest).

Edge case note: This is sequential rather than simultaneous substitution; it coincides with simultaneous substitution when each nin_ini​ has no free occurrences of xi+1,…,xkx_{i+1}, \dots, x_kxi+1​,…,xk​ (in particular, when all nin_ini​ are closed and x1,…,xkx_1, \dots, x_kx1​,…,xk​ are distinct).


5. Numerals (isPositiveBinaryNumeral, isBinaryNumeral, isNumeral, binaryNumeralOfNat, natOfBinaryNumeral, binaryBitLength)

  • isPositiveBinaryNumeral: returns true\mathsf{true}true on t∈Termt \in \mathsf{Term}t∈Term if and only if ttt has the form
si1(si2(⋯(sik(s1 0))⋯ ))s_{i_1}(s_{i_2}(\cdots(s_{i_k}(s_1\, 0))\cdots))si1​​(si2​​(⋯(sik​​(s1​0))⋯))

for some k≥0k \ge 0k≥0 and i1,…,ik∈{0,1}i_1, \dots, i_k \in \{0, 1\}i1​,…,ik​∈{0,1}.

  • isBinaryNumeral: returns true\mathsf{true}true on t∈Termt \in \mathsf{Term}t∈Term if and only if t=const(0)t = \mathsf{const}(0)t=const(0) or isPositiveBinaryNumeral(t)=true\mathsf{isPositiveBinaryNumeral}(t) = \mathsf{true}isPositiveBinaryNumeral(t)=true.

  • isNumeral: returns true\mathsf{true}true on t∈Termt \in \mathsf{Term}t∈Term if and only if t=!⋯!⏟mnt = \underbrace{!\cdots!}_{m} nt=m!⋯!​​n for some m≥0m \ge 0m≥0 and n∈Termn \in \mathsf{Term}n∈Term with isBinaryNumeral(n)=true\mathsf{isBinaryNumeral}(n) = \mathsf{true}isBinaryNumeral(n)=true.

  • bangRepeat: bangRepeat(m,t)\mathsf{bangRepeat}(m, t)bangRepeat(m,t) applies Term.bang\mathsf{Term.bang}Term.bang mmm times to ttt, yielding !mt!^m t!mt.

  • posBinaryNumeralOfNat and binaryNumeralOfNat: for n∈Nn \in \mathbb{N}n∈N, binaryNumeralOfNat(0)=const(0)\mathsf{binaryNumeralOfNat}(0) = \mathsf{const}(0)binaryNumeralOfNat(0)=const(0), and for n≥0n \ge 0n≥0, binaryNumeralOfNat(n+1)=posBinaryNumeralOfNat(n)\mathsf{binaryNumeralOfNat}(n + 1) = \mathsf{posBinaryNumeralOfNat}(n)binaryNumeralOfNat(n+1)=posBinaryNumeralOfNat(n) constructs the canonical positive binary numeral for n+1n + 1n+1 by binary recursion:

    • for n=0n = 0n=0 (representing 111), s1 0s_1\, 0s1​0;
    • for n>0n > 0n>0 (representing m=n+1≥2m = n + 1 \ge 2m=n+1≥2), sm mod 2(posBinaryNumeralOfNat(⌊m/2⌋−1))s_{m \bmod 2}(\mathsf{posBinaryNumeralOfNat}(\lfloor m/2 \rfloor - 1))smmod2​(posBinaryNumeralOfNat(⌊m/2⌋−1)).
  • natOfBinaryNumeral: maps a term ttt to a natural number by

natOfBinaryNumeral(0)=0,natOfBinaryNumeral(si n)=2⋅natOfBinaryNumeral(n)+i    (i∈{0,1}),\mathsf{natOfBinaryNumeral}(0) = 0, \quad \mathsf{natOfBinaryNumeral}(s_i\, n) = 2 \cdot \mathsf{natOfBinaryNumeral}(n) + i \;\; (i \in \{0,1\}),natOfBinaryNumeral(0)=0,natOfBinaryNumeral(si​n)=2⋅natOfBinaryNumeral(n)+i(i∈{0,1}), natOfBinaryNumeral(!r)=natOfBinaryNumeral(rκ)=natOfBinaryNumeral(r),\mathsf{natOfBinaryNumeral}(!r) = \mathsf{natOfBinaryNumeral}(r\kappa) = \mathsf{natOfBinaryNumeral}(r),natOfBinaryNumeral(!r)=natOfBinaryNumeral(rκ)=natOfBinaryNumeral(r),

and returns the fallback value 000 on all other terms. (Note: natOfBinaryNumeral and binaryBitLength are not referenced in any of the theorem statements.)

  • binaryBitLength: for n∈Nn \in \mathbb{N}n∈N, returns 000 if n=0n = 0n=0, and binaryBitLength(⌊n/2⌋)+1\mathsf{binaryBitLength}(\lfloor n / 2 \rfloor) + 1binaryBitLength(⌊n/2⌋)+1 if n>0n > 0n>0.

6. Reduction, Normal Forms, and Term Equivalence (ConvStep, Step, ReducesTo, IsRedex, IsNormal, IsNormalFormOf, TermEquiv)

  • ConvStep: an inductive binary relation t↦convt′t \mapsto_{\mathsf{conv}} t't↦conv​t′ on Term\mathsf{Term}Term with 12 constructors:

    1. (λx. r) s↦convr[s/x](\lambda x.\, r)\, s \mapsto_{\mathsf{conv}} r[s/x](λx.r)s↦conv​r[s/x] for any x∈Varx \in \mathsf{Var}x∈Var and r,s∈Termr, s \in \mathsf{Term}r,s∈Term;
    2. (!r)κ↦convr(!r)\kappa \mapsto_{\mathsf{conv}} r(!r)κ↦conv​r for any r∈Termr \in \mathsf{Term}r∈Term;
    3. s0 0↦conv0s_0\, 0 \mapsto_{\mathsf{conv}} 0s0​0↦conv​0;
    4. p 0↦conv0p\, 0 \mapsto_{\mathsf{conv}} 0p0↦conv​0;
    5. p (s0 n)↦convnp\,(s_0\, n) \mapsto_{\mathsf{conv}} np(s0​n)↦conv​n whenever isPositiveBinaryNumeral(s0 n)=true\mathsf{isPositiveBinaryNumeral}(s_0\, n) = \mathsf{true}isPositiveBinaryNumeral(s0​n)=true;
    6. p (s1 n)↦convnp\,(s_1\, n) \mapsto_{\mathsf{conv}} np(s1​n)↦conv​n whenever isPositiveBinaryNumeral(s1 n)=true\mathsf{isPositiveBinaryNumeral}(s_1\, n) = \mathsf{true}isPositiveBinaryNumeral(s1​n)=true;
    7. cσ 0 r t0 t1↦convrc_\sigma\, 0\, r\, t_0\, t_1 \mapsto_{\mathsf{conv}} rcσ​0rt0​t1​↦conv​r for any σ∈Ty\sigma \in \mathsf{Ty}σ∈Ty and r,t0,t1∈Termr, t_0, t_1 \in \mathsf{Term}r,t0​,t1​∈Term;
    8. cσ (s0 n) r t0 t1↦convt0c_\sigma\,(s_0\, n)\, r\, t_0\, t_1 \mapsto_{\mathsf{conv}} t_0cσ​(s0​n)rt0​t1​↦conv​t0​ whenever isPositiveBinaryNumeral(s0 n)=true\mathsf{isPositiveBinaryNumeral}(s_0\, n) = \mathsf{true}isPositiveBinaryNumeral(s0​n)=true;
    9. cσ (s1 n) r t0 t1↦convt1c_\sigma\,(s_1\, n)\, r\, t_0\, t_1 \mapsto_{\mathsf{conv}} t_1cσ​(s1​n)rt0​t1​↦conv​t1​ whenever isPositiveBinaryNumeral(s1 n)=true\mathsf{isPositiveBinaryNumeral}(s_1\, n) = \mathsf{true}isPositiveBinaryNumeral(s1​n)=true;
    10. Rσ g h (!0)↦convgR_\sigma\, g\, h\, (!0) \mapsto_{\mathsf{conv}} gRσ​gh(!0)↦conv​g for any σ∈Ty\sigma \in \mathsf{Ty}σ∈Ty and g,h∈Termg, h \in \mathsf{Term}g,h∈Term;
    11. Rσ g h (!(s0 n))↦conv(hκ) (!(s0 n)) (Rσ g h (!n))R_\sigma\, g\, h\, (!(s_0\, n)) \mapsto_{\mathsf{conv}} (h\kappa)\, (!(s_0\, n))\, (R_\sigma\, g\, h\, (!n))Rσ​gh(!(s0​n))↦conv​(hκ)(!(s0​n))(Rσ​gh(!n)) whenever isPositiveBinaryNumeral(s0 n)=true\mathsf{isPositiveBinaryNumeral}(s_0\, n) = \mathsf{true}isPositiveBinaryNumeral(s0​n)=true;
    12. Rσ g h (!(s1 n))↦conv(hκ) (!(s1 n)) (Rσ g h (!n))R_\sigma\, g\, h\, (!(s_1\, n)) \mapsto_{\mathsf{conv}} (h\kappa)\, (!(s_1\, n))\, (R_\sigma\, g\, h\, (!n))Rσ​gh(!(s1​n))↦conv​(hκ)(!(s1​n))(Rσ​gh(!n)) whenever isPositiveBinaryNumeral(s1 n)=true\mathsf{isPositiveBinaryNumeral}(s_1\, n) = \mathsf{true}isPositiveBinaryNumeral(s1​n)=true.
  • Step: one-step contextual reduction r→r′r \to r'r→r′, the closure of ConvStep\mathsf{ConvStep}ConvStep under λx. [⋅]\lambda x.\, [\cdot]λx.[⋅], [⋅] s[\cdot]\, s[⋅]s, r [⋅]r\, [\cdot]r[⋅], ![⋅]![\cdot]![⋅], and [⋅]κ[\cdot]\kappa[⋅]κ.

  • ReducesTo: the reflexive-transitive closure r→∗r′r \to^* r'r→∗r′ of Step\mathsf{Step}Step.

  • IsRedex: IsRedex(t)  ⟺  ∃t′∈Term,  ConvStep(t,t′)\mathsf{IsRedex}(t) \iff \exists t' \in \mathsf{Term},\; \mathsf{ConvStep}(t, t')IsRedex(t)⟺∃t′∈Term,ConvStep(t,t′).

  • IsNormal: IsNormal(t)  ⟺  ¬∃t′∈Term,  Step(t,t′)\mathsf{IsNormal}(t) \iff \neg\exists t' \in \mathsf{Term},\; \mathsf{Step}(t, t')IsNormal(t)⟺¬∃t′∈Term,Step(t,t′).

  • IsNormalFormOf: IsNormalFormOf(t,u)  ⟺  ReducesTo(t,u)∧IsNormal(u)\mathsf{IsNormalFormOf}(t, u) \iff \mathsf{ReducesTo}(t, u) \wedge \mathsf{IsNormal}(u)IsNormalFormOf(t,u)⟺ReducesTo(t,u)∧IsNormal(u).

  • TermEquiv: TermEquiv(t,s)  ⟺  ∃u∈Term,  IsNormalFormOf(t,u)∧IsNormalFormOf(s,u)\mathsf{TermEquiv}(t, s) \iff \exists u \in \mathsf{Term},\; \mathsf{IsNormalFormOf}(t, u) \wedge \mathsf{IsNormalFormOf}(s, u)TermEquiv(t,s)⟺∃u∈Term,IsNormalFormOf(t,u)∧IsNormalFormOf(s,u).


7. Set-Theoretic Semantics (SemTy, recNat, evalConst, SemEnv, evalTerm)

  • SemTy: maps each σ∈Ty\sigma \in \mathsf{Ty}σ∈Ty to a Lean type ⟦σ⟧\llbracket \sigma \rrbracket[[σ]]:
⟦ι⟧=N,⟦!σ⟧=⟦σ⟧,⟦σ→τ⟧=⟦σ⟧→⟦τ⟧.\llbracket \iota \rrbracket = \mathbb{N}, \qquad \llbracket !\sigma \rrbracket = \llbracket \sigma \rrbracket, \qquad \llbracket \sigma \to \tau \rrbracket = \llbracket \sigma \rrbracket \to \llbracket \tau \rrbracket.[[ι]]=N,[[!σ]]=[[σ]],[[σ→τ]]=[[σ]]→[[τ]].
  • recNat: given a type α\alphaα, G∈αG \in \alphaG∈α, H:N→α→αH : \mathbb{N} \to \alpha \to \alphaH:N→α→α, and n∈Nn \in \mathbb{N}n∈N, returns GGG if n=0n = 0n=0, and H(n,recNat(G,H,⌊n/2⌋))H(n, \mathsf{recNat}(G, H, \lfloor n / 2 \rfloor))H(n,recNat(G,H,⌊n/2⌋)) if n>0n > 0n>0.

  • evalConst: assigns to each c∈Constc \in \mathsf{Const}c∈Const an element ⟦c⟧∈⟦c.ty⟧\llbracket c \rrbracket \in \llbracket c.\mathsf{ty} \rrbracket[[c]]∈[[c.ty]]:

    • ⟦0⟧=0\llbracket 0 \rrbracket = 0[[0]]=0,
    • ⟦s0⟧(x)=2x\llbracket s_0 \rrbracket(x) = 2x[[s0​]](x)=2x, ⟦s1⟧(x)=2x+1\llbracket s_1 \rrbracket(x) = 2x + 1[[s1​]](x)=2x+1, ⟦p⟧(x)=⌊x/2⌋\llbracket p \rrbracket(x) = \lfloor x / 2 \rfloor[[p]](x)=⌊x/2⌋,
    • ⟦cσ⟧(n,r,t0,t1)=r\llbracket c_\sigma \rrbracket(n, r, t_0, t_1) = r[[cσ​]](n,r,t0​,t1​)=r if n=0n = 0n=0, t0t_0t0​ if n>0n > 0n>0 is even, and t1t_1t1​ if nnn is odd,
    • ⟦Rσ⟧(g,h,n)=recNat(g,h,n)\llbracket R_\sigma \rrbracket(g, h, n) = \mathsf{recNat}(g, h, n)[[Rσ​]](g,h,n)=recNat(g,h,n).
  • SemEnv, updateSemEnv, defaultSem, castSem, and evalTerm: for an environment φ:∏x:Var⟦x.ty⟧\varphi : \prod_{x : \mathsf{Var}} \llbracket x.\mathsf{ty} \rrbracketφ:∏x:Var​[[x.ty]], evalTerm(t,τ,φ)∈⟦τ⟧\mathsf{evalTerm}(t, \tau, \varphi) \in \llbracket \tau \rrbracketevalTerm(t,τ,φ)∈[[τ]] evaluates ttt at target type τ\tauτ by structural recursion on ttt, erasing !!! and κ\kappaκ (⟦!r⟧φ=⟦r⟧φ\llbracket !r \rrbracket_\varphi = \llbracket r \rrbracket_\varphi[[!r]]φ​=[[r]]φ​ and ⟦rκ⟧φ=⟦r⟧φ\llbracket r\kappa \rrbracket_\varphi = \llbracket r \rrbracket_\varphi[[rκ]]φ​=[[r]]φ​) and returning the default value defaultSem(τ)\mathsf{defaultSem}(\tau)defaultSem(τ) (which is 000 at ι\iotaι and constant functions at arrow types) whenever ttt's syntactic form or variable/constant type does not match τ\tauτ. (Note: SemTy, evalConst, and evalTerm are not referenced in any of the theorem statements; recNat is used in evalBC.)

Human review
  • Endorsed by Shuze Chen · Nov 20, 2026

  • Endorsed by kmehltretter82 · Nov 20, 2026

    Confirmed by the mission captain (proposal self-audit).

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