Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

Sections 4-5: Safe/Complete Variables, Affinability, the Term Systems RA and RS, and Register Evaluation NFEval

Definition
BNS2000_Terms

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

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

Formalizes Sections 4-5 of Bellantoni, Niggl, and Schwichtenberg (2000): safe and complete variable occurrences (isSafeIn, isCompleteIn), affinable occurrences (IsAffinableIn), the term systems RA (IsRATerm) and RS (IsRSTerm), ground β_ι-expansions (BetaIotaExpandsTo), and the stack-of-registers evaluation judgment (t, n⃗) ↓^l_k v (NFEval).

Definition code
import Definitions.Def_BNS2000_Syntax

/-!
# RA-Terms, RS-Terms, Sharing, and Register-Machine Evaluation (Bellantoni–Niggl–Schwichtenberg 2000, Sections 3–5)

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 (Sections 3–5, pp. 20–27 / preprint pp. 4–11).

This module formalizes:
- Section 3 (pp. 20–22):
  - Subterm paths (`SubtermPath`), `subtermAt`, and binder scope equivalence (`ScopeEquivalentIn`, p. 20).
  - Affinations of an incomplete variable `x` in `s` (`IsAffination`, p. 20) and affinable variables
    (`IsAffinable`, p. 20).
  - The property `(R)` (`SatisfiesR`: every complete subterm contains complete free variables only),
    the property `(A)` (`SatisfiesA`: for every subterm `λx. s` with `x` incomplete, `x` is affinable
    in a reduct of `s`), and the class of ramified affinable terms (`IsRATerm`, p. 20).
- Section 4 (pp. 22–26):
  - Ground-type `β_ι`-expansion (`BetaIotaExpandsTo`, Lemma 4.2, p. 22).
  - The sharing property `(S)` (`SatisfiesS`), safe-or-ground variable discipline (`AllVarsSafeOrGround`),
    `R`-free terms (`isRFree`), and the class of `RS`-terms (`IsRSTerm`, p. 23).
  - Numeral environments (`IsNumeralEnv`, p. 23) and the inductive register-machine normalization
    algorithm `nf(t; n⃗; x⃗)` (`NFEval`, Theorem 4.3, pp. 23–24).
- Section 5 (pp. 26–27):
  - Bellantoni–Cook safe recursion function algebra `B` (`BCFunc`, `evalBC`, p. 27) and
    representation of two-sorted numerical functions by closed `RA`-terms (`RADenotesTwoSorted`).
-/

namespace BNS2000

/-- Path to a subterm occurrence in a term `t`:
- `0` descends into the body of `λx. r`, `!r`, or `rκ`, or the function part `r` of `r s`.
- `1` descends into the argument part `s` of `r s`. -/
abbrev SubtermPath := List Nat

/-- Subterm at occurrence path `p` in `t`. -/
def subtermAt : Term → SubtermPath → Option Term
  | t, [] => some t
  | .lam _ r, 0 :: p => subtermAt r p
  | .app r _, 0 :: p => subtermAt r p
  | .app _ s, 1 :: p => subtermAt s p
  | .bang r, 0 :: p => subtermAt r p
  | .kappa r, 0 :: p => subtermAt r p
  | _, _ => none

/-- Reflexive subterm relation `IsSubterm u t`. -/
def IsSubterm (u t : Term) : Prop :=
  ∃ p : SubtermPath, subtermAt t p = some u

/-- Whether path `pref` is a prefix of path `p` (meaning `p` lies inside the subterm at `pref`). -/
def isPathPrefix : SubtermPath → SubtermPath → Bool
  | [], _ => true
  | _ :: _, [] => false
  | i :: pref, j :: p => (i == j) && isPathPrefix pref p

/-- Strict prefix of a path. -/
def isStrictPathPrefix (pref p : SubtermPath) : Bool :=
  isPathPrefix pref p && (pref != p)

/-- Whether variable `x` is free along path `p` from the root of `t` (no `λx` binder along `p`). -/
def isFreeAlongPath (x : Var) : Term → SubtermPath → Bool
  | _, [] => true
  | .lam y r, 0 :: p => (y != x) && isFreeAlongPath x r p
  | .app r _, 0 :: p => isFreeAlongPath x r p
  | .app _ s, 1 :: p => isFreeAlongPath x s p
  | .bang r, 0 :: p => isFreeAlongPath x r p
  | .kappa r, 0 :: p => isFreeAlongPath x r p
  | _, _ => false

/-- All occurrence paths of free occurrences of `x` in `t`. -/
def freeVarPaths (x : Var) : Term → List SubtermPath
  | .var y => if y == x then [[]] else []
  | .const _ => []
  | .lam y r =>
      if y == x then [] else (freeVarPaths x r).map (0 :: ·)
  | .app r s =>
      (freeVarPaths x r).map (0 :: ·) ++ (freeVarPaths x s).map (1 :: ·)
  | .bang r => (freeVarPaths x r).map (0 :: ·)
  | .kappa r => (freeVarPaths x r).map (0 :: ·)

/-- Section 3 (p. 20): Two subterm occurrences at paths `p₁, p₂` in `t` (with subterms `a₁, a₂`)
are *scope equivalent* in `t` if whenever `λy` (at a binder path `bpath` in `t`) binds a
variable `y` free in either `a₁` or `a₂`, and `bpath` encloses one of `p₁, p₂`, then both
`p₁` and `p₂` lie within the scope of that `λy`. -/
def ScopeEquivalentIn (t : Term) (p₁ p₂ : SubtermPath) : Prop :=
  ∃ a₁ a₂ : Term,
    subtermAt t p₁ = some a₁ ∧
    subtermAt t p₂ = some a₂ ∧
    ∀ (bpath : SubtermPath) (y : Var) (body : Term),
      subtermAt t bpath = some (.lam y body) →
      (y ∈ freeVars a₁ ∨ y ∈ freeVars a₂) →
      (isStrictPathPrefix bpath p₁ = true ↔ isStrictPathPrefix bpath p₂ = true)

/-- Section 3 (p. 20): An *affination* of an incomplete variable `x` in `s` is a ground-type
subterm `a^τ` with `FO(x, a) = 1` such that every free occurrence of `x` in `s` lies in an
occurrence of `a` in `s`, where the occurrences of `a` are scope equivalent in `s`. -/
def IsAffination (x : Var) (s a : Term) : Prop :=
  x.ty.isIncomplete = true ∧
  (∃ τ : Ty, HasType a τ ∧ τ.isGround = true) ∧
  freeOccCount x a = 1 ∧
  ∃ occPaths : List SubtermPath,
    occPaths ≠ [] ∧
    (∀ p ∈ occPaths, subtermAt s p = some a ∧ isFreeAlongPath x s p = true) ∧
    (∀ px ∈ freeVarPaths x s, ∃ p ∈ occPaths, isPathPrefix p px = true) ∧
    (∀ p₁ ∈ occPaths, ∀ p₂ ∈ occPaths, ScopeEquivalentIn s p₁ p₂)

/-- Section 3 (p. 20): An incomplete variable `x` is *affinable* in `s` if there is an
affination of `x` in `s` or `FO(x, s) ≤ 1`. -/
def IsAffinable (x : Var) (s : Term) : Prop :=
  freeOccCount x s ≤ 1 ∨ ∃ a : Term, IsAffination x s a

/-- Property `(R)` (Section 3, p. 20): every complete subterm of `r` contains complete
free variables only. -/
def SatisfiesR (r : Term) : Prop :=
  ∀ u : Term,
    IsSubterm u r →
    (inferTy u).isComplete = true →
    ∀ v ∈ freeVars u, v.ty.isComplete = true

/-- Property `(A)` (Section 3, p. 20): for every subterm `λx. s` of `r` with `x` incomplete,
the variable `x` is affinable in a reduct `s'` of `s` (`s →* s'`). -/
def SatisfiesA (r : Term) : Prop :=
  ∀ (x : Var) (s : Term),
    IsSubterm (.lam x s) r →
    x.ty.isIncomplete = true →
    ∃ s' : Term, ReducesTo s s' ∧ IsAffinable x s'

/-- Direct affinability (used in Lemma 4.2, p. 22): for every subterm `λx. s` of `t` with
`x` higher-type incomplete, `x` is affinable in `s` itself. -/
def SatisfiesDirectAffinability (t : Term) : Prop :=
  ∀ (x : Var) (s : Term),
    IsSubterm (.lam x s) t →
    x.ty.isHigherType = true →
    x.ty.isIncomplete = true →
    IsAffinable x s

/-- Section 3 (p. 20): A term `r` is an `RA`-term (`R` for ramified, `A` for affinable)
if it is well-typed and satisfies both `(R)` and `(A)`. -/
def IsRATerm (r : Term) : Prop :=
  (∃ τ : Ty, HasType r τ) ∧ SatisfiesR r ∧ SatisfiesA r

/-- Property that every higher-type incomplete bound variable `x` in `t` has at most one
free occurrence in its scope `s` (`FO(x, s) ≤ 1`, Lemma 4.2, p. 22). -/
def HasLinearHigherIncompleteBinders (t : Term) : Prop :=
  ∀ (x : Var) (s : Term),
    IsSubterm (.lam x s) t →
    x.ty.isHigherType = true →
    x.ty.isIncomplete = true →
    freeOccCount x s ≤ 1

/-- One-step ground-type `β_ι`-expansion `r[a/y^τ] ↦ (λy^τ. r) a` inside a context, where `τ`
is a ground type (Lemma 4.2, p. 22). -/
inductive BetaIotaExpandStep : Term → Term → Prop where
  | root (y : Var) (r a : Term) :
      y.ty.isGround = true →
      BetaIotaExpandStep (substTerm r y a) (.app (.lam y r) a)
  | lam (x : Var) {r r' : Term} :
      BetaIotaExpandStep r r' →
      BetaIotaExpandStep (.lam x r) (.lam x r')
  | appLeft {r r' s : Term} :
      BetaIotaExpandStep r r' →
      BetaIotaExpandStep (.app r s) (.app r' s)
  | appRight {r s s' : Term} :
      BetaIotaExpandStep s s' →
      BetaIotaExpandStep (.app r s) (.app r s')
  | bang {r r' : Term} :
      BetaIotaExpandStep r r' →
      BetaIotaExpandStep (.bang r) (.bang r')
  | kappa {r r' : Term} :
      BetaIotaExpandStep r r' →
      BetaIotaExpandStep (.kappa r) (.kappa r')

/-- Reflexive-transitive closure of ground-type `β_ι`-expansions (`t ↦^*_{β_ι} t'`, Lemma 4.2, p. 22). -/
inductive BetaIotaExpandsTo : Term → Term → Prop where
  | refl (t : Term) : BetaIotaExpandsTo t t
  | step {t u v : Term} :
      BetaIotaExpandStep t u →
      BetaIotaExpandsTo u v →
      BetaIotaExpandsTo t v

/-- Every variable occurring (free or bound) in `t` has safe or ground type (Lemma 4.1 & Section 4, p. 22). -/
def AllVarsSafeOrGround (t : Term) : Prop :=
  ∀ v ∈ allVars t, v.ty.isSafe = true ∨ v.ty.isGround = true

/-- Total number of occurrences (free or bound) of variable `x` in `t`. -/
def totalVarOccCount (x : Var) : Term → Nat
  | .var y => if y == x then 1 else 0
  | .const _ => 0
  | .lam y r => (if y == x then 1 else 0) + totalVarOccCount x r
  | .app r s => totalVarOccCount x r + totalVarOccCount x s
  | .bang r => totalVarOccCount x r
  | .kappa r => totalVarOccCount x r

/-- Property `(S)` (Section 4, p. 23): every higher-type variable occurs at most once in `t`
(each free higher-type variable has `FO(v, t) ≤ 1`, and for every subterm `λx. s` with `x`
higher-type, `FO(x, s) ≤ 1`). -/
def SatisfiesS (t : Term) : Prop :=
  (∀ v ∈ freeVars t, v.ty.isHigherType = true → freeOccCount v t ≤ 1) ∧
  (∀ (x : Var) (s : Term),
    IsSubterm (.lam x s) t →
    x.ty.isHigherType = true →
    freeOccCount x s ≤ 1)

/-- Section 4 (p. 23): An `RA`-term is an `RS`-term (`S` for sharing) if it has safe or
ground variables only, and `(S)` every higher-type variable occurs at most once. -/
def IsRSTerm (t : Term) : Prop :=
  IsRATerm t ∧ AllVarsSafeOrGround t ∧ SatisfiesS t

/-- Whether a term `t` is `R`-free (contains no recursor constant `R_σ`, Section 4, p. 24). -/
def isRFree : Term → Bool
  | .var _ => true
  | .const (.recursor _) => false
  | .const _ => true
  | .lam _ r => isRFree r
  | .app r s => isRFree r && isRFree s
  | .bang r => isRFree r
  | .kappa r => isRFree r

/-- An environment `n⃗; x⃗ := [(x₁, n₁), ..., (xₖ, nₖ)]` is a *numeral environment*
(Section 4, p. 23) if each `nᵢ` is a numeral of the same type as `xᵢ`. -/
def IsNumeralEnv (env : List (Var × Term)) : Prop :=
  (env.map Prod.fst).Nodup ∧
  ∀ p ∈ env, HasType p.2 p.1.ty ∧ isNumeral p.2 = true

/-- An environment `n⃗; x⃗` of closed ground-type `R`-free `RS`-terms (Theorem 4.5, p. 24). -/
def IsClosedGroundRFreeRSEnv (env : List (Var × Term)) : Prop :=
  (env.map Prod.fst).Nodup ∧
  ∀ p ∈ env,
    HasType p.2 p.1.ty ∧
    p.1.ty.isGround = true ∧
    IsClosed p.2 ∧
    isRFree p.2 = true ∧
    IsRSTerm p.2

/-- Maximum term length `max |nᵢ|` among terms in an environment `env`. -/
def maxEnvTermLength (env : List (Var × Term)) : Nat :=
  env.foldr (fun p acc => Nat.max (termLength p.2) acc) 0

/-- Sum of term lengths `∑ |nᵢ|` across an environment `env`. -/
def sumEnvTermLength (env : List (Var × Term)) : Nat :=
  env.foldr (fun p acc => termLength p.2 + acc) 0

/-- Apply `k` postfixed `κ` eliminations `t κ ... κ` (Section 4, p. 24). -/
def kappaRepeat : Nat → Term → Term
  | 0, t => t
  | k + 1, t => .kappa (kappaRepeat k t)

/-- Inductive operational specification of the register-machine algorithm `nf(t; n⃗; x⃗)`
for `R`-free `RS`-terms `t` of ground type (proof of Theorem 4.3, Section 4, p. 24),
recording `(steps, regs, maxLen)`:
- `zero`: `t = 0` outputs `0` in `2` steps, `1` register, max length `1`.
- `varKappa`: `t = xᵢ κ ... κ` (`k` times) with `(xᵢ, !^k m) ∈ env` outputs `m` in `k + 2` steps,
  using `min 2 (|t| + #n⃗)` registers, max length `max |t| |!^k m|`.
- `s1`: `t = s₁ r` computes `n := nf(r; n⃗; x⃗)` and forms `s₁ n` in `2 + s_r` steps.
- `bang`: `t = !r` computes `n := nf(r; n⃗; x⃗)` and forms `!n` in `2 + s_r` steps.
- `s0`: `t = s₀ r` computes `n := nf(r; n⃗; x⃗)` and outputs `0` if `n = 0` else `s₀ n` in `3 + s_r` steps.
- `pred`: `t = p r` computes `n := nf(r; n⃗; x⃗)` and outputs `n'` if `n = sᵢ n'` else `0` in `3 + s_r` steps.
- `bangKappa`: `t = ((!s)κ) r⃗` computes `nf(s r⃗; n⃗; x⃗)` in `4 + s` steps.
- `cases`: `t = c_σ t₁ t₂ t₃ t₄ r⃗` computes `n := nf(t₁; n⃗; x⃗)` and then `nf(tⱼ r⃗; n⃗; x⃗)` in `2 + s₁ + sⱼ` steps.
- `betaGround`: `t = (λx. s) r r⃗` with `FO(x, s) > 1` (and `x` ground) computes `n := nf(r; n⃗; x⃗)`
  and then `nf(s r⃗; n⃗, n; x⃗, x)` in `2 + s_r + s_{s r⃗}` steps.
- `betaLinear`: `t = (λx. s) r r⃗` with `FO(x, s) ≤ 1` computes `nf(s[r/x] r⃗; n⃗; x⃗)` in `1 + s'` steps. -/
inductive NFEval : Term → List (Var × Term) → Term → Nat → Nat → Nat → Prop where
  | zero (env : List (Var × Term)) :
      NFEval (.const .zero) env (.const .zero) 2 1 1
  | varKappa (x : Var) (k : Nat) (m : Term) (env : List (Var × Term)) :
      (x, bangRepeat k m) ∈ env →
      isNumeral m = true →
      NFEval
        (kappaRepeat k (.var x))
        env
        m
        (k + 2)
        (Nat.min 2 (termLength (kappaRepeat k (.var x)) + env.length))
        (Nat.max (termLength (kappaRepeat k (.var x))) (termLength (bangRepeat k m)))
  | s1 {r n : Term} {env : List (Var × Term)} {sr gr lr : Nat} :
      NFEval r env n sr gr lr →
      NFEval
        (.app (.const .s1) r)
        env
        (.app (.const .s1) n)
        (2 + sr)
        (1 + gr)
        (Nat.max lr (termLength (.app (.const .s1) n)))
  | bang {r n : Term} {env : List (Var × Term)} {sr gr lr : Nat} :
      NFEval r env n sr gr lr →
      NFEval
        (.bang r)
        env
        (.bang n)
        (2 + sr)
        (1 + gr)
        (Nat.max lr (termLength (.bang n)))
  | s0 {r n out : Term} {env : List (Var × Term)} {sr gr lr : Nat} :
      NFEval r env n sr gr lr →
      out = (if n == .const .zero then .const .zero else .app (.const .s0) n) →
      NFEval
        (.app (.const .s0) r)
        env
        out
        (3 + sr)
        (1 + gr)
        (Nat.max lr (termLength out))
  | pred {r n out : Term} {env : List (Var × Term)} {sr gr lr : Nat} :
      NFEval r env n sr gr lr →
      ((n = .const .zero ∧ out = .const .zero) ∨
       (n = .app (.const .s0) out) ∨
       (n = .app (.const .s1) out)) →
      NFEval
        (.app (.const .pred) r)
        env
        out
        (3 + sr)
        (1 + gr)
        (Nat.max lr (termLength out))
  | bangKappa {s out : Term} (rs : List Term) {env : List (Var × Term)} {st gt lt : Nat} :
      NFEval (Term.appMany s rs) env out st gt lt →
      NFEval
        (Term.appMany (.kappa (.bang s)) rs)
        env
        out
        (4 + st)
        gt
        (Nat.max (termLength (Term.appMany (.kappa (.bang s)) rs)) lt)
  | cases (σ : Ty) {t₁ t₂ t₃ t₄ n tj out : Term} (rs : List Term)
      {env : List (Var × Term)} {s₁ g₁ l₁ sj gj lj : Nat} :
      NFEval t₁ env n s₁ g₁ l₁ →
      ((n = .const .zero ∧ tj = t₂) ∨
       (∃ n', n = .app (.const .s0) n' ∧ tj = t₃) ∨
       (∃ n', n = .app (.const .s1) n' ∧ tj = t₄)) →
      NFEval (Term.appMany tj rs) env out sj gj lj →
      NFEval
        (Term.appMany (.app (.app (.app (.app (.const (.cases σ)) t₁) t₂) t₃) t₄) rs)
        env
        out
        (2 + s₁ + sj)
        (1 + Nat.max g₁ gj)
        (Nat.max l₁ lj)
  | betaGround (x : Var) {s r n out : Term} (rs : List Term)
      {env : List (Var × Term)} {sr gr lr ss gs ls : Nat} :
      freeOccCount x s > 1 →
      x.ty.isGround = true →
      NFEval r env n sr gr lr →
      NFEval (Term.appMany s rs) ((x, n) :: env) out ss gs ls →
      NFEval
        (Term.appMany (.app (.lam x s) r) rs)
        env
        out
        (2 + sr + ss)
        (1 + Nat.max gr gs)
        (Nat.max lr ls)
  | betaLinear (x : Var) {s r out : Term} (rs : List Term)
      {env : List (Var × Term)} {st gt lt : Nat} :
      freeOccCount x s ≤ 1 →
      NFEval (Term.appMany (substTerm s x r) rs) env out st gt lt →
      NFEval
        (Term.appMany (.app (.lam x s) r) rs)
        env
        out
        (1 + st)
        gt
        (Nat.max (termLength (Term.appMany (.app (.lam x s) r) rs)) lt)

/-- Univariate polynomial `p : Nat → Nat` with natural number coefficients. -/
inductive IsUnivariatePoly : (Nat → Nat) → Prop where
  | const (c : Nat) : IsUnivariatePoly (fun _ => c)
  | id : IsUnivariatePoly (fun m => m)
  | add {p q : Nat → Nat} : IsUnivariatePoly p → IsUnivariatePoly q → IsUnivariatePoly (fun m => p m + q m)
  | mul {p q : Nat → Nat} : IsUnivariatePoly p → IsUnivariatePoly q → IsUnivariatePoly (fun m => p m * q m)

/-- Bellantoni–Cook safe recursion function algebra `B` (Section 5, p. 27, citing [1]):
Every function `f(x⃗; y⃗)` has `m` normal inputs `x⃗` and `n` safe inputs `y⃗`. -/
inductive BCFunc : Nat → Nat → Type where
  | zero : BCFunc 0 0
  | s0 : BCFunc 0 1
  | s1 : BCFunc 0 1
  | pred : BCFunc 0 1
  | cases : BCFunc 0 4
  | projNormal {m n : Nat} (j : Fin m) : BCFunc m n
  | projSafe {m n : Nat} (j : Fin n) : BCFunc m n
  | safeComp {m n m' n' : Nat} :
      BCFunc m' n' →
      (Fin m' → BCFunc m 0) →
      (Fin n' → BCFunc m n) →
      BCFunc m n
  | safeRec {m n : Nat} :
      BCFunc m n →
      BCFunc (m + 1) (n + 1) →
      BCFunc (m + 1) (n + 1) →
      BCFunc (m + 1) n

/-- Standard numerical semantics `evalBC f x⃗ y⃗` of a Bellantoni–Cook function `f : BCFunc m n` (p. 27). -/
def evalBC : {m n : Nat} → BCFunc m n → (Fin m → Nat) → (Fin n → Nat) → Nat
  | 0, 0, .zero, _, _ => 0
  | 0, 1, .s0, _, ys => 2 * ys ⟨0, by decide⟩
  | 0, 1, .s1, _, ys => 2 * ys ⟨0, by decide⟩ + 1
  | 0, 1, .pred, _, ys => ys ⟨0, by decide⟩ / 2
  | 0, 4, .cases, _, ys =>
      let y0 := ys ⟨0, by decide⟩
      let y1 := ys ⟨1, by decide⟩
      let y2 := ys ⟨2, by decide⟩
      let y3 := ys ⟨3, by decide⟩
      if y0 == 0 then y1 else if y0 % 2 == 0 then y2 else y3
  | _, _, .projNormal j, xs, _ => xs j
  | _, _, .projSafe j, _, ys => ys j
  | _, _, .safeComp g gs hs, xs, ys =>
      evalBC g (fun i => evalBC (gs i) xs Fin.elim0) (fun j => evalBC (hs j) xs ys)
  | _, _, .safeRec g h0 h1, xs, ys =>
      let x0 := xs ⟨0, by omega⟩
      let xsTail : Fin _ → Nat := fun i => xs ⟨i.val + 1, by omega⟩
      recNat
        (evalBC g xsTail ys)
        (fun cur prev =>
          let half := cur / 2
          let xsRec : Fin _ → Nat := fun i =>
            if _h0 : i.val = 0 then half else xsTail ⟨i.val - 1, by omega⟩
          let ysRec : Fin _ → Nat := fun j =>
            if hj : j.val < _ then ys ⟨j.val, hj⟩ else prev
          if cur % 2 == 0 then evalBC h0 xsRec ysRec else evalBC h1 xsRec ysRec)
        x0

/-- A closed `RA`-term `t` of type `!ι → ... → !ι → ι → ... → ι → ι` (`m` normal inputs of type `!ι`
and `n` safe inputs of type `ι`) denotes a numerical function `f : (Fin m → Nat) → (Fin n → Nat) → Nat`
if for all `xs : Fin m → Nat` and `ys : Fin n → Nat`, applying `t` to `!(binaryNumeralOfNat (xs i))`
and `binaryNumeralOfNat (ys j)` reduces to `binaryNumeralOfNat (f xs ys)` (Section 5, pp. 26–27). -/
def RADenotesTwoSorted {m n : Nat} (t : Term) (f : (Fin m → Nat) → (Fin n → Nat) → Nat) : Prop :=
  let domTys := List.replicate m (.bang .iota) ++ List.replicate n .iota
  IsRATerm t ∧
  IsClosed t ∧
  HasType t (Ty.arrMany domTys .iota) ∧
  ∀ (xs : Fin m → Nat) (ys : Fin n → Nat),
    let normArgs := (List.finRange m).map (fun i => .bang (binaryNumeralOfNat (xs i)))
    let safeArgs := (List.finRange n).map (fun j => binaryNumeralOfNat (ys j))
    IsNormalFormOf
      (Term.appMany t (normArgs ++ safeArgs))
      (binaryNumeralOfNat (f xs ys))

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 4-5, pp. 21-26.
Read-back

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

Read-back: Def_BNS2000_Terms.lean

1. Subterm Paths and Scope Equivalence (SubtermPath, subtermAt, IsSubterm, isFreeAlongPath, freeVarPaths, ScopeEquivalentIn)

  • SubtermPath: an alias for List(N)\mathsf{List}(\mathbb{N})List(N).

  • subtermAt: given t∈Termt \in \mathsf{Term}t∈Term and a path p∈SubtermPathp \in \mathsf{SubtermPath}p∈SubtermPath, returns an optional subterm subtermAt(t,p)∈Option(Term)\mathsf{subtermAt}(t, p) \in \mathsf{Option}(\mathsf{Term})subtermAt(t,p)∈Option(Term):

    • subtermAt(t,[])=some(t)\mathsf{subtermAt}(t, []) = \mathsf{some}(t)subtermAt(t,[])=some(t);
    • subtermAt(λx. r,0::p)=subtermAt(!r,0::p)=subtermAt(rκ,0::p)=subtermAt(r,p)\mathsf{subtermAt}(\lambda x.\, r, 0 :: p) = \mathsf{subtermAt}(!r, 0 :: p) = \mathsf{subtermAt}(r\kappa, 0 :: p) = \mathsf{subtermAt}(r, p)subtermAt(λx.r,0::p)=subtermAt(!r,0::p)=subtermAt(rκ,0::p)=subtermAt(r,p);
    • subtermAt(r s,0::p)=subtermAt(r,p)\mathsf{subtermAt}(r\, s, 0 :: p) = \mathsf{subtermAt}(r, p)subtermAt(rs,0::p)=subtermAt(r,p) and subtermAt(r s,1::p)=subtermAt(s,p)\mathsf{subtermAt}(r\, s, 1 :: p) = \mathsf{subtermAt}(s, p)subtermAt(rs,1::p)=subtermAt(s,p);
    • for all other combinations, subtermAt(t,p)=none\mathsf{subtermAt}(t, p) = \mathsf{none}subtermAt(t,p)=none.
  • IsSubterm: for u,t∈Termu, t \in \mathsf{Term}u,t∈Term, IsSubterm(u,t)  ⟺  ∃p∈SubtermPath,  subtermAt(t,p)=some(u)\mathsf{IsSubterm}(u, t) \iff \exists p \in \mathsf{SubtermPath},\; \mathsf{subtermAt}(t, p) = \mathsf{some}(u)IsSubterm(u,t)⟺∃p∈SubtermPath,subtermAt(t,p)=some(u).

  • isPathPrefix and isStrictPathPrefix: isPathPrefix(pref,p)=true\mathsf{isPathPrefix}(\mathsf{pref}, p) = \mathsf{true}isPathPrefix(pref,p)=true iff the list pref\mathsf{pref}pref is a prefix of ppp; isStrictPathPrefix(pref,p)=true\mathsf{isStrictPathPrefix}(\mathsf{pref}, p) = \mathsf{true}isStrictPathPrefix(pref,p)=true iff pref\mathsf{pref}pref is a prefix of ppp and pref≠p\mathsf{pref} \ne ppref=p.

  • isFreeAlongPath: for x∈Varx \in \mathsf{Var}x∈Var, t∈Termt \in \mathsf{Term}t∈Term, and p∈SubtermPathp \in \mathsf{SubtermPath}p∈SubtermPath, returns true\mathsf{true}true if and only if ppp is a valid path in ttt and no λ\lambdaλ-abstraction encountered strictly along ppp binds xxx.

  • freeVarPaths: for x∈Varx \in \mathsf{Var}x∈Var and t∈Termt \in \mathsf{Term}t∈Term, returns the list of all paths p∈SubtermPathp \in \mathsf{SubtermPath}p∈SubtermPath to free variable leaves var(x)\mathsf{var}(x)var(x) in ttt.

  • ScopeEquivalentIn: for t∈Termt \in \mathsf{Term}t∈Term and p1,p2∈SubtermPathp_1, p_2 \in \mathsf{SubtermPath}p1​,p2​∈SubtermPath, ScopeEquivalentIn(t,p1,p2)\mathsf{ScopeEquivalentIn}(t, p_1, p_2)ScopeEquivalentIn(t,p1​,p2​) holds if and only if there exist a1,a2∈Terma_1, a_2 \in \mathsf{Term}a1​,a2​∈Term such that:

    1. subtermAt(t,p1)=some(a1)\mathsf{subtermAt}(t, p_1) = \mathsf{some}(a_1)subtermAt(t,p1​)=some(a1​) and subtermAt(t,p2)=some(a2)\mathsf{subtermAt}(t, p_2) = \mathsf{some}(a_2)subtermAt(t,p2​)=some(a2​); and
    2. for every path bpath∈SubtermPath\mathsf{bpath} \in \mathsf{SubtermPath}bpath∈SubtermPath, variable y∈Vary \in \mathsf{Var}y∈Var, and term body∈Term\mathsf{body} \in \mathsf{Term}body∈Term such that subtermAt(t,bpath)=some(λy. body)\mathsf{subtermAt}(t, \mathsf{bpath}) = \mathsf{some}(\lambda y.\, \mathsf{body})subtermAt(t,bpath)=some(λy.body) and y∈freeVars(a1)∪freeVars(a2)y \in \mathsf{freeVars}(a_1) \cup \mathsf{freeVars}(a_2)y∈freeVars(a1​)∪freeVars(a2​),
isStrictPathPrefix(bpath,p1)=true  ⟺  isStrictPathPrefix(bpath,p2)=true.\mathsf{isStrictPathPrefix}(\mathsf{bpath}, p_1) = \mathsf{true} \iff \mathsf{isStrictPathPrefix}(\mathsf{bpath}, p_2) = \mathsf{true}.isStrictPathPrefix(bpath,p1​)=true⟺isStrictPathPrefix(bpath,p2​)=true.

2. Affinations, Properties (R) and (A), and RA-Terms (IsAffination, IsAffinable, SatisfiesR, SatisfiesA, SatisfiesDirectAffinability, IsRATerm)

  • IsAffination: for x∈Varx \in \mathsf{Var}x∈Var and s,a∈Terms, a \in \mathsf{Term}s,a∈Term, IsAffination(x,s,a)\mathsf{IsAffination}(x, s, a)IsAffination(x,s,a) holds if and only if:

    1. x.tyx.\mathsf{ty}x.ty is incomplete (isIncomplete(x.ty)=true\mathsf{isIncomplete}(x.\mathsf{ty}) = \mathsf{true}isIncomplete(x.ty)=true);
    2. there exists a ground type τ∈Ty\tau \in \mathsf{Ty}τ∈Ty (l(τ)=0l(\tau) = 0l(τ)=0) such that HasType(a,τ)\mathsf{HasType}(a, \tau)HasType(a,τ);
    3. FO(x,a)=1\mathsf{FO}(x, a) = 1FO(x,a)=1; and
    4. there exists a non-empty list of paths occPaths⊆SubtermPath\mathsf{occPaths} \subseteq \mathsf{SubtermPath}occPaths⊆SubtermPath such that:
      • for every p∈occPathsp \in \mathsf{occPaths}p∈occPaths, subtermAt(s,p)=some(a)\mathsf{subtermAt}(s, p) = \mathsf{some}(a)subtermAt(s,p)=some(a) and isFreeAlongPath(x,s,p)=true\mathsf{isFreeAlongPath}(x, s, p) = \mathsf{true}isFreeAlongPath(x,s,p)=true;
      • for every free occurrence path px∈freeVarPaths(x,s)p_x \in \mathsf{freeVarPaths}(x, s)px​∈freeVarPaths(x,s) of xxx in sss, there is some p∈occPathsp \in \mathsf{occPaths}p∈occPaths that is a prefix of pxp_xpx​; and
      • for all p1,p2∈occPathsp_1, p_2 \in \mathsf{occPaths}p1​,p2​∈occPaths, ScopeEquivalentIn(s,p1,p2)\mathsf{ScopeEquivalentIn}(s, p_1, p_2)ScopeEquivalentIn(s,p1​,p2​) holds.
  • IsAffinable: for x∈Varx \in \mathsf{Var}x∈Var and s∈Terms \in \mathsf{Term}s∈Term,

IsAffinable(x,s)  ⟺  FO(x,s)≤1  ∨  ∃a∈Term,  IsAffination(x,s,a).\mathsf{IsAffinable}(x, s) \iff \mathsf{FO}(x, s) \le 1 \;\vee\; \exists a \in \mathsf{Term},\; \mathsf{IsAffination}(x, s, a).IsAffinable(x,s)⟺FO(x,s)≤1∨∃a∈Term,IsAffination(x,s,a).

Edge case note: The disjunct FO(x,s)≤1\mathsf{FO}(x, s) \le 1FO(x,s)≤1 does not itself constrain x.tyx.\mathsf{ty}x.ty to be incomplete, whereas IsAffination(x,s,a)\mathsf{IsAffination}(x, s, a)IsAffination(x,s,a) requires isIncomplete(x.ty)=true\mathsf{isIncomplete}(x.\mathsf{ty}) = \mathsf{true}isIncomplete(x.ty)=true. Every definition and theorem in the bundle that invokes IsAffinable(x,s)\mathsf{IsAffinable}(x, s)IsAffinable(x,s) adds an explicit hypothesis isIncomplete(x.ty)=true\mathsf{isIncomplete}(x.\mathsf{ty}) = \mathsf{true}isIncomplete(x.ty)=true.

  • SatisfiesR: for r∈Termr \in \mathsf{Term}r∈Term, SatisfiesR(r)\mathsf{SatisfiesR}(r)SatisfiesR(r) holds if and only if for every subterm uuu of rrr (IsSubterm(u,r)\mathsf{IsSubterm}(u, r)IsSubterm(u,r)), if isComplete(inferTy(u))=true\mathsf{isComplete}(\mathsf{inferTy}(u)) = \mathsf{true}isComplete(inferTy(u))=true, then every free variable v∈freeVars(u)v \in \mathsf{freeVars}(u)v∈freeVars(u) satisfies isComplete(v.ty)=true\mathsf{isComplete}(v.\mathsf{ty}) = \mathsf{true}isComplete(v.ty)=true.

  • SatisfiesA: for r∈Termr \in \mathsf{Term}r∈Term, SatisfiesA(r)\mathsf{SatisfiesA}(r)SatisfiesA(r) holds if and only if for every x∈Varx \in \mathsf{Var}x∈Var and s∈Terms \in \mathsf{Term}s∈Term such that λx. s\lambda x.\, sλx.s is a subterm of rrr and isIncomplete(x.ty)=true\mathsf{isIncomplete}(x.\mathsf{ty}) = \mathsf{true}isIncomplete(x.ty)=true, there exists s′∈Terms' \in \mathsf{Term}s′∈Term such that s→∗s′s \to^* s's→∗s′ (ReducesTo(s,s′)\mathsf{ReducesTo}(s, s')ReducesTo(s,s′)) and IsAffinable(x,s′)\mathsf{IsAffinable}(x, s')IsAffinable(x,s′).

  • SatisfiesDirectAffinability: for t∈Termt \in \mathsf{Term}t∈Term, SatisfiesDirectAffinability(t)\mathsf{SatisfiesDirectAffinability}(t)SatisfiesDirectAffinability(t) holds if and only if for every x∈Varx \in \mathsf{Var}x∈Var and s∈Terms \in \mathsf{Term}s∈Term such that λx. s\lambda x.\, sλx.s is a subterm of ttt, l(x.ty)≥1l(x.\mathsf{ty}) \ge 1l(x.ty)≥1 (isHigherType(x.ty)=true\mathsf{isHigherType}(x.\mathsf{ty}) = \mathsf{true}isHigherType(x.ty)=true), and isIncomplete(x.ty)=true\mathsf{isIncomplete}(x.\mathsf{ty}) = \mathsf{true}isIncomplete(x.ty)=true, IsAffinable(x,s)\mathsf{IsAffinable}(x, s)IsAffinable(x,s) holds directly in sss.

  • IsRATerm: for r∈Termr \in \mathsf{Term}r∈Term,

IsRATerm(r)  ⟺  (∃τ∈Ty,  HasType(r,τ))∧SatisfiesR(r)∧SatisfiesA(r).\mathsf{IsRATerm}(r) \iff \bigl(\exists \tau \in \mathsf{Ty},\; \mathsf{HasType}(r, \tau)\bigr) \wedge \mathsf{SatisfiesR}(r) \wedge \mathsf{SatisfiesA}(r).IsRATerm(r)⟺(∃τ∈Ty,HasType(r,τ))∧SatisfiesR(r)∧SatisfiesA(r).

3. Sharing, Ground βι\beta_\iotaβι​-Expansion, and RS-Terms (HasLinearHigherIncompleteBinders, BetaIotaExpandStep, BetaIotaExpandsTo, AllVarsSafeOrGround, SatisfiesS, IsRSTerm, isRFree)

  • HasLinearHigherIncompleteBinders: for t∈Termt \in \mathsf{Term}t∈Term, holds if and only if for every x∈Varx \in \mathsf{Var}x∈Var and s∈Terms \in \mathsf{Term}s∈Term such that λx. s\lambda x.\, sλx.s is a subterm of ttt, l(x.ty)≥1l(x.\mathsf{ty}) \ge 1l(x.ty)≥1, and isIncomplete(x.ty)=true\mathsf{isIncomplete}(x.\mathsf{ty}) = \mathsf{true}isIncomplete(x.ty)=true, we have FO(x,s)≤1\mathsf{FO}(x, s) \le 1FO(x,s)≤1.

  • BetaIotaExpandStep and BetaIotaExpandsTo: BetaIotaExpandStep\mathsf{BetaIotaExpandStep}BetaIotaExpandStep is the contextual closure (under λx. [⋅]\lambda x.\, [\cdot]λx.[⋅], [⋅] s[\cdot]\, s[⋅]s, r [⋅]r\, [\cdot]r[⋅], ![⋅]![\cdot]![⋅], and [⋅]κ[\cdot]\kappa[⋅]κ) of the root rule

r[a/y]↦βι(λy. r) awhenever isGround(y.ty)=true.r[a/y] \mapsto_{\beta_\iota} (\lambda y.\, r)\, a \qquad \text{whenever } \mathsf{isGround}(y.\mathsf{ty}) = \mathsf{true}.r[a/y]↦βι​​(λy.r)awhenever isGround(y.ty)=true.

BetaIotaExpandsTo\mathsf{BetaIotaExpandsTo}BetaIotaExpandsTo is the reflexive-transitive closure of BetaIotaExpandStep\mathsf{BetaIotaExpandStep}BetaIotaExpandStep. Edge case note: The root rule of BetaIotaExpandStep requires l(y.ty)=0l(y.\mathsf{ty}) = 0l(y.ty)=0, but does not require HasType(a,y.ty)\mathsf{HasType}(a, y.\mathsf{ty})HasType(a,y.ty) or y∈freeVars(r)y \in \mathsf{freeVars}(r)y∈freeVars(r).

  • AllVarsSafeOrGround: for t∈Termt \in \mathsf{Term}t∈Term, holds if and only if every variable v∈allVars(t)v \in \mathsf{allVars}(t)v∈allVars(t) (both free and bound) satisfies isSafe(v.ty)=true\mathsf{isSafe}(v.\mathsf{ty}) = \mathsf{true}isSafe(v.ty)=true or isGround(v.ty)=true\mathsf{isGround}(v.\mathsf{ty}) = \mathsf{true}isGround(v.ty)=true.

  • totalVarOccCount: counts all occurrences of xxx in ttt, adding 111 at each binder λx\lambda xλx and 111 at each leaf var(x)\mathsf{var}(x)var(x). (Note: totalVarOccCount is not referenced anywhere else in the bundle.)

  • SatisfiesS: for t∈Termt \in \mathsf{Term}t∈Term, holds if and only if:

    1. for every v∈freeVars(t)v \in \mathsf{freeVars}(t)v∈freeVars(t) with l(v.ty)≥1l(v.\mathsf{ty}) \ge 1l(v.ty)≥1, FO(v,t)≤1\mathsf{FO}(v, t) \le 1FO(v,t)≤1; and
    2. for every x∈Varx \in \mathsf{Var}x∈Var and s∈Terms \in \mathsf{Term}s∈Term such that λx. s\lambda x.\, sλx.s is a subterm of ttt and l(x.ty)≥1l(x.\mathsf{ty}) \ge 1l(x.ty)≥1, FO(x,s)≤1\mathsf{FO}(x, s) \le 1FO(x,s)≤1.
  • IsRSTerm: for t∈Termt \in \mathsf{Term}t∈Term,

IsRSTerm(t)  ⟺  IsRATerm(t)∧AllVarsSafeOrGround(t)∧SatisfiesS(t).\mathsf{IsRSTerm}(t) \iff \mathsf{IsRATerm}(t) \wedge \mathsf{AllVarsSafeOrGround}(t) \wedge \mathsf{SatisfiesS}(t).IsRSTerm(t)⟺IsRATerm(t)∧AllVarsSafeOrGround(t)∧SatisfiesS(t).
  • isRFree: returns true\mathsf{true}true on t∈Termt \in \mathsf{Term}t∈Term if and only if ttt contains no constant of the form Const.recursor(σ)\mathsf{Const.recursor}(\sigma)Const.recursor(σ) (RσR_\sigmaRσ​).

4. Environments and Register-Machine Evaluation (IsNumeralEnv, IsClosedGroundRFreeRSEnv, maxEnvTermLength, sumEnvTermLength, kappaRepeat, NFEval)

  • IsNumeralEnv: for a list of variable-term bindings env=[(x1,n1),…,(xk,nk)]\mathsf{env} = [(x_1, n_1), \dots, (x_k, n_k)]env=[(x1​,n1​),…,(xk​,nk​)], IsNumeralEnv(env)\mathsf{IsNumeralEnv}(\mathsf{env})IsNumeralEnv(env) holds if and only if the variables [x1,…,xk][x_1, \dots, x_k][x1​,…,xk​] are pairwise distinct (Nodup\mathsf{Nodup}Nodup) and for every (xi,ni)∈env(x_i, n_i) \in \mathsf{env}(xi​,ni​)∈env, HasType(ni,xi.ty)\mathsf{HasType}(n_i, x_i.\mathsf{ty})HasType(ni​,xi​.ty) and isNumeral(ni)=true\mathsf{isNumeral}(n_i) = \mathsf{true}isNumeral(ni​)=true.

  • IsClosedGroundRFreeRSEnv: for env=[(x1,n1),…,(xk,nk)]\mathsf{env} = [(x_1, n_1), \dots, (x_k, n_k)]env=[(x1​,n1​),…,(xk​,nk​)], holds if and only if [x1,…,xk][x_1, \dots, x_k][x1​,…,xk​] are pairwise distinct and for every (xi,ni)∈env(x_i, n_i) \in \mathsf{env}(xi​,ni​)∈env:

HasType(ni,xi.ty)∧isGround(xi.ty)=true∧IsClosed(ni)∧isRFree(ni)=true∧IsRSTerm(ni).\mathsf{HasType}(n_i, x_i.\mathsf{ty}) \wedge \mathsf{isGround}(x_i.\mathsf{ty}) = \mathsf{true} \wedge \mathsf{IsClosed}(n_i) \wedge \mathsf{isRFree}(n_i) = \mathsf{true} \wedge \mathsf{IsRSTerm}(n_i).HasType(ni​,xi​.ty)∧isGround(xi​.ty)=true∧IsClosed(ni​)∧isRFree(ni​)=true∧IsRSTerm(ni​).
  • maxEnvTermLength and sumEnvTermLength: for env=[(x1,n1),…,(xk,nk)]\mathsf{env} = [(x_1, n_1), \dots, (x_k, n_k)]env=[(x1​,n1​),…,(xk​,nk​)],
maxEnvTermLength(env)=max⁡1≤i≤k∣ni∣(with 0 on []),sumEnvTermLength(env)=∑i=1k∣ni∣.\mathsf{maxEnvTermLength}(\mathsf{env}) = \max_{1 \le i \le k} |n_i| \quad (\text{with } 0 \text{ on } []), \qquad \mathsf{sumEnvTermLength}(\mathsf{env}) = \sum_{i=1}^k |n_i|.maxEnvTermLength(env)=1≤i≤kmax​∣ni​∣(with 0 on []),sumEnvTermLength(env)=i=1∑k​∣ni​∣.
  • kappaRepeat: kappaRepeat(k,t)\mathsf{kappaRepeat}(k, t)kappaRepeat(k,t) applies Term.kappa\mathsf{Term.kappa}Term.kappa kkk times to ttt, yielding tκ⋯κ⏟kt\underbrace{\kappa\cdots\kappa}_{k}tkκ⋯κ​​.

  • NFEval: an inductive 6-place relation NFEval(t,env,out,steps,regs,maxLen)\mathsf{NFEval}(t, \mathsf{env}, \mathsf{out}, \mathsf{steps}, \mathsf{regs}, \mathsf{maxLen})NFEval(t,env,out,steps,regs,maxLen) on Term×List(Var×Term)×Term×N×N×N\mathsf{Term} \times \mathsf{List}(\mathsf{Var} \times \mathsf{Term}) \times \mathsf{Term} \times \mathbb{N} \times \mathbb{N} \times \mathbb{N}Term×List(Var×Term)×Term×N×N×N defined by 10 rules:

    1. zero: NFEval(0,env,0,2,1,1)\mathsf{NFEval}(0, \mathsf{env}, 0, 2, 1, 1)NFEval(0,env,0,2,1,1).
    2. varKappa: if (x,!km)∈env(x, !^k m) \in \mathsf{env}(x,!km)∈env and isNumeral(m)=true\mathsf{isNumeral}(m) = \mathsf{true}isNumeral(m)=true, then
NFEval(xκk, env, m, k+2, min⁡(2,∣xκk∣+∣env∣), max⁡(∣xκk∣,∣!km∣)).\mathsf{NFEval}\bigl(x\kappa^k,\, \mathsf{env},\, m,\, k + 2,\, \min(2, |x\kappa^k| + |\mathsf{env}|),\, \max(|x\kappa^k|, |!^k m|)\bigr).NFEval(xκk,env,m,k+2,min(2,∣xκk∣+∣env∣),max(∣xκk∣,∣!km∣)).
 *(Note: Since $(x, !^k m) \in \mathsf{env}$, $|\mathsf{env}| \ge 1$ and $|x\kappa^k| = k + 1 \ge 1$, so $\min(2, |x\kappa^k| + |\mathsf{env}|)$ always equals $2$.)*

3. s1: if NFEval(r,env,n,sr,gr,lr)\mathsf{NFEval}(r, \mathsf{env}, n, s_r, g_r, l_r)NFEval(r,env,n,sr​,gr​,lr​), then

NFEval(s1 r, env, s1 n, 2+sr, 1+gr, max⁡(lr,∣s1 n∣)).\mathsf{NFEval}\bigl(s_1\, r,\, \mathsf{env},\, s_1\, n,\, 2 + s_r,\, 1 + g_r,\, \max(l_r, |s_1\, n|)\bigr).NFEval(s1​r,env,s1​n,2+sr​,1+gr​,max(lr​,∣s1​n∣)).
  1. bang: if NFEval(r,env,n,sr,gr,lr)\mathsf{NFEval}(r, \mathsf{env}, n, s_r, g_r, l_r)NFEval(r,env,n,sr​,gr​,lr​), then
NFEval(!r, env, !n, 2+sr, 1+gr, max⁡(lr,∣!n∣)).\mathsf{NFEval}\bigl(!r,\, \mathsf{env},\, !n,\, 2 + s_r,\, 1 + g_r,\, \max(l_r, |!n|)\bigr).NFEval(!r,env,!n,2+sr​,1+gr​,max(lr​,∣!n∣)).
  1. s0: if NFEval(r,env,n,sr,gr,lr)\mathsf{NFEval}(r, \mathsf{env}, n, s_r, g_r, l_r)NFEval(r,env,n,sr​,gr​,lr​) and out={0if n=0s0 nif n≠0\mathsf{out} = \begin{cases} 0 & \text{if } n = 0 \\ s_0\, n & \text{if } n \ne 0 \end{cases}out={0s0​n​if n=0if n=0​, then
NFEval(s0 r, env, out, 3+sr, 1+gr, max⁡(lr,∣out∣)).\mathsf{NFEval}\bigl(s_0\, r,\, \mathsf{env},\, \mathsf{out},\, 3 + s_r,\, 1 + g_r,\, \max(l_r, |\mathsf{out}|)\bigr).NFEval(s0​r,env,out,3+sr​,1+gr​,max(lr​,∣out∣)).
  1. pred: if NFEval(r,env,n,sr,gr,lr)\mathsf{NFEval}(r, \mathsf{env}, n, s_r, g_r, l_r)NFEval(r,env,n,sr​,gr​,lr​) and either (n=0∧out=0n = 0 \wedge \mathsf{out} = 0n=0∧out=0), or n=s0 outn = s_0\, \mathsf{out}n=s0​out, or n=s1 outn = s_1\, \mathsf{out}n=s1​out, then
NFEval(p r, env, out, 3+sr, 1+gr, max⁡(lr,∣out∣)).\mathsf{NFEval}\bigl(p\, r,\, \mathsf{env},\, \mathsf{out},\, 3 + s_r,\, 1 + g_r,\, \max(l_r, |\mathsf{out}|)\bigr).NFEval(pr,env,out,3+sr​,1+gr​,max(lr​,∣out∣)).
  1. bangKappa: if NFEval(appMany(s,r⃗),env,out,st,gt,lt)\mathsf{NFEval}(\mathsf{appMany}(s, \vec{r}), \mathsf{env}, \mathsf{out}, s_t, g_t, l_t)NFEval(appMany(s,r),env,out,st​,gt​,lt​), then
NFEval(appMany((!s)κ,r⃗), env, out, 4+st, gt, max⁡(∣appMany((!s)κ,r⃗)∣,lt)).\mathsf{NFEval}\bigl(\mathsf{appMany}((!s)\kappa, \vec{r}),\, \mathsf{env},\, \mathsf{out},\, 4 + s_t,\, g_t,\, \max(|\mathsf{appMany}((!s)\kappa, \vec{r})|, l_t)\bigr).NFEval(appMany((!s)κ,r),env,out,4+st​,gt​,max(∣appMany((!s)κ,r)∣,lt​)).
  1. cases: if NFEval(t1,env,n,s1,g1,l1)\mathsf{NFEval}(t_1, \mathsf{env}, n, s_1, g_1, l_1)NFEval(t1​,env,n,s1​,g1​,l1​), and either (n=0∧tj=t2n = 0 \wedge t_j = t_2n=0∧tj​=t2​), or (∃n′, n=s0 n′∧tj=t3\exists n',\, n = s_0\, n' \wedge t_j = t_3∃n′,n=s0​n′∧tj​=t3​), or (∃n′, n=s1 n′∧tj=t4\exists n',\, n = s_1\, n' \wedge t_j = t_4∃n′,n=s1​n′∧tj​=t4​), and NFEval(appMany(tj,r⃗),env,out,sj,gj,lj)\mathsf{NFEval}(\mathsf{appMany}(t_j, \vec{r}), \mathsf{env}, \mathsf{out}, s_j, g_j, l_j)NFEval(appMany(tj​,r),env,out,sj​,gj​,lj​), then
NFEval(appMany(cσ t1 t2 t3 t4,r⃗), env, out, 2+s1+sj, 1+max⁡(g1,gj), max⁡(l1,lj)).\mathsf{NFEval}\bigl(\mathsf{appMany}(c_\sigma\, t_1\, t_2\, t_3\, t_4, \vec{r}),\, \mathsf{env},\, \mathsf{out},\, 2 + s_1 + s_j,\, 1 + \max(g_1, g_j),\, \max(l_1, l_j)\bigr).NFEval(appMany(cσ​t1​t2​t3​t4​,r),env,out,2+s1​+sj​,1+max(g1​,gj​),max(l1​,lj​)).
  1. betaGround: if FO(x,s)>1\mathsf{FO}(x, s) > 1FO(x,s)>1, isGround(x.ty)=true\mathsf{isGround}(x.\mathsf{ty}) = \mathsf{true}isGround(x.ty)=true, NFEval(r,env,n,sr,gr,lr)\mathsf{NFEval}(r, \mathsf{env}, n, s_r, g_r, l_r)NFEval(r,env,n,sr​,gr​,lr​), and NFEval(appMany(s,r⃗),(x,n)::env,out,ss,gs,ls)\mathsf{NFEval}(\mathsf{appMany}(s, \vec{r}), (x, n) :: \mathsf{env}, \mathsf{out}, s_s, g_s, l_s)NFEval(appMany(s,r),(x,n)::env,out,ss​,gs​,ls​), then
NFEval(appMany((λx. s) r,r⃗), env, out, 2+sr+ss, 1+max⁡(gr,gs), max⁡(lr,ls)).\mathsf{NFEval}\bigl(\mathsf{appMany}((\lambda x.\, s)\, r, \vec{r}),\, \mathsf{env},\, \mathsf{out},\, 2 + s_r + s_s,\, 1 + \max(g_r, g_s),\, \max(l_r, l_s)\bigr).NFEval(appMany((λx.s)r,r),env,out,2+sr​+ss​,1+max(gr​,gs​),max(lr​,ls​)).
  1. betaLinear: if FO(x,s)≤1\mathsf{FO}(x, s) \le 1FO(x,s)≤1 and NFEval(appMany(s[r/x],r⃗),env,out,st,gt,lt)\mathsf{NFEval}(\mathsf{appMany}(s[r/x], \vec{r}), \mathsf{env}, \mathsf{out}, s_t, g_t, l_t)NFEval(appMany(s[r/x],r),env,out,st​,gt​,lt​), then
NFEval(appMany((λx. s) r,r⃗), env, out, 1+st, gt, max⁡(∣appMany((λx. s) r,r⃗)∣,lt)).\mathsf{NFEval}\bigl(\mathsf{appMany}((\lambda x.\, s)\, r, \vec{r}),\, \mathsf{env},\, \mathsf{out},\, 1 + s_t,\, g_t,\, \max(|\mathsf{appMany}((\lambda x.\, s)\, r, \vec{r})|, l_t)\bigr).NFEval(appMany((λx.s)r,r),env,out,1+st​,gt​,max(∣appMany((λx.s)r,r)∣,lt​)).

5. Polynomials, Bellantoni–Cook Algebra, and Two-Sorted Denotation (IsUnivariatePoly, BCFunc, evalBC, RADenotesTwoSorted)

  • IsUnivariatePoly: an inductive predicate on functions p:N→Np : \mathbb{N} \to \mathbb{N}p:N→N generated by constant functions m↦cm \mapsto cm↦c (c∈Nc \in \mathbb{N}c∈N), the identity function m↦mm \mapsto mm↦m, pointwise addition m↦p(m)+q(m)m \mapsto p(m) + q(m)m↦p(m)+q(m), and pointwise multiplication m↦p(m)⋅q(m)m \mapsto p(m) \cdot q(m)m↦p(m)⋅q(m).

  • BCFunc: an inductive family of syntax trees BCFunc(m,n)\mathsf{BCFunc}(m, n)BCFunc(m,n) indexed by m,n∈Nm, n \in \mathbb{N}m,n∈N (representing mmm normal arguments and nnn safe arguments), generated by:

    • zero∈BCFunc(0,0)\mathsf{zero} \in \mathsf{BCFunc}(0, 0)zero∈BCFunc(0,0),
    • s0,s1,pred∈BCFunc(0,1)s_0, s_1, \mathsf{pred} \in \mathsf{BCFunc}(0, 1)s0​,s1​,pred∈BCFunc(0,1),
    • cases∈BCFunc(0,4)\mathsf{cases} \in \mathsf{BCFunc}(0, 4)cases∈BCFunc(0,4),
    • projNormal(j)∈BCFunc(m,n)\mathsf{projNormal}(j) \in \mathsf{BCFunc}(m, n)projNormal(j)∈BCFunc(m,n) for j∈Fin(m)j \in \mathsf{Fin}(m)j∈Fin(m),
    • projSafe(j)∈BCFunc(m,n)\mathsf{projSafe}(j) \in \mathsf{BCFunc}(m, n)projSafe(j)∈BCFunc(m,n) for j∈Fin(n)j \in \mathsf{Fin}(n)j∈Fin(n),
    • safeComp(g,g⃗,h⃗)∈BCFunc(m,n)\mathsf{safeComp}(g, \vec{g}, \vec{h}) \in \mathsf{BCFunc}(m, n)safeComp(g,g​,h)∈BCFunc(m,n) for g∈BCFunc(m′,n′)g \in \mathsf{BCFunc}(m', n')g∈BCFunc(m′,n′), g⃗:Fin(m′)→BCFunc(m,0)\vec{g} : \mathsf{Fin}(m') \to \mathsf{BCFunc}(m, 0)g​:Fin(m′)→BCFunc(m,0), and h⃗:Fin(n′)→BCFunc(m,n)\vec{h} : \mathsf{Fin}(n') \to \mathsf{BCFunc}(m, n)h:Fin(n′)→BCFunc(m,n),
    • safeRec(g,h0,h1)∈BCFunc(m+1,n)\mathsf{safeRec}(g, h_0, h_1) \in \mathsf{BCFunc}(m + 1, n)safeRec(g,h0​,h1​)∈BCFunc(m+1,n) for g∈BCFunc(m,n)g \in \mathsf{BCFunc}(m, n)g∈BCFunc(m,n) and h0,h1∈BCFunc(m+1,n+1)h_0, h_1 \in \mathsf{BCFunc}(m + 1, n + 1)h0​,h1​∈BCFunc(m+1,n+1).
  • evalBC: evaluates f∈BCFunc(m,n)f \in \mathsf{BCFunc}(m, n)f∈BCFunc(m,n) on normal inputs x⃗:Fin(m)→N\vec{x} : \mathsf{Fin}(m) \to \mathbb{N}x:Fin(m)→N and safe inputs y⃗:Fin(n)→N\vec{y} : \mathsf{Fin}(n) \to \mathbb{N}y​:Fin(n)→N to a natural number:

    • evalBC(zero,x⃗,y⃗)=0\mathsf{evalBC}(\mathsf{zero}, \vec{x}, \vec{y}) = 0evalBC(zero,x,y​)=0;
    • evalBC(s0,x⃗,y⃗)=2y0\mathsf{evalBC}(s_0, \vec{x}, \vec{y}) = 2 y_0evalBC(s0​,x,y​)=2y0​, evalBC(s1,x⃗,y⃗)=2y0+1\mathsf{evalBC}(s_1, \vec{x}, \vec{y}) = 2 y_0 + 1evalBC(s1​,x,y​)=2y0​+1, evalBC(pred,x⃗,y⃗)=⌊y0/2⌋\mathsf{evalBC}(\mathsf{pred}, \vec{x}, \vec{y}) = \lfloor y_0 / 2 \rfloorevalBC(pred,x,y​)=⌊y0​/2⌋;
    • evalBC(cases,x⃗,y⃗)=y1\mathsf{evalBC}(\mathsf{cases}, \vec{x}, \vec{y}) = y_1evalBC(cases,x,y​)=y1​ if y0=0y_0 = 0y0​=0, y2y_2y2​ if y0>0y_0 > 0y0​>0 is even, and y3y_3y3​ if y0y_0y0​ is odd;
    • evalBC(projNormal(j),x⃗,y⃗)=xj\mathsf{evalBC}(\mathsf{projNormal}(j), \vec{x}, \vec{y}) = x_jevalBC(projNormal(j),x,y​)=xj​ and evalBC(projSafe(j),x⃗,y⃗)=yj\mathsf{evalBC}(\mathsf{projSafe}(j), \vec{x}, \vec{y}) = y_jevalBC(projSafe(j),x,y​)=yj​;
    • evalBC(safeComp(g,g⃗,h⃗),x⃗,y⃗)=evalBC(g,(i↦evalBC(gi,x⃗,Fin.elim0)),(j↦evalBC(hj,x⃗,y⃗)))\mathsf{evalBC}(\mathsf{safeComp}(g, \vec{g}, \vec{h}), \vec{x}, \vec{y}) = \mathsf{evalBC}\bigl(g, (i \mapsto \mathsf{evalBC}(g_i, \vec{x}, \mathsf{Fin.elim0})), (j \mapsto \mathsf{evalBC}(h_j, \vec{x}, \vec{y}))\bigr)evalBC(safeComp(g,g​,h),x,y​)=evalBC(g,(i↦evalBC(gi​,x,Fin.elim0)),(j↦evalBC(hj​,x,y​)));
    • evalBC(safeRec(g,h0,h1),(x0,x1,…,xm),(y0,…,yn−1))\mathsf{evalBC}(\mathsf{safeRec}(g, h_0, h_1), (x_0, x_1, \dots, x_m), (y_0, \dots, y_{n-1}))evalBC(safeRec(g,h0​,h1​),(x0​,x1​,…,xm​),(y0​,…,yn−1​)) is defined via recNat\mathsf{recNat}recNat on x0x_0x0​:
      • when x0=0x_0 = 0x0​=0, it returns evalBC(g,(x1,…,xm),(y0,…,yn−1))\mathsf{evalBC}(g, (x_1, \dots, x_m), (y_0, \dots, y_{n-1}))evalBC(g,(x1​,…,xm​),(y0​,…,yn−1​));
      • when cur>0\mathsf{cur} > 0cur>0 with recursive value prev\mathsf{prev}prev at ⌊cur/2⌋\lfloor \mathsf{cur}/2 \rfloor⌊cur/2⌋, it calls h0h_0h0​ (if cur\mathsf{cur}cur is even) or h1h_1h1​ (if cur\mathsf{cur}cur is odd) on normal inputs (⌊cur/2⌋,x1,…,xm)(\lfloor \mathsf{cur}/2 \rfloor, x_1, \dots, x_m)(⌊cur/2⌋,x1​,…,xm​) and safe inputs (y0,…,yn−1,prev)(y_0, \dots, y_{n-1}, \mathsf{prev})(y0​,…,yn−1​,prev).
  • RADenotesTwoSorted: for m,n∈Nm, n \in \mathbb{N}m,n∈N, t∈Termt \in \mathsf{Term}t∈Term, and f:(Fin(m)→N)→(Fin(n)→N)→Nf : (\mathsf{Fin}(m) \to \mathbb{N}) \to (\mathsf{Fin}(n) \to \mathbb{N}) \to \mathbb{N}f:(Fin(m)→N)→(Fin(n)→N)→N, RADenotesTwoSorted(t,f)\mathsf{RADenotesTwoSorted}(t, f)RADenotesTwoSorted(t,f) holds if and only if:

    1. IsRATerm(t)\mathsf{IsRATerm}(t)IsRATerm(t) (i.e., ttt is well-typed and satisfies (R) and (A));
    2. IsClosed(t)\mathsf{IsClosed}(t)IsClosed(t) (freeVars(t)=[]\mathsf{freeVars}(t) = []freeVars(t)=[]);
    3. HasType(t, !ι→⋯→!ι⏟m→ι→⋯→ι⏟n→ι)\mathsf{HasType}\bigl(t,\, \underbrace{!\iota \to \cdots \to !\iota}_{m} \to \underbrace{\iota \to \cdots \to \iota}_{n} \to \iota\bigr)HasType(t,m!ι→⋯→!ι​​→nι→⋯→ι​​→ι); and
    4. for all x⃗:Fin(m)→N\vec{x} : \mathsf{Fin}(m) \to \mathbb{N}x:Fin(m)→N and y⃗:Fin(n)→N\vec{y} : \mathsf{Fin}(n) \to \mathbb{N}y​:Fin(n)→N, let k‾=binaryNumeralOfNat(k)\overline{k} = \mathsf{binaryNumeralOfNat}(k)k=binaryNumeralOfNat(k); then f(x⃗,y⃗)‾\overline{f(\vec{x}, \vec{y})}f(x,y​)​ is a normal form of the left-associated application
t (!x0‾)⋯(!xm−1‾) y0‾⋯yn−1‾t\, (!\overline{x_0}) \cdots (!\overline{x_{m-1}})\, \overline{y_0} \cdots \overline{y_{n-1}}t(!x0​​)⋯(!xm−1​​)y0​​⋯yn−1​​
 (i.e., the application reduces via $\to^*$ to $\overline{f(\vec{x}, \vec{y})}$ and $\overline{f(\vec{x}, \vec{y})}$ is irreducible under $\mathsf{Step}$).
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