Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

Example 2.20, Lemma 2.22, Corollary 2.23, Example 3.3, and Lemma 3.5: Potential Types, Amortized Interfaces, and Sealing

Proved
Grodin2026.lemma_2_22_corollary_2_23_and_lemma_3_5_potential_and_sealing

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

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

Verifies the conservation equation 0+Φ(l1,n::l2)=Φ(l1,l2)+10 + \Phi(l_1, n :: l_2) = \Phi(l_1, l_2) + 10+Φ(l1​,n::l2​)=Φ(l1​,l2​)+1 and the abs\mathsf{abs}abs-phase amortized specification for ephemeral batched queues (Examples 2.20 & 3.3), proves the bidirectional conservation characterization c⊤(x)+ΦY(f(x))=ΦX(x)+cabs(x)c_\top(x) + \Phi_Y(f(x)) = \Phi_X(x) + c_{\mathsf{abs}}(x)c⊤​(x)+ΦY​(f(x))=ΦX​(x)+cabs​(x) for homomorphisms between Potential\mathsf{Potential}Potential types (Lemma 2.22), establishes one-sided lifting into and out of Abstraction(β)\mathsf{Abstraction}(\beta)Abstraction(β) (Corollary 2.23), and proves the Kleisli bijection (A⊸SB)≃(A⊸ ⁣ ⁣ ⁣ ⁣+B)(A \multimap \mathsf{S}B) \simeq (A \mathrel{\multimap\!\!\!\!+} B)(A⊸SB)≃(A⊸+B), abstract-phase erasure ◯(SA)=◯A\bigcirc(\mathsf{S}A) = \bigcirc A◯(SA)=◯A (Lemma 3.5), and the lax conservation characterization c⊤(x)+ΦY(f(x))≤ΦX(x)+cabs(x)c_\top(x) + \Phi_Y(f(x)) \le \Phi_X(x) + c_{\mathsf{abs}}(x)c⊤​(x)+ΦY​(f(x))≤ΦX​(x)+cabs​(x) (§3.2).

Preamble
import Definitions.Def_Grodin2026_Physicist
Formal statement
namespace Grodin2026

/-- **Example 2.20, Lemma 2.22, Corollary 2.23, Example 3.3, and Lemma 3.5 (§§2.3–3.2):**
1. **Batched Queue Conservation & Amortized Interface (Example 2.20 & Example 3.3):**
   For any element type $\alpha$, concrete queue $(l_1, l_2)$, and element $n : \alpha$,
   $0 + \Phi(l_1, n :: l_2) = \Phi(l_1, l_2) + 1$, and there is a phased queue implementation
   $Q : \mathsf{PhasedPreQueue}\,\alpha$ over `BatchedQueueComp α` whose concrete enqueue is
   `enqueueCompTop` and whose abstract-phase view `Q.absView` is definitionally equal to the
   amortized queue specification `AmortizedQueueModel α`.
2. **Strict Conservation of Potential Characterization (Lemma 2.22):**
   Given $f : X \to Y$, true cost $c_\top : X \to \mathbb{N}$, and amortized cost
   $c_{\mathsf{abs}} : X \to \mathbb{N}$, there exists a strict computation homomorphism
   $g : \mathsf{Potential}(\Phi_X) \multimap \mathsf{Potential}(\Phi_Y)$ with
   $g_\top(\mathsf{ret}\,x) = (c_\top(x), f(x))$ and
   $g_{\mathsf{abs}}(\mathsf{ret}\,x) = (c_{\mathsf{abs}}(x), f(x))$ if and only if
   $c_\top(x) + \Phi_Y(f(x)) = \Phi_X(x) + c_{\mathsf{abs}}(x)$ for all $x : X$.
3. **One-Sided Lifting into/out of $\mathsf{Abstraction}$ (Corollary 2.23):**
   For $B = \mathsf{Abstraction}(\beta : B_\top \multimap B_{\mathsf{abs}})$ and any single-world
   cost algebra $A$, every $f_\top : A \multimap B_\top$ lifts to a computation homomorphism
   $\mathsf{ConstComp}\,A \multimap B$ with abstract component $\beta \circ f_\top$, and every
   $g_{\mathsf{abs}} : B_{\mathsf{abs}} \multimap A$ lifts to a computation homomorphism
   $B \multimap \mathsf{ConstComp}\,A$ with concrete component $g_{\mathsf{abs}} \circ \beta$.
4. **Sealing Monad $\mathsf{S}$, Kleisli Lax Homomorphisms, and Lax Conservation (§3.2 & Lemma 3.5):**
   - For all $A, B : \mathcal{C}$, Kleisli homomorphisms $A \multimap \mathsf{S}B$ are in canonical
     bijection (`Equiv`) with lax homomorphisms $A \mathrel{\multimap\!\!\!\!+} B$;
   - In the abstract phase ($\bigcirc$), sealing is erased: $\bigcirc(\mathsf{S}A) = \bigcirc A$
     (Lemma 3.5);
   - A lax homomorphism $g : \mathsf{Potential}(\Phi_X) \mathrel{\multimap\!\!\!\!+} \mathsf{Potential}(\Phi_Y)$
     with $g_\top(\mathsf{ret}\,x) = (c_\top(x), f(x))$ and
     $g_{\mathsf{abs}}(\mathsf{ret}\,x) = (c_{\mathsf{abs}}(x), f(x))$ exists if and only if the
     lax conservation inequality $c_\top(x) + \Phi_Y(f(x)) \le \Phi_X(x) + c_{\mathsf{abs}}(x)$
     holds for all $x : X$. -/
theorem lemma_2_22_corollary_2_23_and_lemma_3_5_potential_and_sealing :
    (∀ (α : Type),
      (∀ (q : BatchedQueueRep α) (n : α), 0 + queuePhi (enqueueValTop n q) = queuePhi q + 1) ∧
      ∃ enq : α → CompHom (BatchedQueueComp α) (BatchedQueueComp α),
        (∀ n : α, (enq n).hom_top = enqueueCompTop n) ∧
        ({ comp := BatchedQueueComp α
           empty_top := retFree ([], [])
           empty_abs := retFree []
           empty_comm := rfl
           enqueue := enq } : PhasedPreQueue α).absView = AmortizedQueueModel α) ∧
    (∀ {X Y : Type} (Phi_X : X → Nat) (Phi_Y : Y → Nat) (f : X → Y) (c_top c_abs : X → Nat),
      (∃ g : CompHom (PotentialType X Phi_X) (PotentialType Y Phi_Y),
        (∀ x : X, g.hom_top.fn (retFree x) = (c_top x, f x)) ∧
        (∀ x : X, g.hom_abs.fn (retFree x) = (c_abs x, f x))) ↔
      (∀ x : X, c_top x + Phi_Y (f x) = Phi_X x + c_abs x)) ∧
    (∀ (A B_top B_abs : CostAlg) (beta : CostHom B_top B_abs),
      (∀ f_top : CostHom A B_top,
        ∃ f : CompHom (ConstComp A) (AbstractionComp B_top B_abs beta),
          f.hom_top = f_top ∧ f.hom_abs = beta.comp f_top) ∧
      (∀ g_abs : CostHom B_abs A,
        ∃ g : CompHom (AbstractionComp B_top B_abs beta) (ConstComp A),
          g.hom_top = g_abs.comp beta ∧ g.hom_abs = g_abs)) ∧
    (∀ A B : CompType,
      Nonempty (CompHom A (SealComp B) ≃ LaxCompHom A B) ∧
      OpenComp (SealComp A) = OpenComp A) ∧
    (∀ {X Y : Type} (Phi_X : X → Nat) (Phi_Y : Y → Nat) (f : X → Y) (c_top c_abs : X → Nat),
      (∃ g : LaxCompHom (PotentialType X Phi_X) (PotentialType Y Phi_Y),
        (∀ x : X, g.hom_top.fn (retFree x) = (c_top x, f x)) ∧
        (∀ x : X, g.hom_abs.fn (retFree x) = (c_abs x, f x))) ↔
      (∀ x : X, c_top x + Phi_Y (f x) ≤ Phi_X x + c_abs x)) := 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 2.3 & 3.1-3.2, Example 2.20, Lemma 2.22, Corollary 2.23, Example 3.3, Lemma 3.5, pp. 10-13.
Read-back

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

Read-back: The theorem lemma_2_22_corollary_2_23_and_lemma_3_5_potential_and_sealing asserts the conjunction of the following five statements:

  1. Batched Queue Potential Conservation and Amortized Interface: For every type α:Type\alpha : \mathrm{Type}α:Type:
    • for every pair of lists q=(l1,l2)∈List α×List αq = (l_1, l_2) \in \mathsf{List}\,\alpha \times \mathsf{List}\,\alphaq=(l1​,l2​)∈Listα×Listα and element n:αn : \alphan:α, 0+∣(n::l2)∣=∣l2∣+10 + |(n :: l_2)| = |l_2| + 10+∣(n::l2​)∣=∣l2​∣+1; and
    • there exists a family of strict computation homomorphisms enq:α→(BatchedQueueComp α⊸BatchedQueueComp α)\mathit{enq} : \alpha \to (\mathsf{BatchedQueueComp}\,\alpha \multimap \mathsf{BatchedQueueComp}\,\alpha)enq:α→(BatchedQueueCompα⊸BatchedQueueCompα) such that for every n:αn : \alphan:α, (enq(n))⊤=enqueueCompTop(n)(\mathit{enq}(n))_\top = \mathsf{enqueueCompTop}(n)(enq(n))⊤​=enqueueCompTop(n), and the abstract-phase projection absView of the phased pre-queue (BatchedQueueComp α,(0,([],[])),(0,[]),enq)(\mathsf{BatchedQueueComp}\,\alpha, (0, ([], [])), (0, []), \mathit{enq})(BatchedQueueCompα,(0,([],[])),(0,[]),enq) equals AmortizedQueueModel(α)=(F(List α),(0,[]),enqueueCompAbs)\mathsf{AmortizedQueueModel}(\alpha) = (F(\mathsf{List}\,\alpha), (0, []), \mathsf{enqueueCompAbs})AmortizedQueueModel(α)=(F(Listα),(0,[]),enqueueCompAbs).
  2. Characterization of Strict Homomorphisms Between Potential Types: 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, value map f:X→Yf : X \to Yf:X→Y, and cost functions c⊤,cabs:X→Nc_\top, c_{\mathsf{abs}} : X \to \mathbb{N}c⊤​,cabs​:X→N, there exists a strict computation homomorphism g:PotentialType(X,ΦX)⊸PotentialType(Y,ΦY)g : \mathsf{PotentialType}(X, \Phi_X) \multimap \mathsf{PotentialType}(Y, \Phi_Y)g:PotentialType(X,ΦX​)⊸PotentialType(Y,ΦY​) satisfying g⊤(0,x)=(c⊤(x),f(x))g_\top(0, x) = (c_\top(x), f(x))g⊤​(0,x)=(c⊤​(x),f(x)) and gabs(0,x)=(cabs(x),f(x))g_{\mathsf{abs}}(0, x) = (c_{\mathsf{abs}}(x), f(x))gabs​(0,x)=(cabs​(x),f(x)) for all x∈Xx \in Xx∈X if and only if
∀x∈X,c⊤(x)+ΦY(f(x))=ΦX(x)+cabs(x).\forall x \in X,\quad c_\top(x) + \Phi_Y(f(x)) = \Phi_X(x) + c_{\mathsf{abs}}(x).∀x∈X,c⊤​(x)+ΦY​(f(x))=ΦX​(x)+cabs​(x).
  1. One-Sided Lifting into and out of AbstractionComp\mathsf{AbstractionComp}AbstractionComp: For all cost algebras A,B⊤,Babs:CostAlgA, B_\top, B_{\mathsf{abs}} : \mathsf{CostAlg}A,B⊤​,Babs​:CostAlg and every cost algebra homomorphism β:B⊤⊸Babs\beta : B_\top \multimap B_{\mathsf{abs}}β:B⊤​⊸Babs​:
    • for every f⊤:A⊸B⊤f_\top : A \multimap B_\topf⊤​:A⊸B⊤​, there exists f:ConstComp(A)⊸AbstractionComp(B⊤,Babs,β)f : \mathsf{ConstComp}(A) \multimap \mathsf{AbstractionComp}(B_\top, B_{\mathsf{abs}}, \beta)f:ConstComp(A)⊸AbstractionComp(B⊤​,Babs​,β) with f⊤=f⊤f_\top = f_\topf⊤​=f⊤​ and fabs=β∘f⊤f_{\mathsf{abs}} = \beta \circ f_\topfabs​=β∘f⊤​; and
    • for every gabs:Babs⊸Ag_{\mathsf{abs}} : B_{\mathsf{abs}} \multimap Agabs​:Babs​⊸A, there exists g:AbstractionComp(B⊤,Babs,β)⊸ConstComp(A)g : \mathsf{AbstractionComp}(B_\top, B_{\mathsf{abs}}, \beta) \multimap \mathsf{ConstComp}(A)g:AbstractionComp(B⊤​,Babs​,β)⊸ConstComp(A) with g⊤=gabs∘βg_\top = g_{\mathsf{abs}} \circ \betag⊤​=gabs​∘β and gabs=gabsg_{\mathsf{abs}} = g_{\mathsf{abs}}gabs​=gabs​.
  2. Sealing Monad Universal Property and Abstract Erasure: For all phased computation types A,B∈CA, B \in \mathcal{C}A,B∈C:
    • there exists a bijection (Equiv) between strict computation homomorphisms A⊸SealComp(B)A \multimap \mathsf{SealComp}(B)A⊸SealComp(B) and lax computation homomorphisms A⊸ ⁣ ⁣ ⁣ ⁣+BA \mathrel{\multimap\!\!\!\!+} BA⊸+B; and
    • ◯(SealComp(A))=◯A\bigcirc(\mathsf{SealComp}(A)) = \bigcirc A◯(SealComp(A))=◯A.
  3. Characterization of Lax Homomorphisms Between Potential Types: 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, value map f:X→Yf : X \to Yf:X→Y, and cost functions c⊤,cabs:X→Nc_\top, c_{\mathsf{abs}} : X \to \mathbb{N}c⊤​,cabs​:X→N, there exists a lax computation homomorphism g:PotentialType(X,ΦX)⊸ ⁣ ⁣ ⁣ ⁣+PotentialType(Y,ΦY)g : \mathsf{PotentialType}(X, \Phi_X) \mathrel{\multimap\!\!\!\!+} \mathsf{PotentialType}(Y, \Phi_Y)g:PotentialType(X,ΦX​)⊸+PotentialType(Y,ΦY​) satisfying g⊤(0,x)=(c⊤(x),f(x))g_\top(0, x) = (c_\top(x), f(x))g⊤​(0,x)=(c⊤​(x),f(x)) and gabs(0,x)=(cabs(x),f(x))g_{\mathsf{abs}}(0, x) = (c_{\mathsf{abs}}(x), f(x))gabs​(0,x)=(cabs​(x),f(x)) for all x∈Xx \in Xx∈X if and only if
∀x∈X,c⊤(x)+ΦY(f(x))≤ΦX(x)+cabs(x).\forall x \in X,\quad c_\top(x) + \Phi_Y(f(x)) \le \Phi_X(x) + c_{\mathsf{abs}}(x).∀x∈X,c⊤​(x)+ΦY​(f(x))≤ΦX​(x)+cabs​(x).

Assessment: sound

Concerns: None

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