Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

Theorem 5.8, Remark 5.5, and Example 5.18: Soundness of AARA and Certified Cost Inference for Calf Insertion Sort

Open
Grodin2026.theorem_5_8_soundness_of_aara_and_giralf_synthesis

by kmehltretter82 · Nov 7, 2026 · Mathlib 0df444a (Lean v4.33.1)

amortized-analysiscost-analysismodal-type-theorysubstructural-type-systemstype-theory

Proves the Soundness Theorem of AARA (Theorem 5.8) from the Kripke semantics of Calf (p+ΦA(v)≤ΦΔ(δ)+qp + \Phi_A(v) \le \Phi_\Delta(\delta) + qp+ΦA​(v)≤ΦΔ​(δ)+q whenever e⊤(ret δ)=(p,v)e_\top(\mathsf{ret}\,\delta) = (p, v)e⊤​(retδ)=(p,v) and eabse_{\mathsf{abs}}eabs​ is cost-erased), proves that pre-paying via e∘save⟨q⟩e \circ \mathsf{save}\langle q \ranglee∘save⟨q⟩ preserves concrete execution while publicizing qqq as the amortized cost in the abstract phase (Remark 5.5), constructs the resource-polymorphic cost inference certificate CList2(p1,p2+1)(FN)⊢0isort‾:CList2(p1,p2)(FN)\mathsf{CList}^{(p_1, p_2 + 1)}_2(F\mathbb{N}) \vdash^0 \underline{\mathsf{isort}} : \mathsf{CList}^{(p_1, p_2)}_2(F\mathbb{N})CList2(p1​,p2​+1)​(FN)⊢0isort​:CList2(p1​,p2​)​(FN) (Definition 5.9, Example 5.18), and derives the certified (∣l∣2)\binom{|l|}{2}(2∣l∣​) comparison bound for Calf insertion sort.

Preamble
import Definitions.Def_Grodin2026_Giralf
Formal statement
namespace Grodin2026

/-- **Theorem 5.8, Remark 5.5, Definition 5.9, and Example 5.18 (§§5.2–5.3):**
1. **Soundness of AARA via Kripke Semantics of Calf (Theorem 5.8):**
   Let $\Delta, A : \mathsf{AARAType}$ be positive AARA types and let
   $e : \mathsf{GiralfSemJudgment}\,(\mathsf{PotentialType}\,\llbracket \Delta \rrbracket\,\Phi_\Delta)\,(\mathsf{PotentialType}\,\llbracket A \rrbracket\,\Phi_A)\,q$
   be a Giralf program whose abstract phase incurs zero cost (`IsZeroCostFreeHom e.hom_abs`).
   If evaluating $e_\top$ on environment $\delta : \llbracket \Delta \rrbracket$ (`retFree δ`)
   yields $(p, v) : \mathbb{N} \times \llbracket A \rrbracket$, then $v = (e_{\mathsf{abs}}(\mathsf{ret}\,\delta)).2$
   and the AARA conservation inequality holds:
   $$p + \Phi_A(v) \le \Phi_\Delta(\delta) + q.$$
2. **Pre-Payment Reframes Credit Context as Amortized Cost (Remark 5.5):**
   For any Giralf program $e : \mathsf{GiralfSemJudgment}\,(\mathsf{PotentialType}\,X\,\Phi_X)\,(\mathsf{PotentialType}\,Y\,\Phi_Y)\,q$
   with `IsZeroCostFreeHom e.hom_abs`, pre-paying via `prepayGiralf e = e.comp (saveComp q _).toLax`
   preserves the concrete program `(prepayGiralf e).hom_top = e.hom_top` while publicizing $q$ as
   the amortized cost in the abstract phase:
   $\forall x : X,\; ((\mathsf{prepayGiralf}\,e)_{\mathsf{abs}}(\mathsf{ret}\,x)).1 = q$.
3. **Resource-Polymorphic Cost Inference and Certified $\binom{|l|}{2}$ Bound for Calf Insertion Sort (Definition 5.9 & Example 5.18):**
   - For all $p_1, p_2 : \mathbb{N}$, `isortTopHom` and `isortAbsHom` inhabit the resource-polymorphic
     Giralf judgment
     $l : \mathsf{CList}^{(p_1, p_2 + 1)}_2(F\mathbb{N}) \vdash^0 \underline{\mathsf{isort}}\,l : \mathsf{CList}^{(p_1, p_2)}_2(F\mathbb{N})$
     and form a valid `CostInferenceCertificate isortTopHom isortAbsHom`;
   - Setting $p_1 = p_2 = 0$, for every input list $l : \mathsf{List}\,\mathbb{N}$,
     $(\mathsf{isortCostTop}\,l).2 = \mathsf{isortPure}\,l$ and
     $(\mathsf{isortCostTop}\,l).1 \le \binom{|l|}{2}$. -/
theorem theorem_5_8_soundness_of_aara_and_giralf_synthesis :
    (∀ (Delta A : AARAType) (q : Nat)
       (e : GiralfSemJudgment
         (PotentialType (aaraValues Delta) (aaraPotential Delta))
         (PotentialType (aaraValues A) (aaraPotential A)) q),
      IsZeroCostFreeHom e.hom_abs →
      ∀ (delta : aaraValues Delta) (p : Nat) (v : aaraValues A),
        e.hom_top.fn (retFree delta) = (p, v) →
        v = (e.hom_abs.fn (retFree delta)).2 ∧
        p + aaraPotential A v ≤ aaraPotential Delta delta + q) ∧
    (∀ {X Y : Type} (Phi_X : X → Nat) (Phi_Y : Y → Nat) (q : Nat)
       (e : GiralfSemJudgment (PotentialType X Phi_X) (PotentialType Y Phi_Y) q),
      IsZeroCostFreeHom e.hom_abs →
      (prepayGiralf e).hom_top = e.hom_top ∧
      ∀ x : X, ((prepayGiralf e).hom_abs.fn (retFree x)).1 = q) ∧
    (∀ p₁ p₂ : Nat,
      ∃ cert : CostInferenceCertificate isortTopHom isortAbsHom,
        cert.A_inf = CList2Comp p₁ (p₂ + 1) Nat ∧
        cert.B_inf = CList2Comp p₁ p₂ Nat ∧
        cert.q = 0) ∧
    (∀ l : List Nat,
      (isortCostTop l).2 = isortPure l ∧
      (isortCostTop l).1 ≤ binom2 l.length) := by
  sorry

end Grodin2026
Source
Harrison Grodin, Ethan Chu, Runming Li, Jan Hoffmann, and Robert Harper, 'Potential Functions as Types: A Synthetic Modal Formulation of Amortized Cost', arXiv:2607.08547v2, 2026. Sections 5.2-5.3, Remark 5.5, Theorem 5.8, Definition 5.9, Example 5.18, pp. 19-24.
Read-back

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

Read-back: The theorem theorem_5_8_soundness_of_aara_and_giralf_synthesis asserts the conjunction of the following four statements:

  1. Soundness of AARA via Kripke Semantics of Calf: For all positive AARA types Δ,A∈AARAType\Delta, A \in \mathsf{AARAType}Δ,A∈AARAType, every grade q∈Nq \in \mathbb{N}q∈N, and every Giralf semantic judgment
e:▹q(PotentialType(⟦Δ⟧,ΦΔ))⊸ ⁣ ⁣ ⁣ ⁣+PotentialType(⟦A⟧,ΦA),e : \triangleright^q\bigl(\mathsf{PotentialType}(\llbracket \Delta \rrbracket, \Phi_\Delta)\bigr) \mathrel{\multimap\!\!\!\!+} \mathsf{PotentialType}(\llbracket A \rrbracket, \Phi_A),e:▹q(PotentialType([[Δ]],ΦΔ​))⊸+PotentialType([[A]],ΦA​),

if eabse_{\mathsf{abs}}eabs​ incurs zero abstract cost (IsZeroCostFreeHom e.hom_abs, meaning ∀c∈N,δ∈⟦Δ⟧,  (eabs(c,δ))1=c\forall c \in \mathbb{N}, \delta \in \llbracket \Delta \rrbracket,\; (e_{\mathsf{abs}}(c, \delta))_1 = c∀c∈N,δ∈[[Δ]],(eabs​(c,δ))1​=c), then for every input value δ∈⟦Δ⟧\delta \in \llbracket \Delta \rrbracketδ∈[[Δ]], every concrete cost p∈Np \in \mathbb{N}p∈N, and every output value v∈⟦A⟧v \in \llbracket A \rrbracketv∈[[A]] such that e⊤(0,δ)=(p,v)e_\top(0, \delta) = (p, v)e⊤​(0,δ)=(p,v), we have

v=(eabs(0,δ))2andp+ΦA(v)≤ΦΔ(δ)+q.v = (e_{\mathsf{abs}}(0, \delta))_2 \quad\text{and}\quad p + \Phi_A(v) \le \Phi_\Delta(\delta) + q.v=(eabs​(0,δ))2​andp+ΦA​(v)≤ΦΔ​(δ)+q.
  1. Pre-Payment Reframes the Credit Context as Amortized Cost: For all types X,Y:TypeX, Y : \mathrm{Type}X,Y:Type, potential functions ΦX:X→N\Phi_X : X \to \mathbb{N}ΦX​:X→N and ΦY:Y→N\Phi_Y : Y \to \mathbb{N}ΦY​:Y→N, grade q∈Nq \in \mathbb{N}q∈N, and every Giralf semantic judgment e:▹q(PotentialType(X,ΦX))⊸ ⁣ ⁣ ⁣ ⁣+PotentialType(Y,ΦY)e : \triangleright^q(\mathsf{PotentialType}(X, \Phi_X)) \mathrel{\multimap\!\!\!\!+} \mathsf{PotentialType}(Y, \Phi_Y)e:▹q(PotentialType(X,ΦX​))⊸+PotentialType(Y,ΦY​), if IsZeroCostFreeHom(eabs)\mathsf{IsZeroCostFreeHom}(e_{\mathsf{abs}})IsZeroCostFreeHom(eabs​) holds, then the pre-paid lax homomorphism prepayGiralf(e)=e∘(save⟨q⟩).toLax:PotentialType(X,ΦX)⊸ ⁣ ⁣ ⁣ ⁣+PotentialType(Y,ΦY)\mathsf{prepayGiralf}(e) = e \circ (\mathsf{save}\langle q \rangle).\mathsf{toLax} : \mathsf{PotentialType}(X, \Phi_X) \mathrel{\multimap\!\!\!\!+} \mathsf{PotentialType}(Y, \Phi_Y)prepayGiralf(e)=e∘(save⟨q⟩).toLax:PotentialType(X,ΦX​)⊸+PotentialType(Y,ΦY​) satisfies
(prepayGiralf(e))⊤=e⊤and∀x∈X,  ((prepayGiralf(e))abs(0,x))1=q.(\mathsf{prepayGiralf}(e))_\top = e_\top \quad\text{and}\quad \forall x \in X,\; \bigl((\mathsf{prepayGiralf}(e))_{\mathsf{abs}}(0, x)\bigr)_1 = q.(prepayGiralf(e))⊤​=e⊤​and∀x∈X,((prepayGiralf(e))abs​(0,x))1​=q.
  1. Resource-Polymorphic Cost Inference Certificate for Insertion Sort: For all p1,p2∈Np_1, p_2 \in \mathbb{N}p1​,p2​∈N, there exists a valid cost inference certificate cert∈CostInferenceCertificate(isortTopHom,isortAbsHom)\mathit{cert} \in \mathsf{CostInferenceCertificate}(\mathsf{isortTopHom}, \mathsf{isortAbsHom})cert∈CostInferenceCertificate(isortTopHom,isortAbsHom) with inferred input type cert.Ainf=CList2(p1,p2+1)(FN)\mathit{cert}.A_{\mathrm{inf}} = \mathsf{CList}^{(p_1, p_2 + 1)}_2(F\mathbb{N})cert.Ainf​=CList2(p1​,p2​+1)​(FN), inferred output type cert.Binf=CList2(p1,p2)(FN)\mathit{cert}.B_{\mathrm{inf}} = \mathsf{CList}^{(p_1, p_2)}_2(F\mathbb{N})cert.Binf​=CList2(p1​,p2​)​(FN), and credit grade cert.q=0\mathit{cert}.q = 0cert.q=0 (witnessing a lax homomorphism ▹0(CList2(p1,p2+1)(FN))⊸ ⁣ ⁣ ⁣ ⁣+CList2(p1,p2)(FN)\triangleright^0(\mathsf{CList}^{(p_1, p_2 + 1)}_2(F\mathbb{N})) \mathrel{\multimap\!\!\!\!+} \mathsf{CList}^{(p_1, p_2)}_2(F\mathbb{N})▹0(CList2(p1​,p2​+1)​(FN))⊸+CList2(p1​,p2​)​(FN) whose concrete map is isortTopHom\mathsf{isortTopHom}isortTopHom and whose abstract map is the zero-cost erasure isortAbsHom\mathsf{isortAbsHom}isortAbsHom).
  2. Certified (∣l∣2)\binom{|l|}{2}(2∣l∣​) Comparison Bound and Functional Correctness of isort: For every list of natural numbers l∈List Nl \in \mathsf{List}\,\mathbb{N}l∈ListN,
(isortCostTop(l))2=isortPure(l)and(isortCostTop(l))1≤binom2(∣l∣)=(∣l∣2).(\mathsf{isortCostTop}(l))_2 = \mathsf{isortPure}(l) \quad\text{and}\quad (\mathsf{isortCostTop}(l))_1 \le \mathsf{binom2}(|l|) = \binom{|l|}{2}.(isortCostTop(l))2​=isortPure(l)and(isortCostTop(l))1​≤binom2(∣l∣)=(2∣l∣​).

Assessment: sound

Concerns: None

Human review
  • Flagged by Shuze Chen · Nov 16, 2026

    Quantifies over semantic lax homomorphisms, not AARA typing judgments, so the soundness clause is immediate from the definition; the only substance is the insertion sort comparison bound.

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