Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

Section 4: Clean Types, Extracted Programs, and Modified Realizability

Definition
ABHS2004_Realizability

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

implicit-computational-complexitypolynomial-timeproof-theoryrealizabilitytype-theory

Formalizes Section 4 of Aehlig, Berger, Hofmann, and Schwichtenberg (2004): the type-cleaning map τ(A) (cleanFormulaTy, cleanTy), Harrop formulas (IsHarropFormula), program extraction ⟦M⟧ (extractProgram, Definition 4.3) mapping natural deduction derivations M to terms of the affine λ-calculus (with ⟦Ind_A⟧ = it_{τ(A)}), and the modified realizability relation r mr A (Realizes, Definition 4.5).

Definition code
import Definitions.Def_ABHS2004_Arithmetic

/-!
# Modified Realizability and Program Extraction (ABHS 2004, Section 4)

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 4 (pp. 20–26).

This module formalizes:
- Section 4.1: Modified realizability `mr r A` (`r mr A`, Definition 4.1, p. 20).
- Section 4.2: Extracted terms `extractProof M` (`⟦M⟧`, Definition 4.3, pp. 20–21) and
  term cleaning `cleanTerm` / `extractCleaned M` (`⟦M⟧^c := c(⟦M⟧)`, Definition 4.4, p. 22).
- Section 4.3: Context extraction `extractCtx`, `extractCleanCtx`, and realizability
  assumptions `mrAssumptions` for Theorem 4.7 (Soundness of typing, p. 23), Corollary 4.8
  (Underivability, p. 23), Theorem 4.9 (Soundness of modified realizability, p. 24),
  and Corollary 4.11 (Main polytime extraction theorem, p. 26).
-/

namespace ABHS2004

/-- Fresh variable id above a term and two formulas, used when introducing a bound variable
in `mr r (A → B)` and `mr r (A ∨ B)` (Definition 4.1, p. 20). -/
def freshMrVarId (r : Term) (A B : Formula) : Nat :=
  Nat.max (maxVarId r) (Nat.max (maxVarIdFormula A) (maxVarIdFormula B)) + 1

/-- Definition 4.1 (Modified realizability `r mr A`, p. 20):
By induction on `A` we define a formula `mr r A` for arbitrary `r^{τ(A)}`.
- `r mr E_ρ(s) := (r =_ρ s)`
- `r mr P(s⃗) := P(s⃗)`
- `r mr ∀x A := ∀x. r mr A` (renaming `x` if `x ∈ FV(r)`)
- `r mr ∃x A := ∃x. r mr A` (renaming `x` if `x ∈ FV(r)`)
- `r mr (A → B) := ∀x. x mr A → r x mr B`
- `r mr (A ⊗ B) := π₀(r) mr A ∧ π₁(r) mr B`
- `r mr (A ∧ B) := fst r mr A ∧ snd r mr B`
- `r mr (A ∨ B) := (∀x. r = inl x → x mr A) ∧ (∀y. r = inr y → y mr B)`. -/
def mr (r : Term) (A : Formula) : Formula :=
  match A with
  | .existPred ρ s => Formula.eqForm ρ r s
  | .pred P ss => .pred P ss
  | .all x B =>
      if x ∈ freeVarOccurrences r then
        let freshId := Nat.max (maxVarId r) (Nat.max (maxVarIdFormula B) x.id) + 1
        let x' : Var := ⟨freshId, x.ty⟩
        .all x' (mr r (renameFreeVarFormula B x x'))
      else
        .all x (mr r B)
  | .ex x B =>
      if x ∈ freeVarOccurrences r then
        let freshId := Nat.max (maxVarId r) (Nat.max (maxVarIdFormula B) x.id) + 1
        let x' : Var := ⟨freshId, x.ty⟩
        .ex x' (mr r (renameFreeVarFormula B x x'))
      else
        .ex x (mr r B)
  | .imp B C =>
      let x : Var := ⟨freshMrVarId r B C, tau B⟩
      .all x (.imp (mr (.var x) B) (mr (.app r (.var x)) C))
  | .tensor B C =>
      let ρ := tau B
      let σ := tau C
      .conj (mr (Term.pi0 ρ σ r) B) (mr (Term.pi1 ρ σ r) C)
  | .conj B C =>
      let ρ := tau B
      let σ := tau C
      .conj
        (mr (.app (.const (.fst ρ σ)) r) B)
        (mr (.app (.const (.snd ρ σ)) r) C)
  | .disj B C =>
      let ρ := tau B
      let σ := tau C
      let baseId := freshMrVarId r B C
      let x : Var := ⟨baseId, ρ⟩
      let y : Var := ⟨baseId + 1, σ⟩
      let inlx := Term.app (.const (.inl ρ σ)) (.var x)
      let inry := Term.app (.const (.inr ρ σ)) (.var y)
      .conj
        (.all x (.imp (Formula.eqForm (.sum ρ σ) r inlx) (mr (.var x) B)))
        (.all y (.imp (Formula.eqForm (.sum ρ σ) r inry) (mr (.var y) C)))
termination_by formulaSize A
decreasing_by
  all_goals
    simp_wf
    simp [formulaSize, renameFreeVarFormula_size]
    try omega

/-- Canonical closed term `ε^ρ` for types whose cleaned type is `U` (or built from dummy terms
for Harrop axiom realizers in Section 4.2, p. 21). -/
def dummyTerm : Ty → Term
  | .unit => .const .eps
  | .diamond => .var ⟨0, .diamond⟩
  | .list ρ => .const (.nil ρ)
  | .lolli ρ σ =>
      let x : Var := ⟨0, ρ⟩
      .lam x (dummyTerm σ)
  | .tensor ρ σ =>
      .app (.app (.const (.tensorIntro ρ σ)) (dummyTerm ρ)) (dummyTerm σ)
  | .prod ρ σ =>
      let u : Var := ⟨0, .unit⟩
      .app (.app (.app (.const (.prodIntro ρ σ .unit))
        (.lam u (dummyTerm ρ)))
        (.lam u (dummyTerm σ)))
        (.const .eps)
  | .sum ρ σ =>
      .app (.const (.inl ρ σ)) (dummyTerm ρ)

/-- Section 4.2 (p. 20): For each assumption variable `u^A` we choose a unique term variable
`x_{u^A}` of type `τ(A)` (using odd variable ids `2 * u.id + 1` to keep them distinct). -/
def assumpRealizerVar (u : AssumpVar) : Var :=
  ⟨2 * u.id + 1, tau u.formula⟩

/-- Definition 4.3 (Extracted terms for core axioms `⟦A⟧`, p. 21). -/
def extractAxiom (ax : CoreAxiom) : Term :=
  match ax with
  | .ax3 A B C => .const (.prodIntro (tau A) (tau B) (tau C))
  | .ax4_left A₀ A₁ => .const (.fst (tau A₀) (tau A₁))
  | .ax4_right A₀ A₁ => .const (.snd (tau A₀) (tau A₁))
  | .ax5 A B => .const (.tensorIntro (tau A) (tau B))
  | .ax6 A B C => .const (.tensorElim (tau A) (tau B) (tau C))
  | .ax7_left A₀ A₁ => .const (.inl (tau A₀) (tau A₁))
  | .ax7_right A₀ A₁ => .const (.inr (tau A₀) (tau A₁))
  | .ax8 A B C =>
      let ρ := tau A
      let σ := tau B
      let τ := tau C
      let z₁ : Var := ⟨0, .prod (.lolli ρ τ) (.lolli σ τ)⟩
      let z₂ : Var := ⟨1, .sum ρ σ⟩
      .lam z₁ (.lam z₂ (.app (.app (.const (.sumElim ρ σ τ)) (.var z₂)) (.var z₁)))
  | .ax9 _ A => Term.idTerm (tau A)
  | .ax10 _ A B =>
      let x : Var := ⟨0, tau A⟩
      let f : Var := ⟨1, .lolli (tau A) (tau B)⟩
      .lam x (.lam f (.app (.var f) (.var x)))
  | .ax16 ρ _ _ => Term.idTerm ρ
  | .ax20_imp ρ σ _ _ => Term.idTerm (.lolli ρ σ)
  | .ax20_mpr ρ σ _ _ => Term.idTerm (.lolli ρ σ)
  | .ax21_imp ρ σ _ => Term.idTerm (.prod ρ σ)
  | .ax21_mpr ρ σ _ => Term.idTerm (.prod ρ σ)
  | .ax22 c => .const c
  | .ax23 ρ σ _ _ _ A =>
      let τ := tau A
      let f : Var := ⟨0, .lolli ρ (.lolli σ τ)⟩
      let z : Var := ⟨1, .tensor ρ σ⟩
      .lam f (.lam z (.app (.app (.const (.tensorElim ρ σ τ)) (.var z)) (.var f)))
  | .ax24 ρ σ _ _ _ A =>
      let τ := tau A
      let z₁ : Var := ⟨0, .prod (.lolli ρ τ) (.lolli σ τ)⟩
      let z₂ : Var := ⟨1, .sum ρ σ⟩
      .lam z₁ (.lam z₂ (.app (.app (.const (.sumElim ρ σ τ)) (.var z₂)) (.var z₁)))
  | other => dummyTerm (tau other.formula)

/-- Definition 4.3 (Extracted terms `⟦M⟧`, pp. 20–21):
- `⟦u^A⟧ := x_u^{τ(A)}`
- `⟦λu^A M^B⟧ := λx_u^{τ(A)} ⟦M⟧`
- `⟦M^{A→B} N⟧ := ⟦M⟧ ⟦N⟧`
- `⟦λx^ρ M^A⟧ := ⟦M⟧`
- `⟦M^{∀x^ρ A} r⟧ := ⟦M⟧`
- `⟦N^{E_{L(ρ)}(t)} {M}⟧ := ⟦N⟧ {⟦M⟧}`. -/
def extractProof : ProofTerm → Term
  | .assump u => .var (assumpRealizerVar u)
  | .ax c => extractAxiom c
  | .lamAssump u M => .lam (assumpRealizerVar u) (extractProof M)
  | .lamTerm _ M => extractProof M
  | .appProof M N => .app (extractProof M) (extractProof N)
  | .appTerm M _ => extractProof M
  | .ind N M => .iter (extractProof N) (extractProof M)

/-- Intrinsic type inference `inferType r` for raw terms (since variables and constants carry
their explicit types in `ABHS2004`). -/
def inferType : Term → Option Ty
  | .var x => some x.ty
  | .const c => some c.ty
  | .lam x r =>
      match inferType r with
      | some σ => some (.lolli x.ty σ)
      | none => none
  | .app r _ =>
      match inferType r with
      | some (.lolli _ σ) => some σ
      | _ => none
  | .iter _ s =>
      match inferType s with
      | some (.lolli .diamond (.lolli _ (.lolli τ _))) => some (.lolli τ τ)
      | _ => none

/-- Definition 4.4 (p. 22): Cleaned variable `x̃^{c(ρ)}` associated with `x^ρ`. -/
def cleanVar (x : Var) : Var :=
  ⟨x.id, cleanTy x.ty⟩

/-- Definition 4.4 (p. 22): Cleaning `c(c)` for constants `c^ρ`. -/
def cleanConst (c : Const) : Term :=
  if cleanTy c.ty == .unit then
    .const .eps
  else
    match c with
    | .eps => .const .eps
    | .nil ρ => .const (.nil (cleanTy ρ))
    | .cons ρ =>
        let cρ := cleanTy ρ
        if cρ == .unit then
          let d : Var := ⟨0, .diamond⟩
          let l : Var := ⟨1, Ty.nat⟩
          .lam d (.lam l (Term.succNat (.var d) (.var l)))
        else
          .const (.cons cρ)
    | .tensorIntro ρ σ =>
        let cρ := cleanTy ρ
        let cσ := cleanTy σ
        if cρ == .unit then Term.idTerm cσ
        else if cσ == .unit then Term.idTerm cρ
        else .const (.tensorIntro cρ cσ)
    | .tensorElim ρ σ τ =>
        let cρ := cleanTy ρ
        let cσ := cleanTy σ
        let cτ := cleanTy τ
        if cρ == .unit && cσ == .unit then
          Term.idTerm cτ
        else if cρ == .unit then
          let x : Var := ⟨0, cσ⟩
          let f : Var := ⟨1, .lolli cσ cτ⟩
          .lam x (.lam f (.app (.var f) (.var x)))
        else if cσ == .unit then
          let x : Var := ⟨0, cρ⟩
          let f : Var := ⟨1, .lolli cρ cτ⟩
          .lam x (.lam f (.app (.var f) (.var x)))
        else
          .const (.tensorElim cρ cσ cτ)
    | .prodIntro ρ σ τ =>
        let cρ := cleanTy ρ
        let cσ := cleanTy σ
        let cτ := cleanTy τ
        if cρ == .unit then
          if cτ == .unit then Term.idTerm cσ
          else
            let g : Var := ⟨0, .lolli cτ cσ⟩
            let z : Var := ⟨1, cτ⟩
            .lam g (.lam z (.app (.var g) (.var z)))
        else if cσ == .unit then
          if cτ == .unit then Term.idTerm cρ
          else
            let f : Var := ⟨0, .lolli cτ cρ⟩
            let z : Var := ⟨1, cτ⟩
            .lam f (.lam z (.app (.var f) (.var z)))
        else if cτ == .unit then
          let x : Var := ⟨0, cρ⟩
          let y : Var := ⟨1, cσ⟩
          let u₁ : Var := ⟨2, .unit⟩
          let u₂ : Var := ⟨3, .unit⟩
          .lam x (.lam y
            (.app (.app (.app (.const (.prodIntro cρ cσ .unit))
              (.lam u₁ (.var x)))
              (.lam u₂ (.var y)))
              (.const .eps)))
        else
          .const (.prodIntro cρ cσ cτ)
    | .fst ρ σ =>
        let cρ := cleanTy ρ
        let cσ := cleanTy σ
        if cσ == .unit then Term.idTerm cρ
        else .const (.fst cρ cσ)
    | .snd ρ σ =>
        let cρ := cleanTy ρ
        let cσ := cleanTy σ
        if cρ == .unit then Term.idTerm cσ
        else .const (.snd cρ cσ)
    | .inl ρ σ =>
        let cρ := cleanTy ρ
        let cσ := cleanTy σ
        if cρ == .unit then .app (.const (.inl .unit cσ)) (.const .eps)
        else .const (.inl cρ cσ)
    | .inr ρ σ =>
        let cρ := cleanTy ρ
        let cσ := cleanTy σ
        if cσ == .unit then .app (.const (.inr cρ .unit)) (.const .eps)
        else .const (.inr cρ cσ)
    | .sumElim ρ σ τ =>
        let cρ := cleanTy ρ
        let cσ := cleanTy σ
        let cτ := cleanTy τ
        let branchL := if cρ == .unit then cτ else .lolli cρ cτ
        let branchR := if cσ == .unit then cτ else .lolli cσ cτ
        if cρ == .unit && cσ == .unit then
          let s : Var := ⟨0, .sum .unit .unit⟩
          let p : Var := ⟨1, .prod cτ cτ⟩
          let u₁ : Var := ⟨2, .unit⟩
          let u₂ : Var := ⟨3, .unit⟩
          let q : Var := ⟨4, .prod cτ cτ⟩
          let adaptedPair :=
            Term.app
              (.app (.app (.const (.prodIntro (.lolli .unit cτ) (.lolli .unit cτ) (.prod cτ cτ)))
                (.lam q (.lam u₁ (.app (.const (.fst cτ cτ)) (.var q)))))
                (.lam q (.lam u₂ (.app (.const (.snd cτ cτ)) (.var q)))))
              (.var p)
          .lam s (.lam p (.app (.app (.const (.sumElim .unit .unit cτ)) (.var s)) adaptedPair))
        else if cρ == .unit then
          let s : Var := ⟨0, .sum .unit cσ⟩
          let p : Var := ⟨1, .prod branchL branchR⟩
          let u₁ : Var := ⟨2, .unit⟩
          let q : Var := ⟨3, .prod branchL branchR⟩
          let adaptedPair :=
            Term.app
              (.app (.app (.const (.prodIntro (.lolli .unit cτ) branchR (.prod branchL branchR)))
                (.lam q (.lam u₁ (.app (.const (.fst branchL branchR)) (.var q)))))
                (.lam q (.app (.const (.snd branchL branchR)) (.var q))))
              (.var p)
          .lam s (.lam p (.app (.app (.const (.sumElim .unit cσ cτ)) (.var s)) adaptedPair))
        else if cσ == .unit then
          let s : Var := ⟨0, .sum cρ .unit⟩
          let p : Var := ⟨1, .prod branchL branchR⟩
          let u₂ : Var := ⟨2, .unit⟩
          let q : Var := ⟨3, .prod branchL branchR⟩
          let adaptedPair :=
            Term.app
              (.app (.app (.const (.prodIntro branchL (.lolli .unit cτ) (.prod branchL branchR)))
                (.lam q (.app (.const (.fst branchL branchR)) (.var q))))
                (.lam q (.lam u₂ (.app (.const (.snd branchL branchR)) (.var q)))))
              (.var p)
          .lam s (.lam p (.app (.app (.const (.sumElim cρ .unit cτ)) (.var s)) adaptedPair))
        else
          .const (.sumElim cρ cσ cτ)

/-- Definition 4.4 (Cleaning of terms `c(r)`, p. 22). -/
def cleanTerm (r : Term) : Term :=
  match inferType r with
  | some ρ =>
      if cleanTy ρ == .unit then
        .const .eps
      else
        match r with
        | .var x => .var (cleanVar x)
        | .const c => cleanConst c
        | .lam x body =>
            if cleanTy x.ty == .unit then
              cleanTerm body
            else
              .lam (cleanVar x) (cleanTerm body)
        | .app t s =>
            match inferType s with
            | some σ =>
                if cleanTy σ == .unit then
                  cleanTerm t
                else
                  .app (cleanTerm t) (cleanTerm s)
            | none => .app (cleanTerm t) (cleanTerm s)
        | .iter t s => .iter (cleanTerm t) (cleanTerm s)
  | none => r

/-- Section 4.3 (p. 23): Cleaned extracted term `⟦M⟧^c := c(⟦M⟧)`. -/
def extractCleaned (M : ProofTerm) : Term :=
  cleanTerm (extractProof M)

/-- Section 4.3 (p. 23): Uncleaned context extraction `⟦Π⟧ := {x_u^{τ(A)} | u^A ∈ Π}`. -/
def extractCtx (Δ : ProofContext) : List Var :=
  Δ.map assumpRealizerVar

/-- Section 4.3 (p. 23): Cleaned context extraction
`⟦Π⟧^c := {x̃_u^{τ^c(A)} | u^A ∈ Π, τ^c(A) ≠ U}`. -/
def extractCleanCtx (Δ : ProofContext) : List Var :=
  (Δ.filter (fun u => tauClean u.formula != .unit)).map (fun u => cleanVar (assumpRealizerVar u))

/-- Section 4.3 (Theorem 4.9, p. 24): Realizability context mapping each assumption `u^B ∈ Π`
to an assumption variable `ũ^{x_u mr B}`. -/
def mrContext (Δ : ProofContext) : ProofContext :=
  Δ.map (fun u => ⟨u.id, mr (.var (assumpRealizerVar u)) u.formula⟩)

/-- A derivation `M` (with passive context `Π` and active context `Γ`) is almost closed
if every assumption in `Π ∪ Γ` has cleaned realizer type `⋄` or `U` (in particular, when
`Π = []` and `Γ = []`, or when `⟦Γ⟧^c` contains only variables of type `⋄`, Corollary 4.11, p. 26). -/
def IsAlmostClosedDerivation (Δ Γ : ProofContext) : Prop :=
  (∀ v ∈ extractCleanCtx Δ, v.ty = .diamond) ∧
  (∀ v ∈ extractCleanCtx Γ, v.ty = .diamond)

/-- Freshness condition from Section 4.2 (p. 20: "For each variable `u^A` we choose a unique
variable `x_{u^A}` of type `τ(A)` that is sufficiently different to all variables used so far"):
all assumption variables in `Δ`, `Γ`, and `M` have pairwise distinct ids when their formulas
differ, and their chosen realizer variables `x_{u^A}` do not occur free in `A` or in the
formulas of `Δ ∪ Γ`. -/
def FreshRealizerVars (Δ Γ : ProofContext) (M : ProofTerm) (A : Formula) : Prop :=
  let allU := Δ ++ Γ ++ allAssumpVars M
  (∀ u ∈ allU, ∀ v ∈ allU, u.id = v.id → u = v) ∧
  (∀ u ∈ allU, assumpRealizerVar u ∉ freeVarsFormula A) ∧
  (∀ u ∈ allU, ∀ w ∈ Δ ++ Γ, assumpRealizerVar u ∉ freeVarsFormula w.formula)

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 4, pp. 18-23.
Read-back

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

Blind Read-back: Def_ABHS2004_Realizability.lean

Modified realizability (freshMrVarId, mr). For a term rrr and formulas A,BA, BA,B, freshMrVarId(r,A,B):=max⁡(maxVarId(r),max⁡(maxVarIdFormula(A),maxVarIdFormula(B)))+1\mathrm{freshMrVarId}(r, A, B) := \max(\mathrm{maxVarId}(r), \max(\mathrm{maxVarIdFormula}(A), \mathrm{maxVarIdFormula}(B))) + 1freshMrVarId(r,A,B):=max(maxVarId(r),max(maxVarIdFormula(A),maxVarIdFormula(B)))+1. Given a raw term rrr and a formula AAA, the modified realizability formula rmrAr \mathbin{\mathrm{mr}} ArmrA (mr(r,A)\mathrm{mr}(r, A)mr(r,A)) is defined by structural recursion on AAA:

  • rmrEρ(s):=(r=ρs)r \mathbin{\mathrm{mr}} E_\rho(s) := (r =_\rho s)rmrEρ​(s):=(r=ρ​s);
  • rmrP(s1,…,sn):=P(s1,…,sn)r \mathbin{\mathrm{mr}} P(s_1, \dots, s_n) := P(s_1, \dots, s_n)rmrP(s1​,…,sn​):=P(s1​,…,sn​) (independent of rrr);
  • rmr(Qx. B)r \mathbin{\mathrm{mr}} (\mathcal{Q} x.\, B)rmr(Qx.B) for Q∈{∀,∃}\mathcal{Q} \in \{\forall, \exists\}Q∈{∀,∃}: if x∈freeVarOccurrences(r)x \in \mathrm{freeVarOccurrences}(r)x∈freeVarOccurrences(r), then Qx′. (rmrrenameFreeVarFormula(B,x,x′))\mathcal{Q} x'.\, (r \mathbin{\mathrm{mr}} \mathrm{renameFreeVarFormula}(B, x, x'))Qx′.(rmrrenameFreeVarFormula(B,x,x′)) where x′:=(max⁡(maxVarId(r),max⁡(maxVarIdFormula(B),x.id))+1,x.ty)x' := (\max(\mathrm{maxVarId}(r), \max(\mathrm{maxVarIdFormula}(B), x.\mathrm{id})) + 1, x.\mathrm{ty})x′:=(max(maxVarId(r),max(maxVarIdFormula(B),x.id))+1,x.ty); otherwise Qx. (rmrB)\mathcal{Q} x.\, (r \mathbin{\mathrm{mr}} B)Qx.(rmrB);
  • rmr(B→C):=∀x. ((xmrB)→(r xmrC))r \mathbin{\mathrm{mr}} (B \to C) := \forall x.\, ((x \mathbin{\mathrm{mr}} B) \to (r\,x \mathbin{\mathrm{mr}} C))rmr(B→C):=∀x.((xmrB)→(rxmrC)), where x:=(freshMrVarId(r,B,C),τ(B))x := (\mathrm{freshMrVarId}(r, B, C), \tau(B))x:=(freshMrVarId(r,B,C),τ(B));
  • rmr(B⊗C):=(π0(τ(B),τ(C),r)mrB)∧(π1(τ(B),τ(C),r)mrC)r \mathbin{\mathrm{mr}} (B \otimes C) := (\pi_0(\tau(B), \tau(C), r) \mathbin{\mathrm{mr}} B) \land (\pi_1(\tau(B), \tau(C), r) \mathbin{\mathrm{mr}} C)rmr(B⊗C):=(π0​(τ(B),τ(C),r)mrB)∧(π1​(τ(B),τ(C),r)mrC);
  • rmr(B∧C):=(fstτ(B),τ(C) rmrB)∧(sndτ(B),τ(C) rmrC)r \mathbin{\mathrm{mr}} (B \land C) := (\mathrm{fst}_{\tau(B),\tau(C)}\,r \mathbin{\mathrm{mr}} B) \land (\mathrm{snd}_{\tau(B),\tau(C)}\,r \mathbin{\mathrm{mr}} C)rmr(B∧C):=(fstτ(B),τ(C)​rmrB)∧(sndτ(B),τ(C)​rmrC);
  • rmr(B∨C):=(∀x. ((r=τ(B)+τ(C)inlτ(B),τ(C) x)→(xmrB)))∧(∀y. ((r=τ(B)+τ(C)inrτ(B),τ(C) y)→(ymrC)))r \mathbin{\mathrm{mr}} (B \lor C) := (\forall x.\, ((r =_{\tau(B)+\tau(C)} \mathrm{inl}_{\tau(B),\tau(C)}\,x) \to (x \mathbin{\mathrm{mr}} B))) \land (\forall y.\, ((r =_{\tau(B)+\tau(C)} \mathrm{inr}_{\tau(B),\tau(C)}\,y) \to (y \mathbin{\mathrm{mr}} C)))rmr(B∨C):=(∀x.((r=τ(B)+τ(C)​inlτ(B),τ(C)​x)→(xmrB)))∧(∀y.((r=τ(B)+τ(C)​inrτ(B),τ(C)​y)→(ymrC))), where x:=(freshMrVarId(r,B,C),τ(B))x := (\mathrm{freshMrVarId}(r, B, C), \tau(B))x:=(freshMrVarId(r,B,C),τ(B)) and y:=(freshMrVarId(r,B,C)+1,τ(C))y := (\mathrm{freshMrVarId}(r, B, C) + 1, \tau(C))y:=(freshMrVarId(r,B,C)+1,τ(C)).

Dummy terms, assumption realizer variables, and uncleaned program extraction (dummyTerm, assumpRealizerVar, extractAxiom, extractProof). For each type ρ∈Ty\rho \in \mathrm{Ty}ρ∈Ty, dummyTerm(ρ)\mathrm{dummyTerm}(\rho)dummyTerm(ρ) constructs a default term of type ρ\rhoρ:

dummyTerm(U)=ε,dummyTerm(⋄)=(0,⋄),dummyTerm(L(ρ))=nilρ,dummyTerm(ρ⊸σ)=λ(0,ρ). dummyTerm(σ),\mathrm{dummyTerm}(U) = \varepsilon, \quad \mathrm{dummyTerm}(\diamond) = (0, \diamond), \quad \mathrm{dummyTerm}(L(\rho)) = \mathrm{nil}_\rho, \quad \mathrm{dummyTerm}(\rho \multimap \sigma) = \lambda (0, \rho).\, \mathrm{dummyTerm}(\sigma),dummyTerm(U)=ε,dummyTerm(⋄)=(0,⋄),dummyTerm(L(ρ))=nilρ​,dummyTerm(ρ⊸σ)=λ(0,ρ).dummyTerm(σ), dummyTerm(ρ⊗σ)=(⊗ρ,σ+ dummyTerm(ρ)) dummyTerm(σ),dummyTerm(ρ+σ)=inlρ,σ dummyTerm(ρ),\mathrm{dummyTerm}(\rho \otimes \sigma) = (\otimes^+_{\rho,\sigma}\,\mathrm{dummyTerm}(\rho))\,\mathrm{dummyTerm}(\sigma), \quad \mathrm{dummyTerm}(\rho + \sigma) = \mathrm{inl}_{\rho,\sigma}\,\mathrm{dummyTerm}(\rho),dummyTerm(ρ⊗σ)=(⊗ρ,σ+​dummyTerm(ρ))dummyTerm(σ),dummyTerm(ρ+σ)=inlρ,σ​dummyTerm(ρ), dummyTerm(ρ×σ)=(((×ρ,σ,U+ (λ(0,U). dummyTerm(ρ))) (λ(0,U). dummyTerm(σ))) ε).\mathrm{dummyTerm}(\rho \times \sigma) = (((\times^+_{\rho,\sigma,U}\,(\lambda (0, U).\, \mathrm{dummyTerm}(\rho)))\,(\lambda (0, U).\, \mathrm{dummyTerm}(\sigma)))\,\varepsilon).dummyTerm(ρ×σ)=(((×ρ,σ,U+​(λ(0,U).dummyTerm(ρ)))(λ(0,U).dummyTerm(σ)))ε).

For each assumption variable u∈AssumpVaru \in \mathrm{AssumpVar}u∈AssumpVar, its associated realizer variable is xu:=assumpRealizerVar(u)=(2 u.id+1,τ(u.formula))x_u := \mathrm{assumpRealizerVar}(u) = (2\,u.\mathrm{id} + 1, \tau(u.\mathrm{formula}))xu​:=assumpRealizerVar(u)=(2u.id+1,τ(u.formula)).

For a core axiom ax∈CoreAxiom\mathit{ax} \in \mathrm{CoreAxiom}ax∈CoreAxiom, extractAxiom(ax)\mathrm{extractAxiom}(\mathit{ax})extractAxiom(ax) is defined by cases:

  • ax3(A,B,CA, B, CA,B,C) ↦×τ(A),τ(B),τ(C)+\mapsto \times^+_{\tau(A),\tau(B),\tau(C)}↦×τ(A),τ(B),τ(C)+​; ax4_left(A0,A1A_0, A_1A0​,A1​) ↦fstτ(A0),τ(A1)\mapsto \mathrm{fst}_{\tau(A_0),\tau(A_1)}↦fstτ(A0​),τ(A1​)​; ax4_right(A0,A1A_0, A_1A0​,A1​) ↦sndτ(A0),τ(A1)\mapsto \mathrm{snd}_{\tau(A_0),\tau(A_1)}↦sndτ(A0​),τ(A1​)​;
  • ax5(A,BA, BA,B) ↦⊗τ(A),τ(B)+\mapsto \otimes^+_{\tau(A),\tau(B)}↦⊗τ(A),τ(B)+​; ax6(A,B,CA, B, CA,B,C) ↦⊗τ(A),τ(B),τ(C)−\mapsto \otimes^-_{\tau(A),\tau(B),\tau(C)}↦⊗τ(A),τ(B),τ(C)−​;
  • ax7_left(A0,A1A_0, A_1A0​,A1​) ↦inlτ(A0),τ(A1)\mapsto \mathrm{inl}_{\tau(A_0),\tau(A_1)}↦inlτ(A0​),τ(A1​)​; ax7_right(A0,A1A_0, A_1A0​,A1​) ↦inrτ(A0),τ(A1)\mapsto \mathrm{inr}_{\tau(A_0),\tau(A_1)}↦inrτ(A0​),τ(A1​)​;
  • ax8(A,B,CA, B, CA,B,C) ↦λz1. λz2. ((+τ(A),τ(B),τ(C)− z2) z1)\mapsto \lambda z_1.\,\lambda z_2.\, ((+^-_{\tau(A),\tau(B),\tau(C)}\,z_2)\,z_1)↦λz1​.λz2​.((+τ(A),τ(B),τ(C)−​z2​)z1​) and ax24(ρ,σ,x,y,z,A\rho, \sigma, x, y, z, Aρ,σ,x,y,z,A) ↦λz1. λz2. ((+ρ,σ,τ(A)− z2) z1)\mapsto \lambda z_1.\,\lambda z_2.\, ((+^-_{\rho,\sigma,\tau(A)}\,z_2)\,z_1)↦λz1​.λz2​.((+ρ,σ,τ(A)−​z2​)z1​), where z1=(0,(ρ⊸τ)×(σ⊸τ))z_1 = (0, (\rho \multimap \tau) \times (\sigma \multimap \tau))z1​=(0,(ρ⊸τ)×(σ⊸τ)) and z2=(1,ρ+σ)z_2 = (1, \rho + \sigma)z2​=(1,ρ+σ);
  • ax9(x,Ax, Ax,A) ↦idTerm(τ(A))\mapsto \mathrm{idTerm}(\tau(A))↦idTerm(τ(A)); ax10(x,A,Bx, A, Bx,A,B) ↦λ(0,τ(A)). λ(1,τ(A)⊸τ(B)). ((1,τ(A)⊸τ(B)) (0,τ(A)))\mapsto \lambda (0, \tau(A)).\,\lambda (1, \tau(A) \multimap \tau(B)).\, ((1, \tau(A) \multimap \tau(B))\,(0, \tau(A)))↦λ(0,τ(A)).λ(1,τ(A)⊸τ(B)).((1,τ(A)⊸τ(B))(0,τ(A)));
  • ax16(ρ,x,y\rho, x, yρ,x,y) ↦idTerm(ρ)\mapsto \mathrm{idTerm}(\rho)↦idTerm(ρ); ax20_imp(ρ,σ,x,f\rho, \sigma, x, fρ,σ,x,f) and ax20_mpr(ρ,σ,x,f\rho, \sigma, x, fρ,σ,x,f) ↦idTerm(ρ⊸σ)\mapsto \mathrm{idTerm}(\rho \multimap \sigma)↦idTerm(ρ⊸σ); ax21_imp(ρ,σ,z\rho, \sigma, zρ,σ,z) and ax21_mpr(ρ,σ,z\rho, \sigma, zρ,σ,z) ↦idTerm(ρ×σ)\mapsto \mathrm{idTerm}(\rho \times \sigma)↦idTerm(ρ×σ);
  • ax22(ccc) ↦c\mapsto c↦c;
  • ax23(ρ,σ,x,y,z,A\rho, \sigma, x, y, z, Aρ,σ,x,y,z,A) ↦λ(0,ρ⊸(σ⊸τ(A))). λ(1,ρ⊗σ). ((⊗ρ,σ,τ(A)− (1,ρ⊗σ)) (0,ρ⊸(σ⊸τ(A))))\mapsto \lambda (0, \rho \multimap (\sigma \multimap \tau(A))).\,\lambda (1, \rho \otimes \sigma).\, ((\otimes^-_{\rho,\sigma,\tau(A)}\,(1, \rho \otimes \sigma))\,(0, \rho \multimap (\sigma \multimap \tau(A))))↦λ(0,ρ⊸(σ⊸τ(A))).λ(1,ρ⊗σ).((⊗ρ,σ,τ(A)−​(1,ρ⊗σ))(0,ρ⊸(σ⊸τ(A))));
  • all other axioms (ax11, ax12_*, ax13, ax14, ax15, ax17, ax18, ax19, ax25) ↦dummyTerm(τ(ax.formula))\mapsto \mathrm{dummyTerm}(\tau(\mathit{ax}.\mathrm{formula}))↦dummyTerm(τ(ax.formula)).

For any proof term M∈ProofTermM \in \mathrm{ProofTerm}M∈ProofTerm, the uncleaned extracted term ⟦M⟧\llbracket M \rrbracket[[M]] (extractProof(M)\mathrm{extractProof}(M)extractProof(M)) is defined recursively by:

⟦u⟧=xu,⟦c⟧=extractAxiom(c),⟦λu. M⟧=λxu. ⟦M⟧,⟦λx. M⟧=⟦M⟧,\llbracket u \rrbracket = x_u, \quad \llbracket c \rrbracket = \mathrm{extractAxiom}(c), \quad \llbracket \lambda u.\, M \rrbracket = \lambda x_u.\, \llbracket M \rrbracket, \quad \llbracket \lambda x.\, M \rrbracket = \llbracket M \rrbracket,[[u]]=xu​,[[c]]=extractAxiom(c),[[λu.M]]=λxu​.[[M]],[[λx.M]]=[[M]], ⟦M N⟧=⟦M⟧ ⟦N⟧,⟦M r⟧=⟦M⟧,⟦N{M}⟧=⟦N⟧{⟦M⟧}.\llbracket M\,N \rrbracket = \llbracket M \rrbracket\,\llbracket N \rrbracket, \quad \llbracket M\,r \rrbracket = \llbracket M \rrbracket, \quad \llbracket N\{M\} \rrbracket = \llbracket N \rrbracket\{\llbracket M \rrbracket\}.[[MN]]=[[M]][[N]],[[Mr]]=[[M]],[[N{M}]]=[[N]]{[[M]]}.

Type inference, term cleaning, and cleaned program extraction (inferType, cleanVar, cleanConst, cleanTerm, extractCleaned). The partial function inferType(r)∈Option(Ty)\mathrm{inferType}(r) \in \mathrm{Option}(\mathrm{Ty})inferType(r)∈Option(Ty) computes a type from intrinsic annotations: some(x.ty)\mathrm{some}(x.\mathrm{ty})some(x.ty) for a variable xxx, some(c.ty)\mathrm{some}(c.\mathrm{ty})some(c.ty) for a constant ccc, some(x.ty⊸σ)\mathrm{some}(x.\mathrm{ty} \multimap \sigma)some(x.ty⊸σ) for λx. r\lambda x.\, rλx.r when inferType(r)=some(σ)\mathrm{inferType}(r) = \mathrm{some}(\sigma)inferType(r)=some(σ), some(σ)\mathrm{some}(\sigma)some(σ) for r sr\,srs when inferType(r)=some(ρ⊸σ)\mathrm{inferType}(r) = \mathrm{some}(\rho \multimap \sigma)inferType(r)=some(ρ⊸σ) (ignoring sss), and some(τ⊸τ)\mathrm{some}(\tau \multimap \tau)some(τ⊸τ) for r{s}r\{s\}r{s} when inferType(s)=some(⋄⊸(ρ⊸(τ⊸τ′)))\mathrm{inferType}(s) = \mathrm{some}(\diamond \multimap (\rho \multimap (\tau \multimap \tau')))inferType(s)=some(⋄⊸(ρ⊸(τ⊸τ′))) (ignoring rrr and not checking τ=τ′\tau = \tau'τ=τ′). For a variable xxx, cleanVar(x):=(x.id,c(x.ty))\mathrm{cleanVar}(x) := (x.\mathrm{id}, c(x.\mathrm{ty}))cleanVar(x):=(x.id,c(x.ty)). For a constant ccc, cleanConst(c)\mathrm{cleanConst}(c)cleanConst(c) returns ε\varepsilonε if c(c.ty)=Uc(c.\mathrm{ty}) = Uc(c.ty)=U, and otherwise simplifies ccc according to which among c(ρ),c(σ),c(τ)c(\rho), c(\sigma), c(\tau)c(ρ),c(σ),c(τ) equal UUU:

  • nilρ↦nilc(ρ)\mathrm{nil}_\rho \mapsto \mathrm{nil}_{c(\rho)}nilρ​↦nilc(ρ)​;
  • consρ↦λ(0,⋄). λ(1,L(U)). succNat((0,⋄),(1,L(U)))\mathrm{cons}_\rho \mapsto \lambda (0, \diamond).\,\lambda (1, L(U)).\, \mathrm{succNat}((0, \diamond), (1, L(U)))consρ​↦λ(0,⋄).λ(1,L(U)).succNat((0,⋄),(1,L(U))) if c(ρ)=Uc(\rho) = Uc(ρ)=U, and consc(ρ)\mathrm{cons}_{c(\rho)}consc(ρ)​ otherwise;
  • ⊗ρ,σ+↦idTerm(c(σ))\otimes^+_{\rho,\sigma} \mapsto \mathrm{idTerm}(c(\sigma))⊗ρ,σ+​↦idTerm(c(σ)) if c(ρ)=Uc(\rho) = Uc(ρ)=U, idTerm(c(ρ))\mathrm{idTerm}(c(\rho))idTerm(c(ρ)) if c(ρ)≠U=c(σ)c(\rho) \neq U = c(\sigma)c(ρ)=U=c(σ), and ⊗c(ρ),c(σ)+\otimes^+_{c(\rho),c(\sigma)}⊗c(ρ),c(σ)+​ otherwise;
  • ⊗ρ,σ,τ−↦idTerm(c(τ))\otimes^-_{\rho,\sigma,\tau} \mapsto \mathrm{idTerm}(c(\tau))⊗ρ,σ,τ−​↦idTerm(c(τ)) if c(ρ)=c(σ)=Uc(\rho) = c(\sigma) = Uc(ρ)=c(σ)=U, λx. λf. (f x)\lambda x.\,\lambda f.\, (f\,x)λx.λf.(fx) if exactly one of c(ρ),c(σ)c(\rho), c(\sigma)c(ρ),c(σ) is UUU, and ⊗c(ρ),c(σ),c(τ)−\otimes^-_{c(\rho),c(\sigma),c(\tau)}⊗c(ρ),c(σ),c(τ)−​ otherwise;
  • ×ρ,σ,τ+\times^+_{\rho,\sigma,\tau}×ρ,σ,τ+​, fstρ,σ\mathrm{fst}_{\rho,\sigma}fstρ,σ​, sndρ,σ\mathrm{snd}_{\rho,\sigma}sndρ,σ​, inlρ,σ\mathrm{inl}_{\rho,\sigma}inlρ,σ​, inrρ,σ\mathrm{inr}_{\rho,\sigma}inrρ,σ​, and +ρ,σ,τ−+^-_{\rho,\sigma,\tau}+ρ,σ,τ−​ similarly specialize to λ\lambdaλ-terms or lower-arity constants whenever one or more of c(ρ),c(σ),c(τ)c(\rho), c(\sigma), c(\tau)c(ρ),c(σ),c(τ) equal UUU.

For a term rrr, cleanTerm(r)\mathrm{cleanTerm}(r)cleanTerm(r) returns rrr unchanged if inferType(r)=none\mathrm{inferType}(r) = \mathrm{none}inferType(r)=none, returns ε\varepsilonε if inferType(r)=some(ρ)\mathrm{inferType}(r) = \mathrm{some}(\rho)inferType(r)=some(ρ) with c(ρ)=Uc(\rho) = Uc(ρ)=U, and otherwise recursively cleans rrr by replacing variables with cleanVar\mathrm{cleanVar}cleanVar, constants with cleanConst\mathrm{cleanConst}cleanConst, dropping λx\lambda xλx when c(x.ty)=Uc(x.\mathrm{ty}) = Uc(x.ty)=U, dropping the argument sss in t st\,sts when inferType(s)=some(σ)\mathrm{inferType}(s) = \mathrm{some}(\sigma)inferType(s)=some(σ) with c(σ)=Uc(\sigma) = Uc(σ)=U, and cleaning both components of t{s}t\{s\}t{s}. The cleaned extracted term of a proof term MMM is ⟦M⟧c:=cleanTerm(⟦M⟧)\llbracket M \rrbracket^c := \mathrm{cleanTerm}(\llbracket M \rrbracket)[[M]]c:=cleanTerm([[M]]) (extractCleaned(M)\mathrm{extractCleaned}(M)extractCleaned(M)).

Context extraction, almost closed derivations, and realizer variable freshness (extractCtx, extractCleanCtx, mrContext, IsAlmostClosedDerivation, FreshRealizerVars). For a proof context Δ=[u1,…,uk]\Delta = [u_1, \dots, u_k]Δ=[u1​,…,uk​], the uncleaned extracted variable context is extractCtx(Δ):=[xu1,…,xuk]\mathrm{extractCtx}(\Delta) := [x_{u_1}, \dots, x_{u_k}]extractCtx(Δ):=[xu1​​,…,xuk​​] where xu=assumpRealizerVar(u)x_u = \mathrm{assumpRealizerVar}(u)xu​=assumpRealizerVar(u); the cleaned extracted variable context extractCleanCtx(Δ)\mathrm{extractCleanCtx}(\Delta)extractCleanCtx(Δ) is the list of cleaned variables cleanVar(xu)\mathrm{cleanVar}(x_u)cleanVar(xu​) for those u∈Δu \in \Deltau∈Δ satisfying τc(u.formula)≠U\tau^c(u.\mathrm{formula}) \neq Uτc(u.formula)=U; and the modified realizability context is mrContext(Δ):=[(u1.id,xu1mru1.formula),…,(uk.id,xukmruk.formula)]\mathrm{mrContext}(\Delta) := [(u_1.\mathrm{id}, x_{u_1} \mathbin{\mathrm{mr}} u_1.\mathrm{formula}), \dots, (u_k.\mathrm{id}, x_{u_k} \mathbin{\mathrm{mr}} u_k.\mathrm{formula})]mrContext(Δ):=[(u1​.id,xu1​​mru1​.formula),…,(uk​.id,xuk​​mruk​.formula)]. A pair of contexts (Δ,Γ)(\Delta, \Gamma)(Δ,Γ) satisfies IsAlmostClosedDerivation(Δ,Γ)\mathrm{IsAlmostClosedDerivation}(\Delta, \Gamma)IsAlmostClosedDerivation(Δ,Γ) if and only if every variable v∈extractCleanCtx(Δ)v \in \mathrm{extractCleanCtx}(\Delta)v∈extractCleanCtx(Δ) has type v.ty=⋄v.\mathrm{ty} = \diamondv.ty=⋄ and every variable v∈extractCleanCtx(Γ)v \in \mathrm{extractCleanCtx}(\Gamma)v∈extractCleanCtx(Γ) has type v.ty=⋄v.\mathrm{ty} = \diamondv.ty=⋄. Finally, given contexts Δ,Γ\Delta, \GammaΔ,Γ, a proof term MMM, and a formula AAA, let allU:=Δ+ ⁣+Γ+ ⁣+allAssumpVars(M)\mathit{allU} := \Delta \mathbin{+\!+} \Gamma \mathbin{+\!+} \mathrm{allAssumpVars}(M)allU:=Δ++Γ++allAssumpVars(M); then FreshRealizerVars(Δ,Γ,M,A)\mathrm{FreshRealizerVars}(\Delta, \Gamma, M, A)FreshRealizerVars(Δ,Γ,M,A) holds if and only if:

  1. for all u,v∈allUu, v \in \mathit{allU}u,v∈allU, u.id=v.idu.\mathrm{id} = v.\mathrm{id}u.id=v.id implies u=vu = vu=v;
  2. for all u∈allUu \in \mathit{allU}u∈allU, xu=assumpRealizerVar(u)∉freeVarsFormula(A)x_u = \mathrm{assumpRealizerVar}(u) \notin \mathrm{freeVarsFormula}(A)xu​=assumpRealizerVar(u)∈/freeVarsFormula(A); and
  3. for all u∈allUu \in \mathit{allU}u∈allU and all w∈Δ+ ⁣+Γw \in \Delta \mathbin{+\!+} \Gammaw∈Δ++Γ, xu=assumpRealizerVar(u)∉freeVarsFormula(w.formula)x_u = \mathrm{assumpRealizerVar}(u) \notin \mathrm{freeVarsFormula}(w.\mathrm{formula})xu​=assumpRealizerVar(u)∈/freeVarsFormula(w.formula).

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