Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

Section 2: Types, Linear/Passive Contexts, Affine Terms, and Denotational Semantics

Definition
ABHS2004_TermSystem

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

implicit-computational-complexitypolynomial-timeproof-theoryrealizabilitytype-theory

Formalizes Section 2 of Aehlig, Berger, Hofmann, and Schwichtenberg (2004): the full and !-free type systems, constants (including if, case, destr, and higher-type iteration it_ρ restricted to !-free result types), linear (Γ) and passive (Δ) typing judgments Δ; Γ ⊢ M : ρ, the operational reduction relation ↦ (Definitions 2.5 and 2.7), term and value size bounds |M| and ‖v‖ (Definition 2.9), polynomial-time Turing computability, and set-theoretic denotational semantics ⟦M⟧_η.

Definition code
import Std

/-!
# Term system for non-size-increasing polynomial-time computation (ABHS 2004, Section 2)

Source: Klaus Aehlig, Ulrich Berger, Martin Hofmann, and Helmut Schwichtenberg,
"An arithmetic for non-size-increasing polynomial-time computation",
*Theoretical Computer Science* 318(1–2), 2004, pp. 3–27, Section 2 (pp. 3–9).

This module formalizes:
- Definition 2.1: Finite linear types (`Ty`) and Remark 2.3 abbreviations (`B`, `N`, `Bin`).
- Definition 2.2: Set model carrier `SetModel` (`S^ρ`).
- Definition 2.4 & 2.5: Raw terms (`Term`) and typed constants (`Const`).
- Definition 2.6: Linear typing judgment `HasType` (`r^ρ`) and weak typing `WeaklyTyped`
  (Section 3.1, p. 10).
- Definition 2.8, 2.9, 2.10: Lists (`listEntryCount`, `IsListTerm`), conversions (`ConvStep`),
  one-step reduction (`RedStep`), multi-step reduction (`RedStar`, `RedSteps`), and normal
  forms (`IsNormal`).
- Definition 2.12 & Proposition 2.13: Almost closed terms (`IsAlmostClosed`) and canonical
  normal forms (`HasCanonicalNormalForm`).
- Definition 2.14, 2.15, 2.16: Projections `pi0`, `pi1` and pairing helpers.
- Definition 2.17 & 2.20: List bound `#_n(r)` (`hashBound`), polynomial step measure
  `ϑ_n(r)` (`theta`), bounded-free-variable reduction `RedStepBounded` (`r →_n r'`),
  iteration nesting depth (`iterNesting`), and syntactical length (`termLength`).
- Definition 2.24: Data types (`IsDataType`), data objects (`IsDataObject`), and
  non-size-increasing polynomial-time computability on data objects.
-/

namespace ABHS2004

/-- Definition 2.1 (Finite linear types, p. 3):
`ρ, σ ::= U | ⋄ | L(ρ) | ρ ⊸ σ | ρ ⊗ σ | ρ × σ | ρ + σ`. -/
inductive Ty where
  | unit : Ty
  | diamond : Ty
  | list : Ty → Ty
  | lolli : Ty → Ty → Ty
  | tensor : Ty → Ty → Ty
  | prod : Ty → Ty → Ty
  | sum : Ty → Ty → Ty
  deriving DecidableEq, Repr

namespace Ty

/-- Remark 2.3 (p. 3): Booleans `B := U + U`. -/
def bool : Ty := .sum .unit .unit

/-- Remark 2.3 (p. 3): Unary natural numbers `N := L(U)`. -/
def nat : Ty := .list .unit

/-- Remark 2.3 (p. 3): Binary natural numbers `Bin := L(B)`. -/
def bin : Ty := .list bool

/-- Iterated linear function type `ρ₁ ⊸ ⋯ ⊸ ρₙ ⊸ τ`. -/
def lolliMany : List Ty → Ty → Ty
  | [], τ => τ
  | ρ :: ρs, τ => .lolli ρ (lolliMany ρs τ)

end Ty

/-- Definition 2.2 (Set model, p. 3):
In the naive set model every type `ρ` is interpreted by the set `S^ρ`, where `U` is a
singleton set, `⋄` is an unspecified nonempty set (modeled here by `Unit`), `L(ρ)` is `List S^ρ`,
`ρ ⊸ σ` is the full function space `S^ρ → S^σ`, `ρ ⊗ σ` and `ρ × σ` are the Cartesian product,
and `ρ + σ` is the disjoint sum. -/
def SetModel : Ty → Type
  | .unit => PUnit
  | .diamond => Unit
  | .list ρ => List (SetModel ρ)
  | .lolli ρ σ => SetModel ρ → SetModel σ
  | .tensor ρ σ => SetModel ρ × SetModel σ
  | .prod ρ σ => SetModel ρ × SetModel σ
  | .sum ρ σ => Sum (SetModel ρ) (SetModel σ)

/-- Typed term variable `x^ρ` (p. 4). Each variable has an identifier and a fixed type `ρ`. -/
structure Var where
  id : Nat
  ty : Ty
  deriving DecidableEq, Repr

/-- Definition 2.5 (Constants, p. 4). -/
inductive Const where
  | eps : Const
  | nil : Ty → Const
  | cons : Ty → Const
  | tensorIntro : Ty → Ty → Const
  | tensorElim : Ty → Ty → Ty → Const
  | prodIntro : Ty → Ty → Ty → Const
  | fst : Ty → Ty → Const
  | snd : Ty → Ty → Const
  | inl : Ty → Ty → Const
  | inr : Ty → Ty → Const
  | sumElim : Ty → Ty → Ty → Const
  deriving DecidableEq, Repr

/-- Type assignment `c : ρ` for constants from Definition 2.5 (p. 4). -/
def Const.ty : Const → Ty
  | .eps => .unit
  | .nil ρ => .list ρ
  | .cons ρ => .lolli .diamond (.lolli ρ (.lolli (.list ρ) (.list ρ)))
  | .tensorIntro ρ σ => .lolli ρ (.lolli σ (.tensor ρ σ))
  | .tensorElim ρ σ τ => .lolli (.tensor ρ σ) (.lolli (.lolli ρ (.lolli σ τ)) τ)
  | .prodIntro ρ σ τ =>
      .lolli (.lolli τ ρ) (.lolli (.lolli τ σ) (.lolli τ (.prod ρ σ)))
  | .fst ρ σ => .lolli (.prod ρ σ) ρ
  | .snd ρ σ => .lolli (.prod ρ σ) σ
  | .inl ρ σ => .lolli ρ (.sum ρ σ)
  | .inr ρ σ => .lolli σ (.sum ρ σ)
  | .sumElim ρ σ τ =>
      .lolli (.sum ρ σ) (.lolli (.prod (.lolli ρ τ) (.lolli σ τ)) τ)

/-- Constructors of the term calculus (Axiom 22, p. 15 & Remark 3.11(4), p. 16):
`ε, ⊗⁺, inl, inr, nil, cons`. -/
def Const.isConstructor : Const → Bool
  | .eps => true
  | .tensorIntro _ _ => true
  | .inl _ _ => true
  | .inr _ _ => true
  | .nil _ => true
  | .cons _ => true
  | _ => false

/-- Definition 2.4 (Terms, p. 4):
`r, s ::= x^ρ | c | λx^ρ r | rs | r{s}`. -/
inductive Term where
  | var : Var → Term
  | const : Const → Term
  | lam : Var → Term → Term
  | app : Term → Term → Term
  | iter : Term → Term → Term
  deriving DecidableEq, Repr

namespace Term

/-- Multi-argument application `r s₁ ... sₙ`. -/
def appMany : Term → List Term → Term
  | r, [] => r
  | r, s :: ss => appMany (.app r s) ss

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

/-- Identity combinator `id_ρ := λ x^ρ. x^ρ`. -/
def idTerm (ρ : Ty) : Term :=
  let x : Var := ⟨0, ρ⟩
  .lam x (.var x)

/-- Boolean constant `tt := inl_{U,U} ε` (Remark 3.2, p. 10). -/
def tt : Term := .app (.const (.inl .unit .unit)) (.const .eps)

/-- Boolean constant `ff := inr_{U,U} ε` (Remark 3.2, p. 10). -/
def ff : Term := .app (.const (.inr .unit .unit)) (.const .eps)

/-- Zero numeral `0 := nil_U` (Example 2.15, p. 6). -/
def zeroNat : Term := .const (.nil .unit)

/-- Successor operation `S d r := cons_U d ε r` (Example 2.15, p. 6). -/
def succNat (d r : Term) : Term :=
  .app (.app (.app (.const (.cons .unit)) d) (.const .eps)) r

/-- Definition 2.14 (Projections, p. 6):
`π₀(r) := ⊗⁻_{ρσρ} r (λx^ρ, y^σ. x)`. -/
def pi0 (ρ σ : Ty) (r : Term) : Term :=
  let x : Var := ⟨0, ρ⟩
  let y : Var := ⟨1, σ⟩
  .app (.app (.const (.tensorElim ρ σ ρ)) r) (.lam x (.lam y (.var x)))

/-- Definition 2.14 (Projections, p. 6):
`π₁(r) := ⊗⁻_{ρσσ} r (λx^ρ, y^σ. y)`. -/
def pi1 (ρ σ : Ty) (r : Term) : Term :=
  let x : Var := ⟨0, ρ⟩
  let y : Var := ⟨1, σ⟩
  .app (.app (.const (.tensorElim ρ σ σ)) r) (.lam x (.lam y (.var y)))

end Term

/-- Free variable occurrences of a term `r`, as a list preserving multiplicity
(Definition 2.4, p. 4 and Definition 2.20, p. 8: `|FV(s)|` is the number of occurrences
of free variables in `s`). -/
def freeVarOccurrences : Term → List Var
  | .var x => [x]
  | .const _ => []
  | .lam x r => (freeVarOccurrences r).filter (· != x)
  | .app r s => freeVarOccurrences r ++ freeVarOccurrences s
  | .iter r s => freeVarOccurrences r ++ freeVarOccurrences s

/-- Underlying set of free variables `FV(r)` (represented as a duplicate-free list). -/
def freeVars (r : Term) : List Var :=
  (freeVarOccurrences r).eraseDups

/-- Number of occurrences of free variables `|FV(r)|` (Definition 2.20, p. 8). -/
def freeVarCount (r : Term) : Nat :=
  (freeVarOccurrences r).length

/-- Disjointness of free variables `FV(r) ∩ FV(s) = ∅`. -/
def DisjointFV (r s : Term) : Prop :=
  ∀ v, v ∈ freeVarOccurrences r → v ∉ freeVarOccurrences s

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

/-- Definition 2.12 (Almost closed terms, p. 6):
A term is almost closed if all its free variables are of type `⋄`. -/
def IsAlmostClosed (r : Term) : Prop :=
  ∀ v ∈ freeVarOccurrences r, v.ty = .diamond

/-- Maximum variable id occurring anywhere in a term (bound or free). -/
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)
  | .iter r s => Nat.max (maxVarId r) (maxVarId s)

/-- 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
  | .const c => .const c
  | .lam v body =>
      if v == x then .lam v body else .lam v (renameFreeVar body x y)
  | .app t s => .app (renameFreeVar t x y) (renameFreeVar s x y)
  | .iter t s => .iter (renameFreeVar t x y) (renameFreeVar s x y)

/-- Syntactic size of a raw term used for termination of capture-avoiding substitution. -/
def rawSize : Term → Nat
  | .var _ => 1
  | .const _ => 1
  | .lam _ r => rawSize r + 1
  | .app r s => rawSize r + rawSize s + 1
  | .iter r s => rawSize r + rawSize s + 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
  | const c => rfl
  | lam v body ih =>
      simp only [renameFreeVar]
      split
      · rfl
      · simp [rawSize, ih]
  | app t s iht ihs =>
      simp [renameFreeVar, rawSize, iht, ihs]
  | iter t s iht ihs =>
      simp [renameFreeVar, rawSize, iht, ihs]

/-- Capture-avoiding substitution `r[s/x]` (Definition 2.4, p. 4):
substitutes `s` for every free occurrence of `x` in `r`, renaming bound variables in `r`
if necessary to avoid capturing free variables of `s`. -/
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)
  | .iter t u => .iter (substTerm t x s) (substTerm u x s)
termination_by rawSize r
decreasing_by
  all_goals
    simp_wf
    simp [rawSize, renameFreeVar_rawSize]
    try omega

/-- Simultaneous substitution `r[s₁/x₁, ..., sₙ/xₙ]` applied sequentially. -/
def substMany : Term → List (Var × Term) → Term
  | r, [] => r
  | r, (x, s) :: rest => substMany (substTerm r x s) rest

/-- Definition 2.6 (Typing, pp. 4–5):
The relation `HasType r ρ` (`r^ρ`, read "`r` has type `ρ`"). -/
inductive HasType : Term → Ty → Prop where
  | var (x : Var) :
      HasType (.var x) x.ty
  | const (c : Const) :
      HasType (.const c) c.ty
  | lam (x : Var) {r : Term} {σ : Ty} :
      HasType r σ →
      HasType (.lam x r) (.lolli x.ty σ)
  | app {r s : Term} {ρ σ : Ty} :
      HasType r (.lolli ρ σ) →
      HasType s ρ →
      DisjointFV r s →
      HasType (.app r s) σ
  | iter {r s : Term} {ρ τ : Ty} :
      HasType r (.list ρ) →
      HasType s (.lolli .diamond (.lolli ρ (.lolli τ τ))) →
      IsClosed s →
      HasType (.iter r s) (.lolli τ τ)

/-- Weak typing (Section 3.1, p. 10):
"all restrictions on free variables (when typing terms of the form `rs` or `r{s}`) are dropped." -/
inductive WeaklyTyped : Term → Ty → Prop where
  | var (x : Var) :
      WeaklyTyped (.var x) x.ty
  | const (c : Const) :
      WeaklyTyped (.const c) c.ty
  | lam (x : Var) {r : Term} {σ : Ty} :
      WeaklyTyped r σ →
      WeaklyTyped (.lam x r) (.lolli x.ty σ)
  | app {r s : Term} {ρ σ : Ty} :
      WeaklyTyped r (.lolli ρ σ) →
      WeaklyTyped s ρ →
      WeaklyTyped (.app r s) σ
  | iter {r s : Term} {ρ τ : Ty} :
      WeaklyTyped r (.list ρ) →
      WeaklyTyped s (.lolli .diamond (.lolli ρ (.lolli τ τ))) →
      WeaklyTyped (.iter r s) (.lolli τ τ)

/-- Definition 2.8 (Lists, p. 5):
Terms of the form `cons_ρ d₁ r₁ (... (cons_ρ dₙ rₙ nil_ρ))` are lists with `n` entries.
`listEntryCount t` returns `some n` if `t` has this spine with `n` entries, and `none` otherwise. -/
def listEntryCount : Term → Option Nat
  | .const (.nil _) => some 0
  | .app (.app (.app (.const (.cons _)) _) _) l =>
      match listEntryCount l with
      | some k => some (k + 1)
      | none => none
  | _ => none

/-- Predicate asserting that `t` is a list in the sense of Definition 2.8 (p. 5). -/
def IsListTerm (t : Term) : Prop :=
  ∃ n, listEntryCount t = some n

/-- Definition 2.9 (Conversions, p. 5): `r ↦ r'`. -/
inductive ConvStep : Term → Term → Prop where
  | beta (x : Var) (r s : Term) :
      ConvStep (.app (.lam x r) s) (substTerm r x s)
  | tensorBeta (ρ σ τ : Ty) (r s t : Term) :
      ConvStep
        (.app (.app (.const (.tensorElim ρ σ τ))
          (.app (.app (.const (.tensorIntro ρ σ)) r) s)) t)
        (.app (.app t r) s)
  | fstBeta (ρ σ τ : Ty) (r s t : Term) :
      ConvStep
        (.app (.const (.fst ρ σ))
          (.app (.app (.app (.const (.prodIntro ρ σ τ)) r) s) t))
        (.app r t)
  | sndBeta (ρ σ τ : Ty) (r s t : Term) :
      ConvStep
        (.app (.const (.snd ρ σ))
          (.app (.app (.app (.const (.prodIntro ρ σ τ)) r) s) t))
        (.app s t)
  | inlBeta (ρ σ τ : Ty) (r s : Term) :
      ConvStep
        (.app (.app (.const (.sumElim ρ σ τ))
          (.app (.const (.inl ρ σ)) r)) s)
        (.app (.app (.const (.fst (.lolli ρ τ) (.lolli σ τ))) s) r)
  | inrBeta (ρ σ τ : Ty) (r s : Term) :
      ConvStep
        (.app (.app (.const (.sumElim ρ σ τ))
          (.app (.const (.inr ρ σ)) r)) s)
        (.app (.app (.const (.snd (.lolli ρ τ) (.lolli σ τ))) s) r)
  | iterNil (ρ : Ty) (s t : Term) :
      ConvStep (.app (.iter (.const (.nil ρ)) s) t) t
  | iterCons (ρ : Ty) (d r l s t : Term) :
      IsListTerm l →
      ConvStep
        (.app (.iter (.app (.app (.app (.const (.cons ρ)) d) r) l) s) t)
        (.app (.app (.app s d) r) (.app (.iter l s) t))

/-- Definition 2.10 (Reduction, p. 5): `r → r'`.
To reduce a term we may convert anywhere, except under `λ`. -/
inductive RedStep : Term → Term → Prop where
  | conv {r r' : Term} :
      ConvStep r r' → RedStep r r'
  | appLeft {r r' : Term} (s : Term) :
      RedStep r r' → RedStep (.app r s) (.app r' s)
  | appRight (r : Term) {s s' : Term} :
      RedStep s s' → RedStep (.app r s) (.app r s')
  | iterLeft {r r' : Term} (s : Term) :
      RedStep r r' → RedStep (.iter r s) (.iter r' s)
  | iterRight (r : Term) {s s' : Term} :
      RedStep s s' → RedStep (.iter r s) (.iter r s')

/-- Reflexive-transitive closure of reduction `r →* r'` (p. 6). -/
inductive RedStar : Term → Term → Prop where
  | refl (r : Term) : RedStar r r
  | step {r s t : Term} : RedStep r s → RedStar s t → RedStar r t

/-- Exact `k`-step reduction chain `r →^k r'`. -/
inductive RedSteps : Nat → Term → Term → Prop where
  | zero (r : Term) : RedSteps 0 r r
  | succ {k : Nat} {r s t : Term} :
      RedStep r s → RedSteps k s t → RedSteps (k + 1) r t

/-- A term is normal if it cannot be reduced (p. 6). -/
def IsNormal (r : Term) : Prop :=
  ¬ ∃ r', RedStep r r'

/-- Proposition 2.13 (Canonical forms of normal almost closed terms, p. 6):
- `U`: `ε`
- `⋄`: variable
- `L(ρ)`: list
- `ρ ⊗ σ`: `⊗⁺ r s`
- `ρ × σ`: `×⁺_{ρ,σ,τ} r s t`
- `ρ + σ`: `inl r` or `inr r`
- `ρ ⊸ σ`: `λx r` or `c r⃗` or `r{s}`. -/
def HasCanonicalNormalForm (τ : Ty) (w : Term) : Prop :=
  match τ with
  | .unit => w = .const .eps
  | .diamond => ∃ x : Var, x.ty = .diamond ∧ w = .var x
  | .list _ => IsListTerm w
  | .tensor ρ σ => ∃ r s, w = .app (.app (.const (.tensorIntro ρ σ)) r) s
  | .prod ρ σ => ∃ ν r s t, w = .app (.app (.app (.const (.prodIntro ρ σ ν)) r) s) t
  | .sum ρ σ =>
      (∃ r, w = .app (.const (.inl ρ σ)) r) ∨
      (∃ r, w = .app (.const (.inr ρ σ)) r)
  | .lolli _ _ =>
      (∃ x r, w = .lam x r) ∨
      (∃ c args, w = Term.appMany (.const c) args) ∨
      (∃ r s, w = .iter r s)

/-- Definition 2.17 (p. 7): `#_n(r)` returns `k` if `r` is a list with `k` entries and `k ≤ n`,
and `n` otherwise. -/
def hashBound (n : Nat) (r : Term) : Nat :=
  match listEntryCount r with
  | some k => if k ≤ n then k else n
  | none => n

/-- Definition 2.17 (pp. 7–8): Step-bounding functional `ϑ_n(r)`. -/
def theta (n : Nat) : Term → Nat
  | .var _ => 1
  | .const _ => 1
  | .app r s => theta n r + theta n s
  | .lam _ r => theta n r + 1
  | .iter r s => theta n r + (hashBound n r + 1) * theta n s

/-- Nesting depth of `{.}` in a term `r` (Section 2.3, pp. 7–9). -/
def iterNesting : Term → Nat
  | .var _ => 0
  | .const _ => 0
  | .lam _ r => iterNesting r
  | .app r s => Nat.max (iterNesting r) (iterNesting s)
  | .iter r s => Nat.max (iterNesting r) (iterNesting s + 1)

/-- Ordinary syntactical length `length(s)` of a term (Proposition 2.23, p. 9). -/
def termLength : Term → Nat
  | .var _ => 1
  | .const _ => 1
  | .lam _ r => termLength r + 1
  | .app r s => termLength r + termLength s
  | .iter r s => termLength r + termLength s

/-- Predicate stating that a term contains no `{.}` (Proposition 2.23, p. 9). -/
def IsIterFree (r : Term) : Prop :=
  iterNesting r = 0

/-- Definition 2.20 (p. 8): `r →_n r'` holds if `r → r'` by converting a subterm `s` of `r`
with `|FV(s)| ≤ n`, where `|FV(s)|` is the number of occurrences of free variables in `s`. -/
inductive RedStepBounded (n : Nat) : Term → Term → Prop where
  | conv {r r' : Term} :
      ConvStep r r' →
      freeVarCount r ≤ n →
      RedStepBounded n r r'
  | appLeft {r r' : Term} (s : Term) :
      RedStepBounded n r r' →
      RedStepBounded n (.app r s) (.app r' s)
  | appRight (r : Term) {s s' : Term} :
      RedStepBounded n s s' →
      RedStepBounded n (.app r s) (.app r s')
  | iterLeft {r r' : Term} (s : Term) :
      RedStepBounded n r r' →
      RedStepBounded n (.iter r s) (.iter r' s)
  | iterRight (r : Term) {s s' : Term} :
      RedStepBounded n s s' →
      RedStepBounded n (.iter r s) (.iter r s')

/-- Definition 2.24 (Data types, p. 9):
A type is called a data type if it is built from `U`, `L(.)`, `⊗`, and `+` only. -/
inductive IsDataType : Ty → Prop where
  | unit : IsDataType .unit
  | list {ρ : Ty} : IsDataType ρ → IsDataType (.list ρ)
  | tensor {ρ σ : Ty} : IsDataType ρ → IsDataType σ → IsDataType (.tensor ρ σ)
  | sum {ρ σ : Ty} : IsDataType ρ → IsDataType σ → IsDataType (.sum ρ σ)

/-- Definition 2.24 (Data objects, p. 9):
A data object of data type `τ` is an almost closed term `w^τ` in normal form. -/
def IsDataObject (τ : Ty) (w : Term) : Prop :=
  IsDataType τ ∧ HasType w τ ∧ IsAlmostClosed w ∧ IsNormal w

/-- A tuple of terms `ws` is a tuple of data objects of types `τs` with pairwise disjoint
free variables (and disjoint from a given program term `r`), so that the application
`r w₁ ... wₙ` is well-typed. -/
inductive ValidDataInputs : Term → List Ty → List Term → Prop where
  | nil (r : Term) : ValidDataInputs r [] []
  | cons {r : Term} {τ : Ty} {w : Term} {τs : List Ty} {ws : List Term} :
      IsDataObject τ w →
      DisjointFV r w →
      ValidDataInputs (.app r w) τs ws →
      ValidDataInputs r (τ :: τs) (w :: ws)

/-- Sum of syntactical lengths of a tuple of input terms. -/
def totalTermLength : List Term → Nat
  | [] => 0
  | w :: ws => termLength w + totalTermLength ws

/-- Sum of free-variable counts of a tuple of input terms. -/
def totalFreeVarCount : List Term → Nat
  | [] => 0
  | w :: ws => freeVarCount w + totalFreeVarCount ws

/-- A function `B : Nat → Nat` is bounded by a polynomial of degree at most `d`
if there exist constants `c₀, c₁ : Nat` such that `B n ≤ c₀ * (n + 1)^d + c₁` for all `n`. -/
def BoundedByPolyOfDegree (B : Nat → Nat) (d : Nat) : Prop :=
  ∃ c₀ c₁ : Nat, ∀ n : Nat, B n ≤ c₀ * (n + 1) ^ d + c₁

end ABHS2004
Source
Klaus Aehlig, Ulrich Berger, Martin Hofmann, and Helmut Schwichtenberg, 'An arithmetic for non-size-increasing polynomial-time computation', Theoretical Computer Science 318(1-2), 2004, pp. 3-27 (doi:10.1016/j.tcs.2003.10.023). Section 2, pp. 6-14.
Read-back

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

Blind Read-back: Def_ABHS2004_TermSystem.lean

Types (Ty, Ty.bool, Ty.nat, Ty.bin, Ty.lolliMany). The inductive type Ty\mathrm{Ty}Ty of types is freely generated by two base types, the unit type UUU and the diamond type ⋄\diamond⋄, together with unary list formation L(ρ)L(\rho)L(ρ) and four binary type constructors: linear implication ρ⊸σ\rho \multimap \sigmaρ⊸σ, tensor product ρ⊗σ\rho \otimes \sigmaρ⊗σ, Cartesian product ρ×σ\rho \times \sigmaρ×σ, and disjoint sum ρ+σ\rho + \sigmaρ+σ. Three abbreviations are defined: the boolean type B:=U+U\mathrm{B} := U + UB:=U+U, the unary natural number type N:=L(U)\mathrm{N} := L(U)N:=L(U), and the binary natural number type Bin:=L(B)=L(U+U)\mathrm{Bin} := L(\mathrm{B}) = L(U + U)Bin:=L(B)=L(U+U). For a finite list of types [ρ1,…,ρn][\rho_1, \dots, \rho_n][ρ1​,…,ρn​] and a target type τ\tauτ, the iterated linear function type lolliMany([ρ1,…,ρn],τ)\mathrm{lolliMany}([\rho_1, \dots, \rho_n], \tau)lolliMany([ρ1​,…,ρn​],τ) is defined by right-associated recursion as ρ1⊸(ρ2⊸⋯⊸(ρn⊸τ)⋯ )\rho_1 \multimap (\rho_2 \multimap \cdots \multimap (\rho_n \multimap \tau) \cdots)ρ1​⊸(ρ2​⊸⋯⊸(ρn​⊸τ)⋯), returning τ\tauτ when the list is empty (n=0n = 0n=0).

Set-theoretic interpretation (SetModel). For each type ρ∈Ty\rho \in \mathrm{Ty}ρ∈Ty, Sρ\mathcal{S}^\rhoSρ (SetModel(ρ)\mathrm{SetModel}(\rho)SetModel(ρ)) is a Type defined recursively on ρ\rhoρ:

SU=PUnit,S⋄=Unit,SL(ρ)=List(Sρ),Sρ⊸σ=(Sρ→Sσ),\mathcal{S}^U = \mathrm{PUnit}, \quad \mathcal{S}^\diamond = \mathrm{Unit}, \quad \mathcal{S}^{L(\rho)} = \mathrm{List}(\mathcal{S}^\rho), \quad \mathcal{S}^{\rho \multimap \sigma} = (\mathcal{S}^\rho \to \mathcal{S}^\sigma),SU=PUnit,S⋄=Unit,SL(ρ)=List(Sρ),Sρ⊸σ=(Sρ→Sσ), Sρ⊗σ=Sρ×Sσ,Sρ×σ=Sρ×Sσ,Sρ+σ=Sρ⊔Sσ.\mathcal{S}^{\rho \otimes \sigma} = \mathcal{S}^\rho \times \mathcal{S}^\sigma, \quad \mathcal{S}^{\rho \times \sigma} = \mathcal{S}^\rho \times \mathcal{S}^\sigma, \quad \mathcal{S}^{\rho + \sigma} = \mathcal{S}^\rho \sqcup \mathcal{S}^\sigma.Sρ⊗σ=Sρ×Sσ,Sρ×σ=Sρ×Sσ,Sρ+σ=Sρ⊔Sσ.

Note that both SU\mathcal{S}^USU and S⋄\mathcal{S}^\diamondS⋄ are concretely fixed as singleton (one-element) unit types, and Sρ⊸σ\mathcal{S}^{\rho \multimap \sigma}Sρ⊸σ is the full metalanguage function space from Sρ\mathcal{S}^\rhoSρ to Sσ\mathcal{S}^\sigmaSσ.

Typed variables and constants (Var, Const, Const.ty, Const.isConstructor). A term variable x∈Varx \in \mathrm{Var}x∈Var is a pair (id,ty)(\mathrm{id}, \mathrm{ty})(id,ty) consisting of a natural number identifier id∈N\mathrm{id} \in \mathbb{N}id∈N and an embedded type ty∈Ty\mathrm{ty} \in \mathrm{Ty}ty∈Ty; two variables are equal if and only if both their identifiers and their types are equal. A constant c∈Constc \in \mathrm{Const}c∈Const is one of eleven type-indexed syntactic forms, each assigned a fixed type c.ty∈Tyc.\mathrm{ty} \in \mathrm{Ty}c.ty∈Ty:

  • ε:U\varepsilon : Uε:U,
  • nilρ:L(ρ)\mathrm{nil}_\rho : L(\rho)nilρ​:L(ρ),
  • consρ:⋄⊸(ρ⊸(L(ρ)⊸L(ρ)))\mathrm{cons}_\rho : \diamond \multimap (\rho \multimap (L(\rho) \multimap L(\rho)))consρ​:⋄⊸(ρ⊸(L(ρ)⊸L(ρ))),
  • ⊗ρ,σ+:ρ⊸(σ⊸(ρ⊗σ))\otimes^+_{\rho,\sigma} : \rho \multimap (\sigma \multimap (\rho \otimes \sigma))⊗ρ,σ+​:ρ⊸(σ⊸(ρ⊗σ)),
  • ⊗ρ,σ,τ−:(ρ⊗σ)⊸((ρ⊸(σ⊸τ))⊸τ)\otimes^-_{\rho,\sigma,\tau} : (\rho \otimes \sigma) \multimap ((\rho \multimap (\sigma \multimap \tau)) \multimap \tau)⊗ρ,σ,τ−​:(ρ⊗σ)⊸((ρ⊸(σ⊸τ))⊸τ),
  • ×ρ,σ,τ+:(τ⊸ρ)⊸((τ⊸σ)⊸(τ⊸(ρ×σ)))\times^+_{\rho,\sigma,\tau} : (\tau \multimap \rho) \multimap ((\tau \multimap \sigma) \multimap (\tau \multimap (\rho \times \sigma)))×ρ,σ,τ+​:(τ⊸ρ)⊸((τ⊸σ)⊸(τ⊸(ρ×σ))),
  • fstρ,σ:(ρ×σ)⊸ρ\mathrm{fst}_{\rho,\sigma} : (\rho \times \sigma) \multimap \rhofstρ,σ​:(ρ×σ)⊸ρ,
  • sndρ,σ:(ρ×σ)⊸σ\mathrm{snd}_{\rho,\sigma} : (\rho \times \sigma) \multimap \sigmasndρ,σ​:(ρ×σ)⊸σ,
  • inlρ,σ:ρ⊸(ρ+σ)\mathrm{inl}_{\rho,\sigma} : \rho \multimap (\rho + \sigma)inlρ,σ​:ρ⊸(ρ+σ),
  • inrρ,σ:σ⊸(ρ+σ)\mathrm{inr}_{\rho,\sigma} : \sigma \multimap (\rho + \sigma)inrρ,σ​:σ⊸(ρ+σ),
  • +ρ,σ,τ−:(ρ+σ)⊸(((ρ⊸τ)×(σ⊸τ))⊸τ)+^-_{\rho,\sigma,\tau} : (\rho + \sigma) \multimap (((\rho \multimap \tau) \times (\sigma \multimap \tau)) \multimap \tau)+ρ,σ,τ−​:(ρ+σ)⊸(((ρ⊸τ)×(σ⊸τ))⊸τ).

The boolean predicate isConstructor(c)\mathrm{isConstructor}(c)isConstructor(c) evaluates to true\mathrm{true}true if ccc is one of ε\varepsilonε, ⊗ρ,σ+\otimes^+_{\rho,\sigma}⊗ρ,σ+​, inlρ,σ\mathrm{inl}_{\rho,\sigma}inlρ,σ​, inrρ,σ\mathrm{inr}_{\rho,\sigma}inrρ,σ​, nilρ\mathrm{nil}_\rhonilρ​, or consρ\mathrm{cons}_\rhoconsρ​, and false\mathrm{false}false otherwise (in particular, isConstructor(×ρ,σ,τ+)=false\mathrm{isConstructor}(\times^+_{\rho,\sigma,\tau}) = \mathrm{false}isConstructor(×ρ,σ,τ+​)=false).

Raw terms and abbreviations (Term, appMany, lamMany, idTerm, tt, ff, zeroNat, succNat, pi0, pi1). The inductive type Term\mathrm{Term}Term of raw terms is generated by: variables xxx, constants ccc, lambda abstractions λx. r\lambda x.\, rλx.r (where x∈Varx \in \mathrm{Var}x∈Var), binary applications r sr\,srs, and list iterations r{s}r\{s\}r{s}. For a term rrr and a list of argument terms [s1,…,sn][s_1, \dots, s_n][s1​,…,sn​], appMany(r,[s1,…,sn])\mathrm{appMany}(r, [s_1, \dots, s_n])appMany(r,[s1​,…,sn​]) is the left-associated iterated application (⋯((r s1) s2)⋯ ) sn(\cdots((r\,s_1)\,s_2)\cdots)\,s_n(⋯((rs1​)s2​)⋯)sn​ (or rrr when n=0n = 0n=0). For a list of 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) is λx1. ⋯ λxn. r\lambda x_1.\,\cdots\,\lambda x_n.\, rλx1​.⋯λxn​.r (or rrr when n=0n = 0n=0). The derived term constructors are:

  • idTerm(ρ):=λx0ρ. x0ρ\mathrm{idTerm}(\rho) := \lambda x_0^\rho.\, x_0^\rhoidTerm(ρ):=λx0ρ​.x0ρ​, where x0ρ=(0,ρ)x_0^\rho = (0, \rho)x0ρ​=(0,ρ);
  • tt:=inlU,U ε\mathrm{tt} := \mathrm{inl}_{U,U}\,\varepsilontt:=inlU,U​ε and ff:=inrU,U ε\mathrm{ff} := \mathrm{inr}_{U,U}\,\varepsilonff:=inrU,U​ε;
  • zeroNat:=nilU\mathrm{zeroNat} := \mathrm{nil}_UzeroNat:=nilU​ and succNat(d,r):=(((consU d) ε) r)\mathrm{succNat}(d, r) := (((\mathrm{cons}_U\,d)\,\varepsilon)\,r)succNat(d,r):=(((consU​d)ε)r);
  • π0(ρ,σ,r):=((⊗ρ,σ,ρ− r) (λx0ρ. λx1σ. x0ρ))\pi_0(\rho, \sigma, r) := ((\otimes^-_{\rho,\sigma,\rho}\,r)\,(\lambda x_0^\rho.\,\lambda x_1^\sigma.\, x_0^\rho))π0​(ρ,σ,r):=((⊗ρ,σ,ρ−​r)(λx0ρ​.λx1σ​.x0ρ​)) and π1(ρ,σ,r):=((⊗ρ,σ,σ− r) (λx0ρ. λx1σ. x1σ))\pi_1(\rho, \sigma, r) := ((\otimes^-_{\rho,\sigma,\sigma}\,r)\,(\lambda x_0^\rho.\,\lambda x_1^\sigma.\, x_1^\sigma))π1​(ρ,σ,r):=((⊗ρ,σ,σ−​r)(λx0ρ​.λx1σ​.x1σ​)), where x0ρ=(0,ρ)x_0^\rho = (0, \rho)x0ρ​=(0,ρ) and x1σ=(1,σ)x_1^\sigma = (1, \sigma)x1σ​=(1,σ).

Free variables and closedness predicates (freeVarOccurrences, freeVars, freeVarCount, DisjointFV, IsClosed, IsAlmostClosed). For any raw term rrr, freeVarOccurrences(r)\mathrm{freeVarOccurrences}(r)freeVarOccurrences(r) is a list of variables preserving multiplicity and left-to-right order: it is [x][x][x] for a variable xxx, [][][] for a constant ccc, the list freeVarOccurrences(r)\mathrm{freeVarOccurrences}(r)freeVarOccurrences(r) with all occurrences equal to xxx removed for λx. r\lambda x.\, rλx.r, and the list concatenation freeVarOccurrences(r)+ ⁣+freeVarOccurrences(s)\mathrm{freeVarOccurrences}(r) \mathbin{+\!+} \mathrm{freeVarOccurrences}(s)freeVarOccurrences(r)++freeVarOccurrences(s) for both r sr\,srs and r{s}r\{s\}r{s}. The list freeVars(r)\mathrm{freeVars}(r)freeVars(r) is obtained by deduplicating freeVarOccurrences(r)\mathrm{freeVarOccurrences}(r)freeVarOccurrences(r), and freeVarCount(r):=∣freeVarOccurrences(r)∣∈N\mathrm{freeVarCount}(r) := |\mathrm{freeVarOccurrences}(r)| \in \mathbb{N}freeVarCount(r):=∣freeVarOccurrences(r)∣∈N is the number of free variable occurrences in rrr counted with multiplicity. Two terms r,sr, sr,s have disjoint free variables, written DisjointFV(r,s)\mathrm{DisjointFV}(r, s)DisjointFV(r,s), if no variable v∈freeVarOccurrences(r)v \in \mathrm{freeVarOccurrences}(r)v∈freeVarOccurrences(r) appears in freeVarOccurrences(s)\mathrm{freeVarOccurrences}(s)freeVarOccurrences(s). A term rrr is closed (IsClosed(r)\mathrm{IsClosed}(r)IsClosed(r)) if freeVarOccurrences(r)=[]\mathrm{freeVarOccurrences}(r) = []freeVarOccurrences(r)=[], and almost closed (IsAlmostClosed(r)\mathrm{IsAlmostClosed}(r)IsAlmostClosed(r)) if every variable v∈freeVarOccurrences(r)v \in \mathrm{freeVarOccurrences}(r)v∈freeVarOccurrences(r) has type v.ty=⋄v.\mathrm{ty} = \diamondv.ty=⋄.

Variable renaming and capture-avoiding substitution (maxVarId, renameFreeVar, rawSize, renameFreeVar_rawSize, substTerm, substMany). For a term rrr, maxVarId(r)∈N\mathrm{maxVarId}(r) \in \mathbb{N}maxVarId(r)∈N is the maximum identifier v.idv.\mathrm{id}v.id among all free and λ\lambdaλ-bound variables occurring in rrr (and 000 if rrr contains only constants). The function renameFreeVar(r,x,y)\mathrm{renameFreeVar}(r, x, y)renameFreeVar(r,x,y) replaces every free occurrence of variable xxx in rrr with variable yyy, halting under λv\lambda vλv when v=xv = xv=x without checking for capture of yyy. Its structural size rawSize(r)\mathrm{rawSize}(r)rawSize(r) (111 for variables and constants, rawSize(r)+1\mathrm{rawSize}(r) + 1rawSize(r)+1 for λx. r\lambda x.\,rλx.r, and rawSize(r)+rawSize(s)+1\mathrm{rawSize}(r) + \mathrm{rawSize}(s) + 1rawSize(r)+rawSize(s)+1 for r sr\,srs and r{s}r\{s\}r{s}) is preserved by renameFreeVar\mathrm{renameFreeVar}renameFreeVar (renameFreeVar_rawSize\mathrm{renameFreeVar\_rawSize}renameFreeVar_rawSize). Capture-avoiding substitution r[s/x]r[s/x]r[s/x] (substTerm(r,x,s)\mathrm{substTerm}(r, x, s)substTerm(r,x,s)) replaces free occurrences of xxx in rrr by sss: at a binder λv. body\lambda v.\,\mathrm{body}λv.body, if v=xv = xv=x the abstraction is returned unchanged; if v≠xv \neq xv=x and v∈freeVarOccurrences(s)v \in \mathrm{freeVarOccurrences}(s)v∈freeVarOccurrences(s), the bound variable vvv is first renamed via renameFreeVar\mathrm{renameFreeVar}renameFreeVar to v′:=(max⁡(maxVarId(body),max⁡(maxVarId(s),x.id))+1,v.ty)v' := (\max(\mathrm{maxVarId}(\mathrm{body}), \max(\mathrm{maxVarId}(s), x.\mathrm{id})) + 1, v.\mathrm{ty})v′:=(max(maxVarId(body),max(maxVarId(s),x.id))+1,v.ty) before recursively substituting sss for xxx; otherwise substitution proceeds directly into body\mathrm{body}body. Note that substTerm\mathrm{substTerm}substTerm does not check whether x.tyx.\mathrm{ty}x.ty matches the type of sss. For a list of pairs [(x1,s1),…,(xn,sn)][(x_1, s_1), \dots, (x_n, s_n)][(x1​,s1​),…,(xn​,sn​)], substMany(r,[(x1,s1),…,(xn,sn)])\mathrm{substMany}(r, [(x_1, s_1), \dots, (x_n, s_n)])substMany(r,[(x1​,s1​),…,(xn​,sn​)]) applies substTerm\mathrm{substTerm}substTerm sequentially from left to right: (⋯(r[s1/x1])⋯ )[sn/xn](\cdots(r[s_1/x_1])\cdots)[s_n/x_n](⋯(r[s1​/x1​])⋯)[sn​/xn​] (because it is sequential rather than simultaneous, free occurrences of xjx_jxj​ inside an earlier replacement term sis_isi​ for i<ji < ji<j will also be substituted by sjs_jsj​).

Linear typing and weak typing (HasType, WeaklyTyped). The binary relation HasType(r,ρ)\mathrm{HasType}(r, \rho)HasType(r,ρ) (meaning term rrr has linear type ρ\rhoρ) is inductively defined by five rules:

  1. Every variable xxx has type x.tyx.\mathrm{ty}x.ty.
  2. Every constant ccc has type c.tyc.\mathrm{ty}c.ty.
  3. If HasType(r,σ)\mathrm{HasType}(r, \sigma)HasType(r,σ), then HasType(λx. r,x.ty⊸σ)\mathrm{HasType}(\lambda x.\, r, x.\mathrm{ty} \multimap \sigma)HasType(λx.r,x.ty⊸σ) (with no restriction on how many times xxx occurs free in rrr).
  4. If HasType(r,ρ⊸σ)\mathrm{HasType}(r, \rho \multimap \sigma)HasType(r,ρ⊸σ), HasType(s,ρ)\mathrm{HasType}(s, \rho)HasType(s,ρ), and DisjointFV(r,s)\mathrm{DisjointFV}(r, s)DisjointFV(r,s), then HasType(r s,σ)\mathrm{HasType}(r\,s, \sigma)HasType(rs,σ).
  5. If HasType(r,L(ρ))\mathrm{HasType}(r, L(\rho))HasType(r,L(ρ)), HasType(s,⋄⊸(ρ⊸(τ⊸τ)))\mathrm{HasType}(s, \diamond \multimap (\rho \multimap (\tau \multimap \tau)))HasType(s,⋄⊸(ρ⊸(τ⊸τ))), and IsClosed(s)\mathrm{IsClosed}(s)IsClosed(s), then HasType(r{s},τ⊸τ)\mathrm{HasType}(r\{s\}, \tau \multimap \tau)HasType(r{s},τ⊸τ).

The weak typing relation WeaklyTyped(r,ρ)\mathrm{WeaklyTyped}(r, \rho)WeaklyTyped(r,ρ) is defined by the exact same five rules, except that the side condition DisjointFV(r,s)\mathrm{DisjointFV}(r, s)DisjointFV(r,s) in the application rule and the side condition IsClosed(s)\mathrm{IsClosed}(s)IsClosed(s) in the iteration rule are dropped.

List terms (listEntryCount, IsListTerm). For any term ttt, listEntryCount(t)∈Option(N)\mathrm{listEntryCount}(t) \in \mathrm{Option}(\mathbb{N})listEntryCount(t)∈Option(N) is defined by structural recursion:

listEntryCount(nilρ)=some(0),listEntryCount(((consρ d) r) l)={some(k+1)if listEntryCount(l)=some(k),noneif listEntryCount(l)=none,\mathrm{listEntryCount}(\mathrm{nil}_\rho) = \mathrm{some}(0), \qquad \mathrm{listEntryCount}(((\mathrm{cons}_\rho\,d)\,r)\,l) = \begin{cases} \mathrm{some}(k + 1) & \text{if } \mathrm{listEntryCount}(l) = \mathrm{some}(k), \\ \mathrm{none} & \text{if } \mathrm{listEntryCount}(l) = \mathrm{none}, \end{cases}listEntryCount(nilρ​)=some(0),listEntryCount(((consρ​d)r)l)={some(k+1)none​if listEntryCount(l)=some(k),if listEntryCount(l)=none,​

and listEntryCount(t)=none\mathrm{listEntryCount}(t) = \mathrm{none}listEntryCount(t)=none for all other terms ttt. A term ttt satisfies IsListTerm(t)\mathrm{IsListTerm}(t)IsListTerm(t) if and only if there exists n∈Nn \in \mathbb{N}n∈N such that listEntryCount(t)=some(n)\mathrm{listEntryCount}(t) = \mathrm{some}(n)listEntryCount(t)=some(n). Note that on untyped terms, listEntryCount\mathrm{listEntryCount}listEntryCount does not require the type subscripts ρ\rhoρ on the consρ\mathrm{cons}_\rhoconsρ​ and nilρ\mathrm{nil}_\rhonilρ​ constants along the spine to be equal, nor does it constrain the subterms ddd and rrr.

Conversions, reductions, normal forms, and canonical forms (ConvStep, RedStep, RedStar, RedSteps, IsNormal, HasCanonicalNormalForm). The one-step conversion relation r↦r′r \mapsto r'r↦r′ (ConvStep(r,r′)\mathrm{ConvStep}(r, r')ConvStep(r,r′)) is inductively defined by eight rules for any types ρ,σ,τ\rho, \sigma, \tauρ,σ,τ and terms r,s,t,d,lr, s, t, d, lr,s,t,d,l:

  1. (λx. r) s↦r[s/x](\lambda x.\, r)\,s \mapsto r[s/x](λx.r)s↦r[s/x];
  2. ((⊗ρ,σ,τ− ((⊗ρ,σ+ r) s)) t)↦(t r) s((\otimes^-_{\rho,\sigma,\tau}\,((\otimes^+_{\rho,\sigma}\,r)\,s))\,t) \mapsto (t\,r)\,s((⊗ρ,σ,τ−​((⊗ρ,σ+​r)s))t)↦(tr)s;
  3. (fstρ,σ (((×ρ,σ,τ+ r) s) t))↦r t(\mathrm{fst}_{\rho,\sigma}\,(((\times^+_{\rho,\sigma,\tau}\,r)\,s)\,t)) \mapsto r\,t(fstρ,σ​(((×ρ,σ,τ+​r)s)t))↦rt;
  4. (sndρ,σ (((×ρ,σ,τ+ r) s) t))↦s t(\mathrm{snd}_{\rho,\sigma}\,(((\times^+_{\rho,\sigma,\tau}\,r)\,s)\,t)) \mapsto s\,t(sndρ,σ​(((×ρ,σ,τ+​r)s)t))↦st;
  5. ((+ρ,σ,τ− (inlρ,σ r)) s)↦((fstρ⊸τ,σ⊸τ s) r)((+^-_{\rho,\sigma,\tau}\,(\mathrm{inl}_{\rho,\sigma}\,r))\,s) \mapsto ((\mathrm{fst}_{\rho \multimap \tau, \sigma \multimap \tau}\,s)\,r)((+ρ,σ,τ−​(inlρ,σ​r))s)↦((fstρ⊸τ,σ⊸τ​s)r);
  6. ((+ρ,σ,τ− (inrρ,σ r)) s)↦((sndρ⊸τ,σ⊸τ s) r)((+^-_{\rho,\sigma,\tau}\,(\mathrm{inr}_{\rho,\sigma}\,r))\,s) \mapsto ((\mathrm{snd}_{\rho \multimap \tau, \sigma \multimap \tau}\,s)\,r)((+ρ,σ,τ−​(inrρ,σ​r))s)↦((sndρ⊸τ,σ⊸τ​s)r);
  7. ((nilρ{s}) t)↦t((\mathrm{nil}_\rho\{s\})\,t) \mapsto t((nilρ​{s})t)↦t;
  8. ((((consρ d) r) l){s}) t↦(((s d) r) ((l{s}) t))((((\mathrm{cons}_\rho\,d)\,r)\,l)\{s\})\,t \mapsto (((s\,d)\,r)\,((l\{s\})\,t))((((consρ​d)r)l){s})t↦(((sd)r)((l{s})t)) provided IsListTerm(l)\mathrm{IsListTerm}(l)IsListTerm(l) holds.

One-step reduction r→r′r \to r'r→r′ (RedStep(r,r′)\mathrm{RedStep}(r, r')RedStep(r,r′)) is the closure of ConvStep\mathrm{ConvStep}ConvStep under the left and right arguments of application (r sr\,srs) and the left and right arguments of iteration (r{s}r\{s\}r{s}), with no reduction permitted under λ\lambdaλ-abstraction. The relation RedStar(r,r′)\mathrm{RedStar}(r, r')RedStar(r,r′) is the reflexive-transitive closure of RedStep\mathrm{RedStep}RedStep, and RedSteps(k,r,r′)\mathrm{RedSteps}(k, r, r')RedSteps(k,r,r′) holds for k∈Nk \in \mathbb{N}k∈N when rrr reduces to r′r'r′ in exactly kkk steps of RedStep\mathrm{RedStep}RedStep. A term rrr is in normal form (IsNormal(r)\mathrm{IsNormal}(r)IsNormal(r)) if there is no r′r'r′ with RedStep(r,r′)\mathrm{RedStep}(r, r')RedStep(r,r′). For a type τ\tauτ and term www, HasCanonicalNormalForm(τ,w)\mathrm{HasCanonicalNormalForm}(\tau, w)HasCanonicalNormalForm(τ,w) holds according to the top-level constructor of τ\tauτ:

  • for τ=U\tau = Uτ=U: w=εw = \varepsilonw=ε;
  • for τ=⋄\tau = \diamondτ=⋄: there exists x∈Varx \in \mathrm{Var}x∈Var with x.ty=⋄x.\mathrm{ty} = \diamondx.ty=⋄ and w=xw = xw=x;
  • for τ=L(ρ)\tau = L(\rho)τ=L(ρ): IsListTerm(w)\mathrm{IsListTerm}(w)IsListTerm(w) holds;
  • for τ=ρ⊗σ\tau = \rho \otimes \sigmaτ=ρ⊗σ: there exist r,sr, sr,s such that w=(⊗ρ,σ+ r) sw = (\otimes^+_{\rho,\sigma}\,r)\,sw=(⊗ρ,σ+​r)s;
  • for τ=ρ×σ\tau = \rho \times \sigmaτ=ρ×σ: there exist a type ν\nuν and terms r,s,tr, s, tr,s,t such that w=((×ρ,σ,ν+ r) s) tw = ((\times^+_{\rho,\sigma,\nu}\,r)\,s)\,tw=((×ρ,σ,ν+​r)s)t;
  • for τ=ρ+σ\tau = \rho + \sigmaτ=ρ+σ: there exists rrr such that w=inlρ,σ rw = \mathrm{inl}_{\rho,\sigma}\,rw=inlρ,σ​r or w=inrρ,σ rw = \mathrm{inr}_{\rho,\sigma}\,rw=inrρ,σ​r;
  • for τ=ρ⊸σ\tau = \rho \multimap \sigmaτ=ρ⊸σ: either w=λx. rw = \lambda x.\, rw=λx.r for some x,rx, rx,r, or w=appMany(c,args)w = \mathrm{appMany}(c, \mathit{args})w=appMany(c,args) for some constant ccc and list of terms args\mathit{args}args (possibly empty), or w=r{s}w = r\{s\}w=r{s} for some r,sr, sr,s.

Step measures, syntactic measures, and bounded-free-variable reduction (hashBound, theta, iterNesting, termLength, IsIterFree, RedStepBounded). For n∈Nn \in \mathbb{N}n∈N and a term rrr, the bounded list length #n(r)\#_n(r)#n​(r) (hashBound(n,r)\mathrm{hashBound}(n, r)hashBound(n,r)) is kkk if listEntryCount(r)=some(k)\mathrm{listEntryCount}(r) = \mathrm{some}(k)listEntryCount(r)=some(k) with k≤nk \le nk≤n, and nnn otherwise. The step-bounding measure ϑn(r)∈N\vartheta_n(r) \in \mathbb{N}ϑn​(r)∈N (theta(n,r)\mathrm{theta}(n, r)theta(n,r)) is defined recursively on rrr by:

ϑn(x)=1,ϑn(c)=1,ϑn(r s)=ϑn(r)+ϑn(s),ϑn(λx. r)=ϑn(r)+1,ϑn(r{s})=ϑn(r)+(#n(r)+1) ϑn(s).\vartheta_n(x) = 1, \quad \vartheta_n(c) = 1, \quad \vartheta_n(r\,s) = \vartheta_n(r) + \vartheta_n(s), \quad \vartheta_n(\lambda x.\, r) = \vartheta_n(r) + 1, \quad \vartheta_n(r\{s\}) = \vartheta_n(r) + (\#_n(r) + 1)\,\vartheta_n(s).ϑn​(x)=1,ϑn​(c)=1,ϑn​(rs)=ϑn​(r)+ϑn​(s),ϑn​(λx.r)=ϑn​(r)+1,ϑn​(r{s})=ϑn​(r)+(#n​(r)+1)ϑn​(s).

The iteration nesting depth iterNesting(r)∈N\mathrm{iterNesting}(r) \in \mathbb{N}iterNesting(r)∈N is 000 for variables and constants, iterNesting(r)\mathrm{iterNesting}(r)iterNesting(r) for λx. r\lambda x.\, rλx.r, max⁡(iterNesting(r),iterNesting(s))\max(\mathrm{iterNesting}(r), \mathrm{iterNesting}(s))max(iterNesting(r),iterNesting(s)) for r sr\,srs, and max⁡(iterNesting(r),iterNesting(s)+1)\max(\mathrm{iterNesting}(r), \mathrm{iterNesting}(s) + 1)max(iterNesting(r),iterNesting(s)+1) for r{s}r\{s\}r{s}; a term rrr satisfies IsIterFree(r)\mathrm{IsIterFree}(r)IsIterFree(r) if iterNesting(r)=0\mathrm{iterNesting}(r) = 0iterNesting(r)=0. The syntactic length termLength(r)∈N\mathrm{termLength}(r) \in \mathbb{N}termLength(r)∈N is 111 for variables and constants, termLength(r)+1\mathrm{termLength}(r) + 1termLength(r)+1 for λx. r\lambda x.\, rλx.r, and termLength(r)+termLength(s)\mathrm{termLength}(r) + \mathrm{termLength}(s)termLength(r)+termLength(s) for both r sr\,srs and r{s}r\{s\}r{s}. For n∈Nn \in \mathbb{N}n∈N, the bounded one-step reduction relation r→nr′r \to_n r'r→n​r′ (RedStepBounded(n,r,r′)\mathrm{RedStepBounded}(n, r, r')RedStepBounded(n,r,r′)) is defined identically to RedStep(r,r′)\mathrm{RedStep}(r, r')RedStep(r,r′) except that at the base conversion step s↦s′s \mapsto s's↦s′, the converted redex subterm sss must satisfy freeVarCount(s)≤n\mathrm{freeVarCount}(s) \le nfreeVarCount(s)≤n.

Data types, data objects, valid inputs, and polynomial bounds (IsDataType, IsDataObject, ValidDataInputs, totalTermLength, totalFreeVarCount, BoundedByPolyOfDegree). A type τ∈Ty\tau \in \mathrm{Ty}τ∈Ty is a data type (IsDataType(τ)\mathrm{IsDataType}(\tau)IsDataType(τ)) if and only if it is built inductively from UUU using L(⋅)L(\cdot)L(⋅), ⊗\otimes⊗, and +++ (excluding ⋄\diamond⋄, ⊸\multimap⊸, and ×\times×). A term www is a data object of type τ\tauτ (IsDataObject(τ,w)\mathrm{IsDataObject}(\tau, w)IsDataObject(τ,w)) if IsDataType(τ)\mathrm{IsDataType}(\tau)IsDataType(τ), HasType(w,τ)\mathrm{HasType}(w, \tau)HasType(w,τ), IsAlmostClosed(w)\mathrm{IsAlmostClosed}(w)IsAlmostClosed(w), and IsNormal(w)\mathrm{IsNormal}(w)IsNormal(w) all hold. Given a term rrr, a list of types [τ1,…,τm][\tau_1, \dots, \tau_m][τ1​,…,τm​], and a list of terms [w1,…,wm][w_1, \dots, w_m][w1​,…,wm​], the predicate ValidDataInputs(r,[τ1,…,τm],[w1,…,wm])\mathrm{ValidDataInputs}(r, [\tau_1, \dots, \tau_m], [w_1, \dots, w_m])ValidDataInputs(r,[τ1​,…,τm​],[w1​,…,wm​]) holds inductively if the two lists have the same length mmm, each wiw_iwi​ satisfies IsDataObject(τi,wi)\mathrm{IsDataObject}(\tau_i, w_i)IsDataObject(τi​,wi​), and for each i∈{1,…,m}i \in \{1, \dots, m\}i∈{1,…,m}, DisjointFV(((⋯(r w1)⋯ ) wi−1),wi)\mathrm{DisjointFV}(((\cdots(r\,w_1)\cdots)\,w_{i-1}), w_i)DisjointFV(((⋯(rw1​)⋯)wi−1​),wi​) holds (equivalently, the free variables of r,w1,…,wmr, w_1, \dots, w_mr,w1​,…,wm​ are pairwise disjoint). For a list of terms ws=[w1,…,wm]ws = [w_1, \dots, w_m]ws=[w1​,…,wm​], totalTermLength(ws):=∑i=1mtermLength(wi)\mathrm{totalTermLength}(ws) := \sum_{i=1}^m \mathrm{termLength}(w_i)totalTermLength(ws):=∑i=1m​termLength(wi​) and totalFreeVarCount(ws):=∑i=1mfreeVarCount(wi)\mathrm{totalFreeVarCount}(ws) := \sum_{i=1}^m \mathrm{freeVarCount}(w_i)totalFreeVarCount(ws):=∑i=1m​freeVarCount(wi​) (both equal to 000 when ws=[]ws = []ws=[]). Finally, a function B:N→NB : \mathbb{N} \to \mathbb{N}B:N→N satisfies BoundedByPolyOfDegree(B,d)\mathrm{BoundedByPolyOfDegree}(B, d)BoundedByPolyOfDegree(B,d) for d∈Nd \in \mathbb{N}d∈N if and only if there exist natural numbers c0,c1∈Nc_0, c_1 \in \mathbb{N}c0​,c1​∈N such that for all n∈Nn \in \mathbb{N}n∈N,

B(n)≤c0 (n+1)d+c1.B(n) \le c_0\,(n + 1)^d + c_1.B(n)≤c0​(n+1)d+c1​.

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