Theorem 5.8, Remark 5.5, and Example 5.18: Soundness of AARA and Certified Cost Inference for Calf Insertion Sort
OpenGrodin2026.theorem_5_8_soundness_of_aara_and_giralf_synthesisProves the Soundness Theorem of AARA (Theorem 5.8) from the Kripke semantics of Calf ( whenever and is cost-erased), proves that pre-paying via preserves concrete execution while publicizing as the amortized cost in the abstract phase (Remark 5.5), constructs the resource-polymorphic cost inference certificate (Definition 5.9, Example 5.18), and derives the certified comparison bound for Calf insertion sort.
import Definitions.Def_Grodin2026_Giralf
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 Grodin2026Read-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:
- Soundness of AARA via Kripke Semantics of Calf: For all positive AARA types , every grade , and every Giralf semantic judgment
if incurs zero abstract cost (IsZeroCostFreeHom e.hom_abs, meaning ), then for every input value , every concrete cost , and every output value such that , we have
- Pre-Payment Reframes the Credit Context as Amortized Cost: For all types , potential functions and , grade , and every Giralf semantic judgment , if holds, then the pre-paid lax homomorphism satisfies
- Resource-Polymorphic Cost Inference Certificate for Insertion Sort: For all , there exists a valid cost inference certificate with inferred input type , inferred output type , and credit grade (witnessing a lax homomorphism whose concrete map is and whose abstract map is the zero-cost erasure ).
- Certified Comparison Bound and Functional Correctness of
isort: For every list of natural numbers ,
Assessment: sound
Concerns: None
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.