Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

Sections 3-5: Definite and Goal Formulas, Program Extraction, and Modified Realizability

Definition
BBS2002_Extraction

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

classical-logicprogram-extractionproof-theoryrealizabilitytype-theory

Formalizes Sections 3-5 of Berger, Buchholz, and Schwichtenberg (2002): relevant and irrelevant formulas, the mutual inductive classes of goal formulas G (IsGoalFormula) and definite formulas D (IsDefiniteFormula), type assignment τ(A), structural program extraction ⟦M⟧, modified realizability r mr_A B, and Section 5 *-aware combinators.

November 2026 correction: the V2 extraction targets use BBS2002_V2_Realizability, which replaces this module's mrFormula and AdmissibleForExtraction with capture-avoiding realizability and comprehension-independent naming conventions. This original module remains active for its formula classes, type assignment, structural extraction, and other shared definitions used by the proved early milestones. Its original realizability-based Theorems 4.1, 5.1, 5.2 and Corollary 5.3 are deprecated.

Definition code
import Definitions.Def_BBS2002_Arithmetic

/-!
# Definite and Goal Formulas, Program Extraction, and Modified Realizability (Berger–Buchholz–Schwichtenberg 2002, Sections 3–5)

Source: Ulrich Berger, Wilfried Buchholz, and Helmut Schwichtenberg,
"Refined program extraction from classical proofs",
*Annals of Pure and Applied Logic* 114, 2002, pp. 3–25 (Sections 3–5, pp. 8–16 / preprint pp. 6–14).

This module formalizes:
- Section 3 (pp. 6–9):
  - Relevant and irrelevant formulas (`isRelevant`, `isIrrelevant`, p. 6).
  - Goal formulas `G` (`IsGoalFormula`) and definite formulas `D` (`IsDefiniteFormula`, p. 6).
- Section 4 (pp. 11–12):
  - Assignment of types or the symbol `*` (`Option Ty`) to formulas: `τ(A)` (`tau`, p. 11).
  - Extracted program `⟦M⟧` (`extractProgram`, pp. 11–12) for a derivation `M` of `A`
    (returning `none` for the symbol `ε` when `τ(A) = *`, and `some t` when `τ(A) ≠ *`).
  - Comprehension terms `A := λy^ν A₀` (`CompTerm`) and the modified realizability relation
    `r mr_A B` (`mrFormula`, p. 12).
- Section 5 (pp. 12–14):
  - `*`-aware application and abstraction (`applyGoalRealizer`, `optLamVars`, `buildGoalCombinatorTerm`,
    `applyOptTerms`, `extractClassicalTerm`) where arguments and binders of type `*` (`none`)
    are omitted (p. 12).
-/

namespace BBS2002

/-- A formula is *relevant* if it "ends" with `⊥` (Section 3, p. 6):
- `⊥` is relevant,
- if `C` is relevant and `B` is arbitrary, then `B → C` is relevant,
- if `C` is relevant, then `∀x C` is relevant. -/
def isRelevant : Formula → Bool
  | .bot => true
  | .imp _ C => isRelevant C
  | .all _ C => isRelevant C
  | _ => false

/-- A formula which is not relevant is called *irrelevant* (Section 3, p. 6). -/
def isIrrelevant (A : Formula) : Bool :=
  !isRelevant A

mutual
  /-- Inductive class of *goal formulas* `G` (Section 3, p. 6):
  - `P` (where `P` is an atomic `L`-formula, including `⊥`)
  - `D → G` provided `D irrelevant ⇒ D quantifier-free` (with `D` definite and `G` goal)
  - `∀x G` provided `G irrelevant` (with `G` goal). -/
  inductive IsGoalFormula : Formula → Prop where
    | atom {P : Formula} :
        isAtomicL P = true →
        FormulaWellTyped P →
        IsGoalFormula P
    | imp {D G : Formula} :
        IsDefiniteFormula D →
        IsGoalFormula G →
        (isIrrelevant D = true → isQuantifierFree D = true) →
        IsGoalFormula (.imp D G)
    | all (x : Var) {G : Formula} :
        IsGoalFormula G →
        isIrrelevant G = true →
        IsGoalFormula (.all x G)

  /-- Inductive class of *definite formulas* `D` (Section 3, p. 6):
  - `P` (where `P` is an atomic `L`-formula, including `⊥`)
  - `G → D` provided `D irrelevant ⇒ G irrelevant` (with `G` goal and `D` definite)
  - `∀x D` (with `D` definite). -/
  inductive IsDefiniteFormula : Formula → Prop where
    | atom {P : Formula} :
        isAtomicL P = true →
        FormulaWellTyped P →
        IsDefiniteFormula P
    | imp {G D : Formula} :
        IsGoalFormula G →
        IsDefiniteFormula D →
        (isIrrelevant D = true → isIrrelevant G = true) →
        IsDefiniteFormula (.imp G D)
    | all (x : Var) {D : Formula} :
        IsDefiniteFormula D →
        IsDefiniteFormula (.all x D)
end

/-- Section 4 (p. 11): Assignment `τ(A)` of a type (`some ρ`) or the symbol `*` (`none`)
to every formula `A ∈ L[X]`, assuming a proof of `X` carries computational content of type `ν`:
- `τ(X) := ν`
- `τ(P) := *` (in particular `τ(⊥) = *`)
- `τ(∀x^ρ A) := *` if `τ(A) = *`, else `ρ → τ(A)`
- `τ(A → B) := τ(B)` if `τ(A) = *`, `*` if `τ(B) = *`, else `τ(A) → τ(B)`. -/
def tau (ν : Ty) : Formula → Option Ty
  | .varX => some ν
  | .bot | .atom _ => none
  | .all x A =>
      match tau ν A with
      | none => none
      | some σ => some (.arr x.ty σ)
  | .imp A B =>
      match tau ν A, tau ν B with
      | none, τB => τB
      | some _, none => none
      | some τA, some τB => some (.arr τA τB)

/-- Convert an assumption variable `u^A` into its realizer variable `u^{τ(A)}` when `τ(A) = some ρ`,
or `none` (`ε`) when `τ(A) = *` (Section 4, pp. 11–12). -/
def assumpRealizerVar (ν : Ty) (u : AssumpVar) : Option Var :=
  (tau ν u.formula).map (fun ρ => ⟨u.id, ρ⟩)

/-- Realizer term or `ε` (`none`) associated with an assumption variable `u^A` (Section 4, p. 12). -/
def assumpRealizerTerm (ν : Ty) (u : AssumpVar) : Option Term :=
  (assumpRealizerVar ν u).map Term.var

/-- Closed dummy term of type `ρ` built from `dummy^ν : ν` (used for `⟦efq_A⟧`, pp. 11–12). -/
def efqTerm (ν : Ty) (dummy : Term) (ρ : Ty) : Term :=
  if ρ == ν then dummy else defaultTerm ρ

/-- Raw structural Extraction `extractRaw ν dummy M` for a derivation `M` with `τ(A) ≠ *`
(Section 4, pp. 11–12):
- `⟦u^A⟧ := u^{τ(A)}`
- `⟦λu^A M⟧ := ⟦M⟧` if `τ(A) = *`, else `λu^{τ(A)} ⟦M⟧`
- `⟦M^{A→B} N⟧ := ⟦M⟧` if `τ(A) = *`, else `⟦M⟧ ⟦N⟧`
- `⟦λx^ρ M⟧ := λx^ρ ⟦M⟧`
- `⟦M t⟧ := ⟦M⟧ t`
- `⟦Ind_{p,A}⟧ := R_{o,ρ}` with `ρ := τ(A) ≠ *`
- `⟦Ind_{n,A}⟧ := R_{ι,ρ}` with `ρ := τ(A) ≠ *`
- `⟦efq_X⟧ := dummy^ν` (and `efqTerm ν dummy ρ` for general `efq_A` with `τ(A) = some ρ`). -/
def extractRaw (ν : Ty) (dummy : Term) : ProofTerm → Option Term
  | .assump u => assumpRealizerTerm ν u
  | .indBool _ A =>
      (tau ν A).map (fun ρ => .const (.recBool ρ))
  | .indNat _ A =>
      (tau ν A).map (fun ρ => .const (.recNat ρ))
  | .axTrue => none
  | .axFalse A =>
      (tau ν A).map (fun ρ => efqTerm ν dummy ρ)
  | .efq A =>
      (tau ν A).map (fun ρ => efqTerm ν dummy ρ)
  | .lamAssump u M =>
      match tau ν u.formula with
      | none => extractRaw ν dummy M
      | some ρ => (extractRaw ν dummy M).map (fun body => .lam ⟨u.id, ρ⟩ body)
  | .appAssump M N =>
      match extractRaw ν dummy M, extractRaw ν dummy N with
      | none, _ => none
      | some m, none => some m
      | some m, some n => some (.app m n)
  | .lamTerm x M =>
      (extractRaw ν dummy M).map (fun body => .lam x body)
  | .appTerm M t =>
      (extractRaw ν dummy M).map (fun body => .app body t)

/-- Section 4 (pp. 11–12): Extracted program `⟦M⟧` of a derivation `M` of formula `A`:
returns `none` (`ε`) if `τ(A) = *`, and `extractRaw ν dummy M` if `τ(A) ≠ *`. -/
def extractProgram (ν : Ty) (dummy : Term) (M : ProofTerm) (A : Formula) : Option Term :=
  match tau ν A with
  | none => none
  | some _ => extractRaw ν dummy M

/-- Comprehension term `A := λy^ν A₀` with an `L`-formula `A₀` (Section 4, p. 12). -/
structure CompTerm (ν : Ty) where
  var : Var
  body : Formula
  var_ty : var.ty = ν
  body_isL : isLFormula body = true
  body_wt : FormulaWellTyped body

namespace CompTerm

/-- Apply a comprehension term `A(r) := A₀[y^ν := r]` (Section 4, p. 12). -/
def apply {ν : Ty} (comp : CompTerm ν) (r : Term) : Formula :=
  substFormula comp.body comp.var r

end CompTerm

/-- Fresh variable id above `A`, `B`, `comp.body`, and `r`. -/
def freshMrVarId {ν : Ty} (comp : CompTerm ν) (r : Option Term) (A B : Formula) : Nat :=
  let rMax := match r with | none => 0 | some t => maxVarIdTerm t
  Nat.max
    (Nat.max (maxVarIdFormula A) (maxVarIdFormula B))
    (Nat.max (maxVarIdFormula comp.body) (Nat.max comp.var.id rMax)) + 1

/-- Section 4 (p. 12): Modified realizability formula `r mr_A A` (`mrFormula ν comp r A`),
where `r` is `some t` if `τ(A) = some ρ`, or `none` (`ε`) if `τ(A) = *`:
- `r mr_A X = A(r)`
- `r mr_A P = P` for atomic `L`-formulas `P`
- `r mr_A ∀x A = ∀x. ε mr_A A` if `τ(A) = *`, else `∀x. (r x) mr_A A`
- `r mr_A (A → B) =`:
  - `ε mr_A A → r mr_A B` if `τ(A) = *`
  - `∀x. x mr_A A → ε mr_A B` if `τ(A) ≠ * = τ(B)`
  - `∀x. x mr_A A → (r x) mr_A B` if `τ(A) ≠ *` and `τ(B) ≠ *`. -/
def mrFormula (ν : Ty) (comp : CompTerm ν) (r : Option Term) : Formula → Formula
  | .varX =>
      match r with
      | some t => comp.apply t
      | none => comp.apply (defaultTerm ν)
  | .bot => .bot
  | .atom t => .atom t
  | .all x A =>
      match tau ν A with
      | none => .all x (mrFormula ν comp none A)
      | some _ =>
          let rx := r.map (fun t => .app t (.var x))
          .all x (mrFormula ν comp rx A)
  | .imp A B =>
      match tau ν A, tau ν B with
      | none, _ =>
          .imp (mrFormula ν comp none A) (mrFormula ν comp r B)
      | some τA, none =>
          let x : Var := ⟨freshMrVarId comp r A B, τA⟩
          .all x (.imp (mrFormula ν comp (some (.var x)) A) (mrFormula ν comp none B))
      | some τA, some _ =>
          let x : Var := ⟨freshMrVarId comp r A B, τA⟩
          let rx := r.map (fun t => .app t (.var x))
          .all x (.imp (mrFormula ν comp (some (.var x)) A) (mrFormula ν comp rx B))

/-- Conjunction of hypotheses `(G₁ → X) → ⋯ → (Gₘ → X) → X` written `(G⃗ → X) → X`
in Lemma 3.2, Theorem 3.3, and Theorem 5.1 (pp. 9, 12–13):
note that in the paper `∀y⃗(G⃗ → ⊥) → ⊥` abbreviates `∀y⃗(G₁ → ⋯ → Gₘ → ⊥) → ⊥`. -/
def goalImpliesBot (ys : List Var) (Gs : List Formula) : Formula :=
  Formula.neg (Formula.allMany ys (Formula.impMany Gs .bot))

/-- Formula `∀y⃗(G⃗ → X) → X` (Theorem 3.3, p. 9). -/
def goalImpliesX (ys : List Var) (Gs : List Formula) : Formula :=
  .imp (Formula.allMany ys (Formula.impMany Gs .varX)) .varX

/-- Section 5 convention (pp. 12–13): apply goal realizer `s : Term` to `w : Option Var`
(omitted if `w = none`, i.e. of type `*`) and then to `v : Term`. -/
def applyGoalRealizer (s : Term) (w : Option Var) (v : Term) : Term :=
  match w with
  | none => .app s v
  | some wVar => .app (.app s (.var wVar)) v

/-- Section 5 convention (p. 12): Multi-variable abstraction `λw₁ ... wₘ. t` where variables
of type `*` (`none`) are omitted (`λw^* t` stands for `t`). -/
def optLamVars : List (Option Var) → Term → Term
  | [], t => t
  | none :: ws, t => optLamVars ws t
  | some w :: ws, t => .lam w (optLamVars ws t)

/-- Combined realizer `s := λy⃗ λw⃗. s₁ w₁ (... (sₘ wₘ (r y⃗)) ...)` in Theorem 5.1 (p. 13),
using the Section 5 `*`-aware conventions (`applyGoalRealizer`, `optLamVars`) for variables of type `*`. -/
def buildGoalCombinatorTerm
    (ys : List Var) (ws : List (Option Var)) (ss : List Term) (r : Term) : Term :=
  let ry : Term := Term.appMany r (ys.map Term.var)
  let body : Term := (List.zip ss ws).foldr
    (fun (si, wi) acc => applyGoalRealizer si wi acc)
    ry
  Term.lamMany ys (optLamVars ws body)

/-- Section 5 convention (p. 12): Apply `p : Term` to `t₁, ..., tₙ : List (Option Term)`,
where arguments of type `*` (`none`) are omitted (`p t` stands for `p` when `t` has type `*`). -/
def applyOptTerms : Term → List (Option Term) → Term
  | p, [] => p
  | p, none :: ts => applyOptTerms p ts
  | p, some t :: ts => applyOptTerms (.app p t) ts

/-- Extracted program term `⟦M^X⟧ t₁ ... tₙ s` in Theorem 5.1 (p. 13) and Corollary 5.3 (p. 14). -/
def extractClassicalTerm
    (ν : Ty) (dummy : Term) (M : ProofTerm)
    (ts : List (Option Term)) (s : Term) : Term :=
  let p := (extractRaw ν dummy (substBotProof M)).getD dummy
  .app (applyOptTerms p ts) s

/-- All assumption variables (free or bound) occurring in a derivation `M`. -/
def allAssumpVars : ProofTerm → List AssumpVar
  | .assump u => [u]
  | .indNat _ _ | .indBool _ _ | .axTrue | .axFalse _ | .efq _ => []
  | .lamAssump u M => u :: allAssumpVars M
  | .appAssump M N => allAssumpVars M ++ allAssumpVars N
  | .lamTerm _ M => allAssumpVars M
  | .appTerm M _ => allAssumpVars M

/-- All object variables (free or bound) occurring in a derivation `M`. -/
def allVarsProof : ProofTerm → List Var
  | .assump u => allVarsFormula u.formula
  | .indNat n A => n :: allVarsFormula A
  | .indBool p A => p :: allVarsFormula A
  | .axTrue => []
  | .axFalse A | .efq A => allVarsFormula A
  | .lamAssump u M => allVarsFormula u.formula ++ allVarsProof M
  | .appAssump M N => allVarsProof M ++ allVarsProof N
  | .lamTerm x M => x :: allVarsProof M
  | .appTerm M t => allVarsProof M ++ allVarsTerm t

/-- Variable-hygiene and well-typedness condition for program extraction from a derivation `M`
with respect to `dummy : Term` of type `ν` and comprehension term `comp : CompTerm ν` (Section 4, pp. 11–12):
- `dummy` is a closed term of type `ν`,
- assumption variable IDs in `M` are disjoint from object variable IDs in `M` and `comp`,
- assumption variables in `M` with the same `id` carry the same formula,
- object variables bound in `M` are disjoint from the free variables of `comp.body`. -/
def AdmissibleForExtraction (ν : Ty) (dummy : Term) (comp : CompTerm ν) (M : ProofTerm) : Prop :=
  HasType dummy ν ∧
  IsClosedTerm dummy ∧
  (∀ u ∈ allAssumpVars M, ∀ x ∈ allVarsProof M ++ allVarsFormula comp.body, u.id ≠ x.id) ∧
  (∀ u₁ ∈ allAssumpVars M, ∀ u₂ ∈ allAssumpVars M, u₁.id = u₂.id → u₁.formula = u₂.formula) ∧
  (∀ x ∈ allVarsProof M, x = comp.var ∨ x ∉ freeVarsFormula comp.body)

end BBS2002
Source
Ulrich Berger, Wilfried Buchholz, and Helmut Schwichtenberg, 'Refined program extraction from classical proofs', Annals of Pure and Applied Logic 114(1-3), 2002, pp. 3-25 (doi:10.1016/S0168-0072(01)00073-2). Sections 3-5, pp. 8-16 (preprint pp. 6-14).
Read-back

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

Independent Mathematical Read-Back: Def_BBS2002_Extraction.lean

isRelevant and isIrrelevant (Relevant and Irrelevant Formulas). isRelevant is a boolean function on formulas defined by structural recursion:

  • isRelevant(⊥)=true\mathrm{isRelevant}(\bot) = \mathsf{true}isRelevant(⊥)=true,
  • isRelevant(B→C)=isRelevant(C)\mathrm{isRelevant}(B \to C) = \mathrm{isRelevant}(C)isRelevant(B→C)=isRelevant(C),
  • isRelevant(∀x. C)=isRelevant(C)\mathrm{isRelevant}(\forall x.\, C) = \mathrm{isRelevant}(C)isRelevant(∀x.C)=isRelevant(C),
  • isRelevant(X)=false\mathrm{isRelevant}(X) = \mathsf{false}isRelevant(X)=false and isRelevant(atom(t))=false\mathrm{isRelevant}(\mathrm{atom}(t)) = \mathsf{false}isRelevant(atom(t))=false.

Equivalently, a formula is relevant if and only if its rightmost/innermost target subformula (after stripping all implication antecedents and universal quantifiers) is ⊥\bot⊥. isIrrelevant A is the boolean negation ¬ isRelevant(A)\lnot\,\mathrm{isRelevant}(A)¬isRelevant(A).

IsGoalFormula and IsDefiniteFormula (Goal and Definite Formulas). IsGoalFormula and IsDefiniteFormula are mutually defined inductive predicates on Formula:

  • Goal formulas (IsGoalFormula):
    1. (atom) Any formula PPP satisfying isAtomicL(P)=true\mathrm{isAtomicL}(P) = \mathsf{true}isAtomicL(P)=true (i.e., P=⊥P = \botP=⊥ or P=atom(t)P = \mathrm{atom}(t)P=atom(t)) and FormulaWellTyped(P)\mathrm{FormulaWellTyped}(P)FormulaWellTyped(P) is a goal formula.
    2. (imp) If DDD satisfies IsDefiniteFormula D, GGG satisfies IsGoalFormula G, and isIrrelevant(D)=true  ⟹  isQuantifierFree(D)=true\mathrm{isIrrelevant}(D) = \mathsf{true} \implies \mathrm{isQuantifierFree}(D) = \mathsf{true}isIrrelevant(D)=true⟹isQuantifierFree(D)=true, then D→GD \to GD→G is a goal formula.
    3. (all) If GGG satisfies IsGoalFormula G and isIrrelevant(G)=true\mathrm{isIrrelevant}(G) = \mathsf{true}isIrrelevant(G)=true, then ∀x. G\forall x.\, G∀x.G is a goal formula for any object variable x:Varx : \mathrm{Var}x:Var.
  • Definite formulas (IsDefiniteFormula):
    1. (atom) Any formula PPP satisfying isAtomicL(P)=true\mathrm{isAtomicL}(P) = \mathsf{true}isAtomicL(P)=true and FormulaWellTyped(P)\mathrm{FormulaWellTyped}(P)FormulaWellTyped(P) is a definite formula.
    2. (imp) If GGG satisfies IsGoalFormula G, DDD satisfies IsDefiniteFormula D, and isIrrelevant(D)=true  ⟹  isIrrelevant(G)=true\mathrm{isIrrelevant}(D) = \mathsf{true} \implies \mathrm{isIrrelevant}(G) = \mathsf{true}isIrrelevant(D)=true⟹isIrrelevant(G)=true, then G→DG \to DG→D is a definite formula.
    3. (all) If DDD satisfies IsDefiniteFormula D, then ∀x. D\forall x.\, D∀x.D is a definite formula for any object variable x:Varx : \mathrm{Var}x:Var.

Because the base case requires isAtomicL(P)=true\mathrm{isAtomicL}(P) = \mathsf{true}isAtomicL(P)=true and FormulaWellTyped(P)\mathrm{FormulaWellTyped}(P)FormulaWellTyped(P), every formula satisfying IsGoalFormula or IsDefiniteFormula is necessarily a well-typed L\mathcal{L}L-formula (free of XXX).

tau (Type Assignment τν(A)\tau_\nu(A)τν​(A)). Given a simple type ν:Ty\nu : \mathrm{Ty}ν:Ty (representing the computational type assigned to XXX), tau ν A assigns an optional type τν(A):Option Ty\tau_\nu(A) : \mathrm{Option}\,\mathrm{Ty}τν​(A):OptionTy (where none\mathrm{none}none represents the nullary symbol ∗*∗ and some ρ\mathrm{some}\,\rhosomeρ represents the type ρ\rhoρ) to each formula A∈L[X]A \in \mathcal{L}[X]A∈L[X]:

τν(X)=some ν,τν(⊥)=τν(atom(t))=none(∗),τν(∀xρ. A)={noneif τν(A)=none,some(ρ→σ)if τν(A)=some σ,τν(A→B)={τν(B)if τν(A)=none,noneif τν(A)=some τA and τν(B)=none,some(τA→τB)if τν(A)=some τA and τν(B)=some τB.\begin{aligned} \tau_\nu(X) &= \mathrm{some}\,\nu, \\ \tau_\nu(\bot) = \tau_\nu(\mathrm{atom}(t)) &= \mathrm{none} \quad (*), \\ \tau_\nu(\forall x^\rho.\, A) &= \begin{cases} \mathrm{none} & \text{if } \tau_\nu(A) = \mathrm{none}, \\ \mathrm{some}(\rho \to \sigma) & \text{if } \tau_\nu(A) = \mathrm{some}\,\sigma, \end{cases} \\ \tau_\nu(A \to B) &= \begin{cases} \tau_\nu(B) & \text{if } \tau_\nu(A) = \mathrm{none}, \\ \mathrm{none} & \text{if } \tau_\nu(A) = \mathrm{some}\,\tau_A \text{ and } \tau_\nu(B) = \mathrm{none}, \\ \mathrm{some}(\tau_A \to \tau_B) & \text{if } \tau_\nu(A) = \mathrm{some}\,\tau_A \text{ and } \tau_\nu(B) = \mathrm{some}\,\tau_B. \end{cases} \end{aligned}τν​(X)τν​(⊥)=τν​(atom(t))τν​(∀xρ.A)τν​(A→B)​=someν,=none(∗),={nonesome(ρ→σ)​if τν​(A)=none,if τν​(A)=someσ,​=⎩⎨⎧​τν​(B)nonesome(τA​→τB​)​if τν​(A)=none,if τν​(A)=someτA​ and τν​(B)=none,if τν​(A)=someτA​ and τν​(B)=someτB​.​​

assumpRealizerVar, assumpRealizerTerm, efqTerm, extractRaw, and extractProgram (Program Extraction ⟦M⟧\llbracket M \rrbracket[[M]]). For a type ν:Ty\nu : \mathrm{Ty}ν:Ty and a fallback term dummy:Term\mathrm{dummy} : \mathrm{Term}dummy:Term:

  • For an assumption variable uA:AssumpVaru^A : \mathrm{AssumpVar}uA:AssumpVar, assumpRealizerVar ν u returns some ⟨u.id,ρ⟩\mathrm{some}\,\langle u.\mathrm{id}, \rho\ranglesome⟨u.id,ρ⟩ when τν(A)=some ρ\tau_\nu(A) = \mathrm{some}\,\rhoτν​(A)=someρ and none\mathrm{none}none when τν(A)=none\tau_\nu(A) = \mathrm{none}τν​(A)=none; assumpRealizerTerm ν u wraps that variable in Term.var.
  • For any type ρ:Ty\rho : \mathrm{Ty}ρ:Ty, efqTerm ν dummy ρ returns dummy\mathrm{dummy}dummy if ρ=ν\rho = \nuρ=ν, and defaultTerm ρ otherwise.
  • extractRaw ν dummy M recursively extracts an optional System T\mathsf{T}T term from a proof term M:ProofTermM : \mathrm{ProofTerm}M:ProofTerm:
    • assump u ↦assumpRealizerTerm(ν,u)\mapsto \mathrm{assumpRealizerTerm}(\nu, u)↦assumpRealizerTerm(ν,u);
    • indBool _ A ↦some(Ro,ρ)\mapsto \mathrm{some}(R_{o,\rho})↦some(Ro,ρ​) if τν(A)=some ρ\tau_\nu(A) = \mathrm{some}\,\rhoτν​(A)=someρ, else none\mathrm{none}none;
    • indNat _ A ↦some(Rι,ρ)\mapsto \mathrm{some}(R_{\iota,\rho})↦some(Rι,ρ​) if τν(A)=some ρ\tau_\nu(A) = \mathrm{some}\,\rhoτν​(A)=someρ, else none\mathrm{none}none;
    • axTrue ↦none\mapsto \mathrm{none}↦none;
    • axFalse A and efq A ↦some(efqTerm(ν,dummy,ρ))\mapsto \mathrm{some}(\mathrm{efqTerm}(\nu, \mathrm{dummy}, \rho))↦some(efqTerm(ν,dummy,ρ)) if τν(A)=some ρ\tau_\nu(A) = \mathrm{some}\,\rhoτν​(A)=someρ, else none\mathrm{none}none;
    • lamAssump u M ↦extractRaw(ν,dummy,M)\mapsto \mathrm{extractRaw}(\nu, \mathrm{dummy}, M)↦extractRaw(ν,dummy,M) if τν(u.formula)=none\tau_\nu(u.\mathrm{formula}) = \mathrm{none}τν​(u.formula)=none, and (extractRaw(ν,dummy,M)).map(λbody⇒λ⟨u.id,ρ⟩. body)(\mathrm{extractRaw}(\nu, \mathrm{dummy}, M)).\mathrm{map}(\lambda \mathrm{body} \Rightarrow \lambda\langle u.\mathrm{id}, \rho\rangle.\,\mathrm{body})(extractRaw(ν,dummy,M)).map(λbody⇒λ⟨u.id,ρ⟩.body) if τν(u.formula)=some ρ\tau_\nu(u.\mathrm{formula}) = \mathrm{some}\,\rhoτν​(u.formula)=someρ;
    • appAssump M N ↦none\mapsto \mathrm{none}↦none if extractRaw(ν,dummy,M)=none\mathrm{extractRaw}(\nu, \mathrm{dummy}, M) = \mathrm{none}extractRaw(ν,dummy,M)=none; some m\mathrm{some}\,msomem if extractRaw(ν,dummy,M)=some m\mathrm{extractRaw}(\nu, \mathrm{dummy}, M) = \mathrm{some}\,mextractRaw(ν,dummy,M)=somem and extractRaw(ν,dummy,N)=none\mathrm{extractRaw}(\nu, \mathrm{dummy}, N) = \mathrm{none}extractRaw(ν,dummy,N)=none; and some(m n)\mathrm{some}(m\, n)some(mn) if extractRaw(ν,dummy,M)=some m\mathrm{extractRaw}(\nu, \mathrm{dummy}, M) = \mathrm{some}\,mextractRaw(ν,dummy,M)=somem and extractRaw(ν,dummy,N)=some n\mathrm{extractRaw}(\nu, \mathrm{dummy}, N) = \mathrm{some}\,nextractRaw(ν,dummy,N)=somen;
    • lamTerm x M ↦(extractRaw(ν,dummy,M)).map(λbody⇒λx. body)\mapsto (\mathrm{extractRaw}(\nu, \mathrm{dummy}, M)).\mathrm{map}(\lambda \mathrm{body} \Rightarrow \lambda x.\,\mathrm{body})↦(extractRaw(ν,dummy,M)).map(λbody⇒λx.body);
    • appTerm M t ↦(extractRaw(ν,dummy,M)).map(λbody⇒body t)\mapsto (\mathrm{extractRaw}(\nu, \mathrm{dummy}, M)).\mathrm{map}(\lambda \mathrm{body} \Rightarrow \mathrm{body}\, t)↦(extractRaw(ν,dummy,M)).map(λbody⇒bodyt).
  • extractProgram ν dummy M A (⟦M⟧\llbracket M \rrbracket[[M]]) returns none\mathrm{none}none (ε\varepsilonε) if τν(A)=none\tau_\nu(A) = \mathrm{none}τν​(A)=none, and extractRaw(ν,dummy,M)\mathrm{extractRaw}(\nu, \mathrm{dummy}, M)extractRaw(ν,dummy,M) if τν(A)=some ρ\tau_\nu(A) = \mathrm{some}\,\rhoτν​(A)=someρ.

CompTerm, CompTerm.apply, freshMrVarId, and mrFormula (Comprehension Terms and Modified Realizability). CompTerm ν is a structure representing a comprehension predicate A=λyν. A0\mathcal{A} = \lambda y^\nu.\, A_0A=λyν.A0​, consisting of an object variable y=var:Vary = \mathrm{var} : \mathrm{Var}y=var:Var and a formula A0=body:FormulaA_0 = \mathrm{body} : \mathrm{Formula}A0​=body:Formula together with proofs that y.ty=νy.\mathrm{ty} = \nuy.ty=ν, isLFormula(A0)=true\mathrm{isLFormula}(A_0) = \mathsf{true}isLFormula(A0​)=true, and FormulaWellTyped(A0)\mathrm{FormulaWellTyped}(A_0)FormulaWellTyped(A0​). comp.apply r defines A(r):=A0[y:=r]\mathcal{A}(r) := A_0[y := r]A(r):=A0​[y:=r] via capture-avoiding substitution (substFormula). Given comp:CompTerm ν\mathrm{comp} : \mathrm{CompTerm}\,\nucomp:CompTermν and an optional realizer r:Option Termr : \mathrm{Option}\,\mathrm{Term}r:OptionTerm, mrFormula ν comp r C defines the modified realizability formula r  mrA  Cr \;\mathsf{mr}_{\mathcal{A}}\; CrmrA​C by structural recursion on CCC:

  • r  mrA  X=A(t)r \;\mathsf{mr}_{\mathcal{A}}\; X = \mathcal{A}(t)rmrA​X=A(t) if r=some tr = \mathrm{some}\,tr=somet, and A(defaultTerm(ν))\mathcal{A}(\mathrm{defaultTerm}(\nu))A(defaultTerm(ν)) if r=noner = \mathrm{none}r=none;
  • r  mrA  ⊥=⊥r \;\mathsf{mr}_{\mathcal{A}}\; \bot = \botrmrA​⊥=⊥, and r  mrA  atom(t)=atom(t)r \;\mathsf{mr}_{\mathcal{A}}\; \mathrm{atom}(t) = \mathrm{atom}(t)rmrA​atom(t)=atom(t);
  • r  mrA  (∀x. A)=∀x. (none  mrA  A)r \;\mathsf{mr}_{\mathcal{A}}\; (\forall x.\, A) = \forall x.\, (\mathrm{none} \;\mathsf{mr}_{\mathcal{A}}\; A)rmrA​(∀x.A)=∀x.(nonemrA​A) if τν(A)=none\tau_\nu(A) = \mathrm{none}τν​(A)=none, and ∀x. ((r.map(λt⇒t x))  mrA  A)\forall x.\, \bigl((r.\mathrm{map}(\lambda t \Rightarrow t\, x)) \;\mathsf{mr}_{\mathcal{A}}\; A\bigr)∀x.((r.map(λt⇒tx))mrA​A) if τν(A)=some σ\tau_\nu(A) = \mathrm{some}\,\sigmaτν​(A)=someσ;
  • r  mrA  (A→B)r \;\mathsf{mr}_{\mathcal{A}}\; (A \to B)rmrA​(A→B) is defined by cases on τν(A)\tau_\nu(A)τν​(A) and τν(B)\tau_\nu(B)τν​(B), letting x:=⟨freshMrVarId(comp,r,A,B),τA⟩x := \langle \mathrm{freshMrVarId}(\mathrm{comp}, r, A, B), \tau_A\ranglex:=⟨freshMrVarId(comp,r,A,B),τA​⟩ where freshMrVarId picks 111 plus the maximum variable id\mathrm{id}id occurring in AAA, BBB, A0A_0A0​, yyy, and rrr:
r  mrA  (A→B)={(none  mrA  A)→(r  mrA  B)if τν(A)=none,∀xτA. ((some x  mrA  A)→(none  mrA  B))if τν(A)=some τA and τν(B)=none,∀xτA. ((some x  mrA  A)→(r.map(λt⇒t x)  mrA  B))if τν(A)=some τA and τν(B)=some τB.\begin{aligned} r \;\mathsf{mr}_{\mathcal{A}}\; (A \to B) = \begin{cases} (\mathrm{none} \;\mathsf{mr}_{\mathcal{A}}\; A) \to (r \;\mathsf{mr}_{\mathcal{A}}\; B) & \text{if } \tau_\nu(A) = \mathrm{none}, \\ \forall x^{\tau_A}.\, \bigl((\mathrm{some}\,x \;\mathsf{mr}_{\mathcal{A}}\; A) \to (\mathrm{none} \;\mathsf{mr}_{\mathcal{A}}\; B)\bigr) & \text{if } \tau_\nu(A) = \mathrm{some}\,\tau_A \text{ and } \tau_\nu(B) = \mathrm{none}, \\ \forall x^{\tau_A}.\, \bigl((\mathrm{some}\,x \;\mathsf{mr}_{\mathcal{A}}\; A) \to (r.\mathrm{map}(\lambda t \Rightarrow t\, x) \;\mathsf{mr}_{\mathcal{A}}\; B)\bigr) & \text{if } \tau_\nu(A) = \mathrm{some}\,\tau_A \text{ and } \tau_\nu(B) = \mathrm{some}\,\tau_B. \end{cases} \end{aligned}rmrA​(A→B)=⎩⎨⎧​(nonemrA​A)→(rmrA​B)∀xτA​.((somexmrA​A)→(nonemrA​B))∀xτA​.((somexmrA​A)→(r.map(λt⇒tx)mrA​B))​if τν​(A)=none,if τν​(A)=someτA​ and τν​(B)=none,if τν​(A)=someτA​ and τν​(B)=someτB​.​​

goalImpliesBot, goalImpliesX, and Section 5 ∗*∗-Aware Combinators (applyGoalRealizer, optLamVars, buildGoalCombinatorTerm, applyOptTerms, extractClassicalTerm). For a list of object variables y⃗=[y1,…,yk]\vec{y} = [y_1, \dots, y_k]y​=[y1​,…,yk​] and a list of formulas G⃗=[G1,…,Gm]\vec{G} = [G_1, \dots, G_m]G=[G1​,…,Gm​]:

  • goalImpliesBot ys Gs is the formula ¬(∀y1…∀yk. (G1→⋯→Gm→⊥))=(∀y1…∀yk. (G1→⋯→Gm→⊥))→⊥\neg\bigl(\forall y_1 \dots \forall y_k.\, (G_1 \to \cdots \to G_m \to \bot)\bigr) = \bigl(\forall y_1 \dots \forall y_k.\, (G_1 \to \cdots \to G_m \to \bot)\bigr) \to \bot¬(∀y1​…∀yk​.(G1​→⋯→Gm​→⊥))=(∀y1​…∀yk​.(G1​→⋯→Gm​→⊥))→⊥.
  • goalImpliesX ys Gs is the formula (∀y1…∀yk. (G1→⋯→Gm→X))→X\bigl(\forall y_1 \dots \forall y_k.\, (G_1 \to \cdots \to G_m \to X)\bigr) \to X(∀y1​…∀yk​.(G1​→⋯→Gm​→X))→X.
  • applyGoalRealizer s w v returns s vs\, vsv if w=nonew = \mathrm{none}w=none, and (s wvar) v(s\, w_{\mathrm{var}})\, v(swvar​)v if w=some wvarw = \mathrm{some}\,w_{\mathrm{var}}w=somewvar​.
  • optLamVars ws t abstracts over the variables in ws:List (Option Var)\mathrm{ws} : \mathrm{List}\,(\mathrm{Option}\,\mathrm{Var})ws:List(OptionVar) from left to right, omitting entries that are none\mathrm{none}none.
  • buildGoalCombinatorTerm ys ws ss r constructs the term
λy1…λyk. optLamVars(ws,  foldr (λ(si,wi) acc⇒applyGoalRealizer(si,wi,acc)) (r y1…yk) (zip(ss,ws))),\lambda y_1 \dots \lambda y_k.\, \mathrm{optLamVars}\bigl(\mathrm{ws},\; \mathrm{foldr}\,\bigl(\lambda (s_i, w_i)\,\mathrm{acc} \Rightarrow \mathrm{applyGoalRealizer}(s_i, w_i, \mathrm{acc})\bigr)\,(r\, y_1 \dots y_k)\,(\mathrm{zip}(\mathrm{ss}, \mathrm{ws}))\bigr),λy1​…λyk​.optLamVars(ws,foldr(λ(si​,wi​)acc⇒applyGoalRealizer(si​,wi​,acc))(ry1​…yk​)(zip(ss,ws))),

which for ∣ss∣=∣ws∣=m|\mathrm{ss}| = |\mathrm{ws}| = m∣ss∣=∣ws∣=m represents λy⃗ λw⃗. s1 w1 (⋯(sm wm (r y⃗))⋯ )\lambda \vec{y}\,\lambda \vec{w}.\, s_1\, w_1\,\bigl(\cdots\bigl(s_m\, w_m\,(r\,\vec{y})\bigr)\cdots\bigr)λy​λw.s1​w1​(⋯(sm​wm​(ry​))⋯) with each wi=nonew_i = \mathrm{none}wi​=none omitted in both binder and argument positions (if ∣ss∣≠∣ws∣|\mathrm{ss}| \neq |\mathrm{ws}|∣ss∣=∣ws∣, List.zip truncates to min⁡(∣ss∣,∣ws∣)\min(|\mathrm{ss}|, |\mathrm{ws}|)min(∣ss∣,∣ws∣)).

  • applyOptTerms p ts applies p:Termp : \mathrm{Term}p:Term from left to right to the some tj\mathrm{some}\,t_jsometj​ entries of ts:List (Option Term)\mathrm{ts} : \mathrm{List}\,(\mathrm{Option}\,\mathrm{Term})ts:List(OptionTerm), skipping none\mathrm{none}none entries.
  • extractClassicalTerm ν dummy M ts s lets p:=(extractRaw(ν,dummy,MX)).getD(dummy)p := (\mathrm{extractRaw}(\nu, \mathrm{dummy}, M^X)).\mathrm{getD}(\mathrm{dummy})p:=(extractRaw(ν,dummy,MX)).getD(dummy) (falling back to dummy\mathrm{dummy}dummy if extractRaw returns none\mathrm{none}none) and returns (applyOptTerms(p,ts)) s(\mathrm{applyOptTerms}(p, \mathrm{ts}))\, s(applyOptTerms(p,ts))s.

allAssumpVars, allVarsProof, and AdmissibleForExtraction (Hygiene Conditions for Extraction). allAssumpVars M collects all assumption variables (free or bound) occurring in MMM, and allVarsProof M collects all object variables occurring anywhere in MMM (including inside formula annotations and term arguments). AdmissibleForExtraction ν dummy comp M is the conjunction of five conditions:

  1. HasType(dummy,ν)\mathrm{HasType}(\mathrm{dummy}, \nu)HasType(dummy,ν) (dummy\mathrm{dummy}dummy has type ν\nuν);
  2. IsClosedTerm(dummy)\mathrm{IsClosedTerm}(\mathrm{dummy})IsClosedTerm(dummy) (dummy\mathrm{dummy}dummy has no free object variables);
  3. for every u∈allAssumpVars(M)u \in \mathrm{allAssumpVars}(M)u∈allAssumpVars(M) and every x∈allVarsProof(M)++allVarsFormula(comp.body)x \in \mathrm{allVarsProof}(M) \mathbin{++} \mathrm{allVarsFormula}(\mathrm{comp.body})x∈allVarsProof(M)++allVarsFormula(comp.body), u.id≠x.idu.\mathrm{id} \neq x.\mathrm{id}u.id=x.id (assumption variable numeric IDs are disjoint from object variable numeric IDs in MMM and comp.body\mathrm{comp.body}comp.body);
  4. for every u1,u2∈allAssumpVars(M)u_1, u_2 \in \mathrm{allAssumpVars}(M)u1​,u2​∈allAssumpVars(M), if u1.id=u2.idu_1.\mathrm{id} = u_2.\mathrm{id}u1​.id=u2​.id then u1.formula=u2.formulau_1.\mathrm{formula} = u_2.\mathrm{formula}u1​.formula=u2​.formula (distinct assumption variables in MMM cannot share a numeric ID);
  5. for every x∈allVarsProof(M)x \in \mathrm{allVarsProof}(M)x∈allVarsProof(M), either x=comp.varx = \mathrm{comp.var}x=comp.var or x∉freeVarsFormula(comp.body)x \notin \mathrm{freeVarsFormula}(\mathrm{comp.body})x∈/freeVarsFormula(comp.body).
Human review
  • Endorsed by Shuze Chen · Oct 31, 2026

    Confirmed by the moderator at approval.

  • Endorsed by kmehltretter82 · Oct 31, 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