Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

Section 5: Giralf Graded Judgments, Positive AARA Types, Insertion Sort, and Cost Inference Annotations

Definition
Grodin2026_Giralf

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

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

Formalizes Section 5 of Grodin, Chu, Li, Hoffmann & Harper (2026): cartesian (lazy) products ProdComp and coproducts SumComp of computation types, the Calf semantics of Giralf judgments GiralfSemJudgment Delta A q (▹qΔ⊸ ⁣ ⁣ ⁣ ⁣+A\triangleright^q \Delta \mathrel{\multimap\!\!\!\!+} A▹qΔ⊸+A, §5.2), pre-payment prepayGiralf (Remark 5.5), giralfStore, giralfSpend, giralfPair, giralfProj1, giralfProj2, giralfGet, and giralfPay (§5.1), the positive AARA type grammar AARAType, value sets aaraValues (⟦A⟧\llbracket A \rrbracket[[A]]), potential functions aaraPotential (ΦA:⟦A⟧→N\Phi_A : \llbracket A \rrbracket \to \mathbb{N}ΦA​:[[A]]→N, Figure 2), compositional Calf interpretation interpAARAType (Theorem 5.7), cost-instrumented insertion sort insertCostTop and isortCostTop (Examples 5.2, 5.3, 5.18), CostInferenceCertificate (Definition 5.9), and skeletal recursor annotations ListRecAnnotation, ValidSnocRecAnnotation, and snocSymbolicAnnotation (Lemmas 5.15-5.16, Examples 5.14 & 5.17).

Definition code
import Definitions.Def_Grodin2026_Banker

/-!
# Giralf: A Graded, Inferential, Resource-Aware Logical Framework (Grodin et al., 2026, §5)

Constructive Lean 4 formalization of Section 5 of:
Harrison Grodin, Ethan Chu, Runming Li, Jan Hoffmann, and Robert Harper (2026),
*Potential Functions as Types: A Synthetic Modal Formulation of Amortized Cost*
(`arXiv:2607.08547v2`).

This module formalizes:
- **§5.1:** The graded judgment structure $\Delta \vdash^q e : A$ of Giralf, standard linear and
  negative types (lazy products $A_1 \times A_2$), credit/debit rules (`store⟨p⟩`, `release`,
  `get⟨p⟩`, `pay`), linear- and quadratic-credit list rules, the derivable credit-aware
  paramorphism (Lemma 5.1), and insertion sort (`insert` and `isort`, Examples 5.2–5.3).
- **§5.2:** The resource-aware Calf semantics of $\Delta \vdash^q e : A$ as a lax homomorphism
  $\triangleright^q(\bigotimes \Delta) \mathrel{\multimap\!\!\!\!+} A$, pre-paying via
  $e \circ \mathsf{save}\langle q \rangle$ (Remark 5.5), the positive AARA type grammar, values
  $\llbracket A \rrbracket$, and potential functions $\Phi_A : \llbracket A \rrbracket \to \mathbb{N}$
  (Figure 2), the Calf interpretation of AARA types (Theorem 5.7), and Soundness of AARA
  (Theorem 5.8).
- **§5.3:** Cost inference $a : \underline{A} \vdash^q \underline{e} : \underline{B}$
  (Definition 5.9), concrete $\neg\mathsf{abs}$ erasure (Lemma 5.10, Corollary 5.11), skeletal type
  translations and pointwise operations $\oplus$ and $k \odot -$ on cost-free and cost-aware
  annotations (Lemmas 5.15–5.16), and the `snoc` / `isort` inference examples
  (Examples 5.13, 5.14, 5.17, 5.18).
-/

namespace Grodin2026

/-! ## §5.1–5.2 Positive & Negative Calf Connectives and Graded Giralf Semantics -/

/-- Cartesian (lazy) product $A_1 \times A_2$ of single-world cost algebras (§5.1.1):
carrier is $UA_1 \times UA_2$ with componentwise cost action
$\mathsf{charge}_{A_1 \times A_2}\langle c \rangle(a_1, a_2) =
(\mathsf{charge}_{A_1}\langle c \rangle(a_1), \mathsf{charge}_{A_2}\langle c \rangle(a_2))$. -/
def ProdCostAlg (A₁ A₂ : CostAlg) : CostAlg where
  carrier := A₁.carrier × A₂.carrier
  le := fun p q => A₁.le p.1 q.1 ∧ A₂.le p.2 q.2
  le_refl := fun p => ⟨A₁.le_refl p.1, A₂.le_refl p.2⟩
  le_trans := fun ⟨h₁, h₂⟩ ⟨k₁, k₂⟩ => ⟨A₁.le_trans h₁ k₁, A₂.le_trans h₂ k₂⟩
  charge := fun c p => (A₁.charge c p.1, A₂.charge c p.2)
  charge_zero := fun p => Prod.ext (A₁.charge_zero p.1) (A₂.charge_zero p.2)
  charge_add := fun c₁ c₂ p => Prod.ext (A₁.charge_add c₁ c₂ p.1) (A₂.charge_add c₁ c₂ p.2)
  charge_mono_cost := fun p h => ⟨A₁.charge_mono_cost p.1 h, A₂.charge_mono_cost p.2 h⟩
  charge_mono_val := fun c {_ _} ⟨h₁, h₂⟩ => ⟨A₁.charge_mono_val c h₁, A₂.charge_mono_val c h₂⟩

/-- Lazy product $A_1 \times A_2$ of phased computation types in $\mathcal{C}$ (§5.1.1). -/
def ProdComp (A₁ A₂ : CompType) : CompType where
  top := ProdCostAlg A₁.top A₂.top
  abs := ProdCostAlg A₁.abs A₂.abs
  alpha := {
    fn := fun p => (A₁.alpha.fn p.1, A₂.alpha.fn p.2)
    map_charge := fun c p => Prod.ext (A₁.alpha.map_charge c p.1) (A₂.alpha.map_charge c p.2)
    mono := fun ⟨h₁, h₂⟩ => ⟨A₁.alpha.mono h₁, A₂.alpha.mono h₂⟩
  }

/-- Coproduct (sum) $A_1 + A_2$ of single-world cost algebras (§2, p. 4 & Figure 2):
carrier is $UA_1 \oplus UA_2$ with cost action acting on the active summand. -/
def SumCostAlg (A₁ A₂ : CostAlg) : CostAlg where
  carrier := Sum A₁.carrier A₂.carrier
  le := fun s₁ s₂ =>
    match s₁, s₂ with
    | .inl a₁, .inl b₁ => A₁.le a₁ b₁
    | .inr a₂, .inr b₂ => A₂.le a₂ b₂
    | _, _ => False
  le_refl := fun s =>
    match s with
    | .inl a₁ => A₁.le_refl a₁
    | .inr a₂ => A₂.le_refl a₂
  le_trans := by
    intro s₁ s₂ s₃ h₁₂ h₂₃
    cases s₁ <;> cases s₂ <;> cases s₃
    · exact A₁.le_trans h₁₂ h₂₃
    · exact False.elim h₂₃
    · exact False.elim h₁₂
    · exact False.elim h₁₂
    · exact False.elim h₁₂
    · exact False.elim h₁₂
    · exact False.elim h₂₃
    · exact A₂.le_trans h₁₂ h₂₃
  charge := fun c s =>
    match s with
    | .inl a₁ => .inl (A₁.charge c a₁)
    | .inr a₂ => .inr (A₂.charge c a₂)
  charge_zero := fun s =>
    match s with
    | .inl a₁ => congrArg Sum.inl (A₁.charge_zero a₁)
    | .inr a₂ => congrArg Sum.inr (A₂.charge_zero a₂)
  charge_add := fun c₁ c₂ s =>
    match s with
    | .inl a₁ => congrArg Sum.inl (A₁.charge_add c₁ c₂ a₁)
    | .inr a₂ => congrArg Sum.inr (A₂.charge_add c₁ c₂ a₂)
  charge_mono_cost := fun s h =>
    match s with
    | .inl a₁ => A₁.charge_mono_cost a₁ h
    | .inr a₂ => A₂.charge_mono_cost a₂ h
  charge_mono_val := by
    intro c s₁ s₂ h
    cases s₁ <;> cases s₂
    · exact A₁.charge_mono_val c h
    · exact False.elim h
    · exact False.elim h
    · exact A₂.charge_mono_val c h

/-- Coproduct (sum) $A_1 + A_2$ of phased computation types in $\mathcal{C}$. -/
def SumComp (A₁ A₂ : CompType) : CompType where
  top := SumCostAlg A₁.top A₂.top
  abs := SumCostAlg A₁.abs A₂.abs
  alpha := {
    fn := fun s =>
      match s with
      | .inl a₁ => .inl (A₁.alpha.fn a₁)
      | .inr a₂ => .inr (A₂.alpha.fn a₂)
    map_charge := fun c s =>
      match s with
      | .inl a₁ => congrArg Sum.inl (A₁.alpha.map_charge c a₁)
      | .inr a₂ => congrArg Sum.inr (A₂.alpha.map_charge c a₂)
    mono := by
      intro s₁ s₂ h
      cases s₁ <;> cases s₂
      · exact A₁.alpha.mono h
      · exact False.elim h
      · exact False.elim h
      · exact A₂.alpha.mono h
  }

/-- **Section 5.2 (Resource-Aware Semantics of Giralf Judgments):**
A Giralf typing judgment $\Delta \vdash^q e : A$ is interpreted in Calf as a lax homomorphism
$\triangleright^q \Delta \mathrel{\multimap\!\!\!\!+} A$. -/
abbrev GiralfSemJudgment (Delta A : CompType) (q : Nat) : Type :=
  LaxCompHom (CreditComp q Delta) A

/-- **Remark 5.5 (Pre-Paying for the Credit Context):**
Given a Giralf program $e : \triangleright^q \Delta \mathrel{\multimap\!\!\!\!+} A$, pre-paying for
the $q$ credits via $e \circ \mathsf{save}\langle q \rangle : \Delta \mathrel{\multimap\!\!\!\!+} A$
reframes the credit context $q$ as the amortized cost publicized in the abstract phase. -/
def prepayGiralf {Delta A : CompType} {q : Nat} (e : GiralfSemJudgment Delta A q) :
    LaxCompHom Delta A :=
  e.comp (saveComp q Delta).toLax

/-- **Example 5.4 (Semantics of `store⟨p⟩(e)` in Giralf):**
Given $q \ge p + q'$ and $e : \triangleright^{q'} \Delta \mathrel{\multimap\!\!\!\!+} A$,
$\mathsf{store}\langle p \rangle(e) : \triangleright^q \Delta \mathrel{\multimap\!\!\!\!+} \triangleright^p A$
weakens $\triangleright^q \Delta$ to $\triangleright^{p + q'} \Delta = \triangleright^p(\triangleright^{q'} \Delta)$
and applies $\triangleright^p e$. -/
def giralfStore {Delta A : CompType} {q p q' : Nat} (h : p + q' ≤ q)
    (e : GiralfSemJudgment Delta A q') :
    GiralfSemJudgment Delta (CreditComp p A) q where
  hom_top := e.hom_top
  hom_abs := e.hom_abs
  lax_comm := fun (d : Delta.top.carrier) => by
    change A.abs.le (A.abs.charge p (A.alpha.fn (e.hom_top.fn d)))
      (e.hom_abs.fn (Delta.abs.charge q (Delta.alpha.fn d)))
    have h₁ : A.abs.le
        (A.abs.charge p (A.alpha.fn (e.hom_top.fn d)))
        (A.abs.charge p (e.hom_abs.fn (Delta.abs.charge q' (Delta.alpha.fn d)))) :=
      A.abs.charge_mono_val p (e.lax_comm d)
    have h₂ : A.abs.charge p (e.hom_abs.fn (Delta.abs.charge q' (Delta.alpha.fn d))) =
        e.hom_abs.fn (Delta.abs.charge (p + q') (Delta.alpha.fn d)) := by
      calc A.abs.charge p (e.hom_abs.fn (Delta.abs.charge q' (Delta.alpha.fn d)))
        _ = e.hom_abs.fn (Delta.abs.charge p (Delta.abs.charge q' (Delta.alpha.fn d))) :=
          (e.hom_abs.map_charge p (Delta.abs.charge q' (Delta.alpha.fn d))).symm
        _ = e.hom_abs.fn (Delta.abs.charge (p + q') (Delta.alpha.fn d)) :=
          congrArg e.hom_abs.fn (Delta.abs.charge_add p q' (Delta.alpha.fn d)).symm
    rw [h₂] at h₁
    exact A.abs.le_trans h₁
      (e.hom_abs.mono (Delta.abs.charge_mono_cost (Delta.alpha.fn d) h))

/-- **Example 5.4 (Semantics of `spend⟨p⟩(e)` in Giralf):**
Post-composes `giralfStore h e` with $\mathsf{spend}\langle p \rangle : \triangleright^p A \multimap A$. -/
def giralfSpend {Delta A : CompType} {q p q' : Nat} (h : p + q' ≤ q)
    (e : GiralfSemJudgment Delta A q') :
    GiralfSemJudgment Delta A q :=
  (spendComp p A).toLax.comp (giralfStore h e)

/-- **Section 5.1.1 (Lazy Product Introduction in Giralf):**
From $\Delta \vdash^q e_1 : A_1$ and $\Delta \vdash^q e_2 : A_2$, derive
$\Delta \vdash^q (e_1, e_2) : A_1 \times A_2$. -/
def giralfPair {Delta A₁ A₂ : CompType} {q : Nat}
    (e₁ : GiralfSemJudgment Delta A₁ q)
    (e₂ : GiralfSemJudgment Delta A₂ q) :
    GiralfSemJudgment Delta (ProdComp A₁ A₂) q where
  hom_top := {
    fn := fun d => (e₁.hom_top.fn d, e₂.hom_top.fn d)
    map_charge := fun c d => Prod.ext (e₁.hom_top.map_charge c d) (e₂.hom_top.map_charge c d)
    mono := fun h => ⟨e₁.hom_top.mono h, e₂.hom_top.mono h⟩
  }
  hom_abs := {
    fn := fun d => (e₁.hom_abs.fn d, e₂.hom_abs.fn d)
    map_charge := fun c d => Prod.ext (e₁.hom_abs.map_charge c d) (e₂.hom_abs.map_charge c d)
    mono := fun h => ⟨e₁.hom_abs.mono h, e₂.hom_abs.mono h⟩
  }
  lax_comm := fun d => ⟨e₁.lax_comm d, e₂.lax_comm d⟩

/-- **Section 5.1.1 (Lazy Product First Projection in Giralf):**
From $\Delta \vdash^q e : A_1 \times A_2$, derive $\Delta \vdash^q \mathsf{proj}_1\,e : A_1$. -/
def giralfProj1 {Delta A₁ A₂ : CompType} {q : Nat}
    (e : GiralfSemJudgment Delta (ProdComp A₁ A₂) q) :
    GiralfSemJudgment Delta A₁ q where
  hom_top := {
    fn := fun d => (e.hom_top.fn d).1
    map_charge := fun c d => congrArg Prod.fst (e.hom_top.map_charge c d)
    mono := fun h => (e.hom_top.mono h).1
  }
  hom_abs := {
    fn := fun d => (e.hom_abs.fn d).1
    map_charge := fun c d => congrArg Prod.fst (e.hom_abs.map_charge c d)
    mono := fun h => (e.hom_abs.mono h).1
  }
  lax_comm := fun d => (e.lax_comm d).1

/-- **Section 5.1.1 (Lazy Product Second Projection in Giralf):**
From $\Delta \vdash^q e : A_1 \times A_2$, derive $\Delta \vdash^q \mathsf{proj}_2\,e : A_2$. -/
def giralfProj2 {Delta A₁ A₂ : CompType} {q : Nat}
    (e : GiralfSemJudgment Delta (ProdComp A₁ A₂) q) :
    GiralfSemJudgment Delta A₂ q where
  hom_top := {
    fn := fun d => (e.hom_top.fn d).2
    map_charge := fun c d => congrArg Prod.snd (e.hom_top.map_charge c d)
    mono := fun h => (e.hom_top.mono h).2
  }
  hom_abs := {
    fn := fun d => (e.hom_abs.fn d).2
    map_charge := fun c d => congrArg Prod.snd (e.hom_abs.map_charge c d)
    mono := fun h => (e.hom_abs.mono h).2
  }
  lax_comm := fun d => (e.lax_comm d).2

/-- **Section 5.1.2 (Debit Introduction `get⟨p⟩(e)` in Giralf):**
From $q' \ge p + q$ and $\Delta \vdash^{q'} e : A$, derive
$\Delta \vdash^q \mathsf{get}\langle p \rangle(e) : \triangleleft^p A$. -/
def giralfGet {Delta A : CompType} {q p q' : Nat} (h : q' ≤ p + q)
    (e : GiralfSemJudgment Delta A q') :
    GiralfSemJudgment Delta (DebitComp p A) q where
  hom_top := {
    fn := fun (d : Delta.top.carrier) =>
      ⟨(e.hom_top.fn d, e.hom_abs.fn (Delta.abs.charge q (Delta.alpha.fn d))), by
        have h₁ : A.abs.le (A.alpha.fn (e.hom_top.fn d))
            (e.hom_abs.fn (Delta.abs.charge q' (Delta.alpha.fn d))) := e.lax_comm d
        have h₂ : A.abs.le
            (e.hom_abs.fn (Delta.abs.charge q' (Delta.alpha.fn d)))
            (e.hom_abs.fn (Delta.abs.charge (p + q) (Delta.alpha.fn d))) :=
          e.hom_abs.mono (Delta.abs.charge_mono_cost (Delta.alpha.fn d) h)
        have h₃ : e.hom_abs.fn (Delta.abs.charge (p + q) (Delta.alpha.fn d)) =
            A.abs.charge p (e.hom_abs.fn (Delta.abs.charge q (Delta.alpha.fn d))) := by
          calc e.hom_abs.fn (Delta.abs.charge (p + q) (Delta.alpha.fn d))
            _ = e.hom_abs.fn (Delta.abs.charge p (Delta.abs.charge q (Delta.alpha.fn d))) :=
              congrArg e.hom_abs.fn (Delta.abs.charge_add p q (Delta.alpha.fn d))
            _ = A.abs.charge p (e.hom_abs.fn (Delta.abs.charge q (Delta.alpha.fn d))) :=
              e.hom_abs.map_charge p (Delta.abs.charge q (Delta.alpha.fn d))
        rw [h₃] at h₂
        exact A.abs.le_trans h₁ h₂⟩
    map_charge := fun c (d : Delta.top.carrier) => by
      apply Subtype.ext
      apply Prod.ext
      · exact e.hom_top.map_charge c d
      · calc e.hom_abs.fn (Delta.abs.charge q (Delta.alpha.fn (Delta.top.charge c d)))
          _ = e.hom_abs.fn (Delta.abs.charge q (Delta.abs.charge c (Delta.alpha.fn d))) :=
            congrArg (fun x => e.hom_abs.fn (Delta.abs.charge q x)) (Delta.alpha.map_charge c d)
          _ = e.hom_abs.fn (Delta.abs.charge (q + c) (Delta.alpha.fn d)) :=
            congrArg e.hom_abs.fn (Delta.abs.charge_add q c (Delta.alpha.fn d)).symm
          _ = e.hom_abs.fn (Delta.abs.charge (c + q) (Delta.alpha.fn d)) := by
            rw [Nat.add_comm q c]
          _ = e.hom_abs.fn (Delta.abs.charge c (Delta.abs.charge q (Delta.alpha.fn d))) :=
            congrArg e.hom_abs.fn (Delta.abs.charge_add c q (Delta.alpha.fn d))
          _ = A.abs.charge c (e.hom_abs.fn (Delta.abs.charge q (Delta.alpha.fn d))) :=
            e.hom_abs.map_charge c (Delta.abs.charge q (Delta.alpha.fn d))
    mono := fun h_le =>
      ⟨e.hom_top.mono h_le,
       e.hom_abs.mono (Delta.abs.charge_mono_val q (Delta.alpha.mono h_le))⟩
  }
  hom_abs := e.hom_abs
  lax_comm := fun _ => A.abs.le_refl _

/-- **Section 5.1.2 (Debit Elimination `pay(e)` in Giralf):**
From $q \ge p + q'$ and $\Delta \vdash^{q'} e : \triangleleft^p A$, derive
$\Delta \vdash^q \mathsf{pay}(e) : A$ using the counit of $\triangleright^p \dashv \triangleleft^p$. -/
def giralfPay {Delta A : CompType} {q p q' : Nat} (h : p + q' ≤ q)
    (e : GiralfSemJudgment Delta (DebitComp p A) q') :
    GiralfSemJudgment Delta A q :=
  (creditDebitCounit p A).comp (giralfStore h e)

/-! ## §5.2 Positive AARA Type Grammar and Potential Function Semantics (Figure 2) -/

/-- **Section 5.2 & Figure 2 (Positive AARA Type Grammar):**
Inductively-defined subset of computation types in Calf and Giralf:
$$A, B ::= FX \mid \top \mid A \otimes B \mid A + B \mid \triangleright^p A \mid \mathsf{CList}^p_1 A \mid \mathsf{CList}^{(p_1, p_2)}_2 A.$$ -/
inductive AARAType : Type 1 where
  | free (X : Type) : AARAType
  | top : AARAType
  | tensor (A B : AARAType) : AARAType
  | sum (A B : AARAType) : AARAType
  | credit (p : Nat) (A : AARAType) : AARAType
  | clist1 (p : Nat) (A : AARAType) : AARAType
  | clist2 (p₁ p₂ : Nat) (A : AARAType) : AARAType

/-- **Figure 2 (Underlying Value Set $\llbracket A \rrbracket : \mathcal{V}$ of an AARA Type):**
Assigns to each positive AARA type $A$ its underlying type of values $\llbracket A \rrbracket$. -/
def aaraValues : AARAType → Type
  | .free X => X
  | .top => PUnit
  | .tensor A B => aaraValues A × aaraValues B
  | .sum A B => Sum (aaraValues A) (aaraValues B)
  | .credit _ A => aaraValues A
  | .clist1 _ A => List (aaraValues A)
  | .clist2 _ _ A => List (aaraValues A)

/-- Sum of element potentials $\sum_{a \in l} \Phi(a)$ over a list $l$. -/
def listPotentialSum {α : Type} (phi : α → Nat) : List α → Nat
  | [] => 0
  | x :: xs => phi x + listPotentialSum phi xs

/-- **Figure 2 (Potential Function $\Phi_A : \llbracket A \rrbracket \to \mathbb{N}$ of an AARA Type):**
Computes the potential/credits contained within a value of type $A$. -/
def aaraPotential : (A : AARAType) → aaraValues A → Nat
  | .free _, _ => 0
  | .top, _ => 0
  | .tensor A B, (a, b) => aaraPotential A a + aaraPotential B b
  | .sum A B, s =>
      match s with
      | .inl a => aaraPotential A a
      | .inr b => aaraPotential B b
  | .credit p A, a => p + aaraPotential A a
  | .clist1 p A, l => l.length * p + listPotentialSum (aaraPotential A) l
  | .clist2 p₁ p₂ A, l =>
      l.length * p₁ + binom2 l.length * p₂ + listPotentialSum (aaraPotential A) l

/-- Sum of element potentials along an inductive quadratic-credit list `CList2Ind`. -/
def clist2ElemPotentialSum {α : Type} (phi : α → Nat) :
    {c₁ c₂ : Nat} → CList2Ind α c₁ c₂ → Nat
  | _, _, .nil => 0
  | _, _, .cons x tail => phi x + clist2ElemPotentialSum phi tail

/-- Inductive potential of a `CList2Ind α p₁ p₂` structure: the sum of its inductively-shifted
structural credits (`CList2Ind.credits`) and the potentials of its elements. -/
def clist2IndTotalPotential {α : Type} (phi : α → Nat) {p₁ p₂ : Nat}
    (t : CList2Ind α p₁ p₂) : Nat :=
  t.credits + clist2ElemPotentialSum phi t

/-- Compositional Calf computation type interpretation of a positive AARA type (Theorem 5.7):
interprets `.free X` as `FreeComp X`, `.top` as `TopComp`, `.credit p A` via the banker's credit
operator `CreditComp p`, `.tensor` and `.sum` via product/coproduct of potential-carrying types,
`.clist1` via linear-credit lists, and `.clist2 p₁ p₂ A` via the inductive family
`CList2Ind (aaraValues A) p₁ p₂` equipped with its inductive credit accumulator
`clist2IndTotalPotential`. -/
def interpAARAType : AARAType → CompType
  | .free X => FreeComp X
  | .top => TopComp
  | .tensor A B =>
      PotentialType (aaraValues A × aaraValues B)
        (fun p => aaraPotential A p.1 + aaraPotential B p.2)
  | .sum A B =>
      SumComp (interpAARAType A) (interpAARAType B)
  | .credit p A =>
      CreditComp p (interpAARAType A)
  | .clist1 p A =>
      PotentialType (List (aaraValues A))
        (fun l => l.length * p + listPotentialSum (aaraPotential A) l)
  | .clist2 p₁ p₂ A =>
      PotentialType (CList2Ind (aaraValues A) p₁ p₂)
        (clist2IndTotalPotential (aaraPotential A))

/-! ## §5.1 & §5.3 Insertion Sort (`insert` and `isort`, Examples 5.2, 5.3, and 5.18) -/

/-- **Example 5.2 (Concrete Cost-Instrumented `insert`):**
Inserts `x : Nat` into a list `l : List Nat`, incurring $1$ unit of cost for each comparison
`x ≤ y`. If `x ≤ y`, it stops immediately without recursing on the tail (as captured by the
credit-aware paramorphism of Lemma 5.1). -/
def insertCostTop (x : Nat) : List Nat → (FreeCostAlg (List Nat)).carrier
  | [] => (0, [x])
  | y :: ys =>
      if x ≤ y then
        (1, x :: y :: ys)
      else
        let recRes := insertCostTop x ys
        (1 + recRes.1, y :: recRes.2)

/-- Pure mathematical specification of `insert` (zero abstract cost). -/
def insertPure (x : Nat) : List Nat → List Nat
  | [] => [x]
  | y :: ys =>
      if x ≤ y then
        x :: y :: ys
      else
        y :: insertPure x ys

/-- **Example 5.3 & Example 5.18 (Concrete Cost-Instrumented `isort`):**
Insertion sort iterating `insertCostTop` over the input list, accumulating total comparison cost. -/
def isortCostTop : List Nat → (FreeCostAlg (List Nat)).carrier
  | [] => (0, [])
  | x :: xs =>
      let tailRes := isortCostTop xs
      let insRes := insertCostTop x tailRes.2
      (tailRes.1 + insRes.1, insRes.2)

/-- Pure mathematical specification of `isort` (zero abstract cost). -/
def isortPure : List Nat → List Nat
  | [] => []
  | x :: xs => insertPure x (isortPure xs)

/-- Concrete cost-algebra homomorphism for `insert x` on $F(\mathsf{List}\,\mathbb{N})$. -/
def insertTopHom (x : Nat) : CostHom (FreeCostAlg (List Nat)) (FreeCostAlg (List Nat)) where
  fn := fun p =>
    let res := insertCostTop x p.2
    (p.1 + res.1, res.2)
  map_charge := fun c ⟨c₀, l⟩ =>
    Prod.ext (Nat.add_assoc c c₀ (insertCostTop x l).1) rfl
  mono := fun ⟨h, e⟩ => ⟨by rw [e]; exact Nat.add_le_add_right h _, by rw [e]⟩

/-- Abstract zero-cost homomorphism for `insert x` on $F(\mathsf{List}\,\mathbb{N})$. -/
def insertAbsHom (x : Nat) : CostHom (FreeCostAlg (List Nat)) (FreeCostAlg (List Nat)) where
  fn := fun p => (p.1, insertPure x p.2)
  map_charge := fun _ _ => rfl
  mono := fun ⟨h, e⟩ => ⟨h, congrArg (insertPure x) e⟩

/-- Concrete cost-algebra homomorphism for `isort` on $F(\mathsf{List}\,\mathbb{N})$. -/
def isortTopHom : CostHom (FreeCostAlg (List Nat)) (FreeCostAlg (List Nat)) where
  fn := fun p =>
    let res := isortCostTop p.2
    (p.1 + res.1, res.2)
  map_charge := fun c ⟨c₀, l⟩ =>
    Prod.ext (Nat.add_assoc c c₀ (isortCostTop l).1) rfl
  mono := fun ⟨h, e⟩ => ⟨by rw [e]; exact Nat.add_le_add_right h _, by rw [e]⟩

/-- Abstract zero-cost homomorphism for `isort` on $F(\mathsf{List}\,\mathbb{N})$. -/
def isortAbsHom : CostHom (FreeCostAlg (List Nat)) (FreeCostAlg (List Nat)) where
  fn := fun p => (p.1, isortPure p.2)
  map_charge := fun _ _ => rfl
  mono := fun ⟨h, e⟩ => ⟨h, congrArg isortPure e⟩

/-! ## §5.3 Cost Inference and Skeletal Credit Annotations (Definition 5.9, Lemmas 5.15–5.16) -/

/-- A single-world cost algebra homomorphism $f : FX \multimap FY$ is *cost-erased* (incurs zero
abstract cost) if it preserves the cost counter unchanged on every input $(c, x)$. -/
def IsZeroCostFreeHom {X Y : Type} (f : CostHom (FreeCostAlg X) (FreeCostAlg Y)) : Prop :=
  ∀ (c : Nat) (x : X), (f.fn (c, x)).1 = c

/-- **Definition 5.9 (Cost Inference for a Calf Program):**
Given concrete and abstract value types $X, Y$, a concrete cost-instrumented homomorphism
$e_\top : FX \multimap FY$, and its cost erasure $e_{\mathsf{abs}} : FX \multimap FY$, a Giralf
judgment $a : \underline{A} \vdash^q \underline{e} : \underline{B}$ is a valid *cost inference* if:
1. ($\neg\mathsf{abs}$ concrete fidelity): $\underline{A}_\top = FX$, $\underline{B}_\top = FY$, and
   $\underline{e}_\top = e_\top$;
2. ($\mathsf{abs}$ cost erasure): $\underline{A}_{\mathsf{abs}} = FX$,
   $\underline{B}_{\mathsf{abs}} = FY$, and $\underline{e}_{\mathsf{abs}} = e_{\mathsf{abs}}$ is
   the zero-cost erasure of $e_\top$. -/
structure CostInferenceCertificate {X Y : Type}
    (e_top e_abs : CostHom (FreeCostAlg X) (FreeCostAlg Y)) where
  A_inf : CompType
  B_inf : CompType
  q : Nat
  e_inf : GiralfSemJudgment A_inf B_inf q
  hA_top : A_inf.top = FreeCostAlg X
  hB_top : B_inf.top = FreeCostAlg Y
  hA_abs : A_inf.abs = FreeCostAlg X
  hB_abs : B_inf.abs = FreeCostAlg Y
  he_top : HEq e_inf.hom_top e_top
  he_abs : HEq e_inf.hom_abs e_abs
  h_erased : IsZeroCostFreeHom e_abs

/-- **Section 5.3.1 (Grammar of Skeletal Types for Cost Inference):**
$$S ::= \top \mid S_1 + S_2 \mid S_1 \times S_2 \mid \mathsf{List}(FX).$$ -/
inductive SkelType : Type 1 where
  | top : SkelType
  | sum (S₁ S₂ : SkelType) : SkelType
  | prod (S₁ S₂ : SkelType) : SkelType
  | list (X : Type) : SkelType

/-- Numeric assignment of credit and debit annotations to a skeletal type $S$ (§5.3.1):
- $\underline{\top} = \top$ (no numeric unknowns),
- $\underline{A_1 + A_2} = \triangleright^{q_1} \underline{A_1} + \triangleright^{q_2} \underline{A_2}$,
- $\underline{A_1 \times A_2} = \triangleleft^{q_1} \underline{A_1} \times \triangleleft^{q_2} \underline{A_2}$,
- $\underline{\mathsf{List}(FX)} = \mathsf{CList}^{(p_1, p_2)}_2(FX)$. -/
def SkelAnnotation : SkelType → Type
  | .top => PUnit
  | .sum S₁ S₂ => (Nat × Nat) × SkelAnnotation S₁ × SkelAnnotation S₂
  | .prod S₁ S₂ => (Nat × Nat) × SkelAnnotation S₁ × SkelAnnotation S₂
  | .list _ => Nat × Nat

/-- Pointwise addition $A_1 \oplus A_2$ of numeric credit assignments on the same skeletal type $S$
(Lemmas 5.15 & 5.16). -/
def SkelAnnotation.add : {S : SkelType} → SkelAnnotation S → SkelAnnotation S → SkelAnnotation S
  | .top, .unit, .unit => .unit
  | .sum _ _, ((q₁, q₂), a₁, a₂), ((r₁, r₂), b₁, b₂) =>
      ((q₁ + r₁, q₂ + r₂), SkelAnnotation.add a₁ b₁, SkelAnnotation.add a₂ b₂)
  | .prod _ _, ((q₁, q₂), a₁, a₂), ((r₁, r₂), b₁, b₂) =>
      ((q₁ + r₁, q₂ + r₂), SkelAnnotation.add a₁ b₁, SkelAnnotation.add a₂ b₂)
  | .list _, (p₁, p₂), (q₁, q₂) => (p₁ + q₁, p₂ + q₂)

/-- Pointwise scalar multiplication $k \odot A$ of numeric credit assignments on a skeletal type $S$
(Lemma 5.15). -/
def SkelAnnotation.smul (k : Nat) : {S : SkelType} → SkelAnnotation S → SkelAnnotation S
  | .top, .unit => .unit
  | .sum _ _, ((q₁, q₂), a₁, a₂) =>
      ((k * q₁, k * q₂), SkelAnnotation.smul k a₁, SkelAnnotation.smul k a₂)
  | .prod _ _, ((q₁, q₂), a₁, a₂) =>
      ((k * q₁, k * q₂), SkelAnnotation.smul k a₁, SkelAnnotation.smul k a₂)
  | .list _, (p₁, p₂) => (k * p₁, k * p₂)

/-- **Examples 5.13, 5.14, and 5.17 & Lemmas 5.15–5.16 (List Recursor Typing Annotation):**
Represents a credit assignment
$l : \mathsf{CList}^{(in_1, in_2)}_2(F\mathbb{N}) \vdash^q e : \P \rightharpoonup \triangleleft^d \mathsf{CList}^{(out_1, out_2)}_2(F\mathbb{N})$
on a skeletal list recursor such as $\underline{\mathsf{snoc'}}$. -/
structure ListRecAnnotation where
  in_lin : Nat
  in_quad : Nat
  ctx_q : Nat
  debit : Nat
  out_lin : Nat
  out_quad : Nat
  deriving DecidableEq, Repr

/-- Pointwise addition $A_1 \oplus A_2$ of `ListRecAnnotation`s (Lemmas 5.15 & 5.16). -/
def ListRecAnnotation.add (A₁ A₂ : ListRecAnnotation) : ListRecAnnotation where
  in_lin := A₁.in_lin + A₂.in_lin
  in_quad := A₁.in_quad + A₂.in_quad
  ctx_q := A₁.ctx_q + A₂.ctx_q
  debit := A₁.debit + A₂.debit
  out_lin := A₁.out_lin + A₂.out_lin
  out_quad := A₁.out_quad + A₂.out_quad

/-- Pointwise scalar multiplication $k \odot A$ of a `ListRecAnnotation` (Lemma 5.15). -/
def ListRecAnnotation.smul (k : Nat) (A : ListRecAnnotation) : ListRecAnnotation where
  in_lin := k * A.in_lin
  in_quad := k * A.in_quad
  ctx_q := k * A.ctx_q
  debit := k * A.debit
  out_lin := k * A.out_lin
  out_quad := k * A.out_quad

/-- Semantic validity predicate for a `ListRecAnnotation` on a list transformation that increases
list length by $1$ (`|l'| = |l| + 1`, as in `snoc'`) and incurs `stepCost` units of cost per input
element (`stepCost = 1` for cost-aware $\underline{\mathsf{snoc'}}$ and `stepCost = 0` for cost-free
$\underline{\widehat{\mathsf{snoc'}}}$):
for every input list length $n = |l|$, the total credits spent ($n \cdot \mathsf{stepCost}$) plus
the output list's potential ($(n + 1) \cdot out_1 + \binom{n + 1}{2} \cdot out_2$) is bounded by
the input list's potential ($n \cdot in_1 + \binom{n}{2} \cdot in_2$) plus the top-level credit
context $q$ and the assumed debit $d$. -/
def ValidSnocRecAnnotation (stepCost : Nat) (A : ListRecAnnotation) : Prop :=
  ∀ n : Nat,
    n * stepCost + ((n + 1) * A.out_lin + binom2 (n + 1) * A.out_quad) ≤
      (n * A.in_lin + binom2 n * A.in_quad) + A.ctx_q + A.debit

/-- **Example 5.14(1):** Cost-aware annotation $A_{\mathrm{ca}}$ for $\underline{\mathsf{snoc'}}$:
$l : \mathsf{CList}^{(1, 0)}_2(F\mathbb{N}) \vdash^0 \underline{\mathsf{snoc'}} :
\P \rightharpoonup \triangleleft^0 \mathsf{CList}^{(0, 0)}_2(F\mathbb{N})$. -/
def snocAnnCA : ListRecAnnotation :=
  ⟨1, 0, 0, 0, 0, 0⟩

/-- **Example 5.14(2):** First cost-free annotation $A_{\mathrm{cf1}}$ for $\underline{\widehat{\mathsf{snoc'}}}$
(movement of linear credits):
$l : \mathsf{CList}^{(1, 0)}_2(F\mathbb{N}) \vdash^0 \underline{\widehat{\mathsf{snoc'}}} :
\P \rightharpoonup \triangleleft^1 \mathsf{CList}^{(1, 0)}_2(F\mathbb{N})$. -/
def snocAnnCF1 : ListRecAnnotation :=
  ⟨1, 0, 0, 1, 1, 0⟩

/-- **Example 5.14(3):** Second cost-free annotation $A_{\mathrm{cf2}}$ for $\underline{\widehat{\mathsf{snoc'}}}$
(movement of quadratic credits):
$l : \mathsf{CList}^{(1, 1)}_2(F\mathbb{N}) \vdash^0 \underline{\widehat{\mathsf{snoc'}}} :
\P \rightharpoonup \triangleleft^0 \mathsf{CList}^{(0, 1)}_2(F\mathbb{N})$. -/
def snocAnnCF2 : ListRecAnnotation :=
  ⟨1, 1, 0, 0, 0, 1⟩

/-- **Example 5.17:** Symbolic linear combination
$A_{\mathrm{ca}} \oplus (p_1 \odot A_{\mathrm{cf1}}) \oplus (p_2 \odot A_{\mathrm{cf2}})$ yielding
the resource-polymorphic typing for $\underline{\mathsf{snoc'}}$. -/
def snocSymbolicAnnotation (p₁ p₂ : Nat) : ListRecAnnotation :=
  ListRecAnnotation.add
    (ListRecAnnotation.add snocAnnCA (ListRecAnnotation.smul p₁ snocAnnCF1))
    (ListRecAnnotation.smul p₂ snocAnnCF2)

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. Section 5, pp. 17-24.
Read-back

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

Read-back: Importing Def_Grodin2026_Banker, this module defines the following types, operations, and predicates in the namespace Grodin2026:

  1. Cartesian Products, Coproducts, and Graded Giralf Semantic Judgments:

    • For cost algebras A1,A2A_1, A_2A1​,A2​, the product A1×A2A_1 \times A_2A1​×A2​ (ProdCostAlg A₁ A₂) has carrier UA1×UA2UA_1 \times UA_2UA1​×UA2​, componentwise preorder, and diagonal cost action charge(c,(a1,a2)):=(chargeA1(c,a1),chargeA2(c,a2))\mathsf{charge}(c, (a_1, a_2)) := (\mathsf{charge}_{A_1}(c, a_1), \mathsf{charge}_{A_2}(c, a_2))charge(c,(a1​,a2​)):=(chargeA1​​(c,a1​),chargeA2​​(c,a2​)). The coproduct A1+A2A_1 + A_2A1​+A2​ (SumCostAlg A₁ A₂) has carrier UA1⊕UA2UA_1 \oplus UA_2UA1​⊕UA2​, summand-local preorder (inl(a1)≤inl(b1)  ⟺  a1≤A1b1\mathsf{inl}(a_1) \le \mathsf{inl}(b_1) \iff a_1 \le_{A_1} b_1inl(a1​)≤inl(b1​)⟺a1​≤A1​​b1​, inr(a2)≤inr(b2)  ⟺  a2≤A2b2\mathsf{inr}(a_2) \le \mathsf{inr}(b_2) \iff a_2 \le_{A_2} b_2inr(a2​)≤inr(b2​)⟺a2​≤A2​​b2​, and False\mathrm{False}False across distinct summands), and summand-wise cost action. These lift componentwise to phased computation types as ProdComp and SumComp.
    • For Δ,A∈C\Delta, A \in \mathcal{C}Δ,A∈C and q∈Nq \in \mathbb{N}q∈N, a semantic Giralf judgment (GiralfSemJudgment Delta A q) is a lax computation homomorphism ▹qΔ⊸ ⁣ ⁣ ⁣ ⁣+A\triangleright^q \Delta \mathrel{\multimap\!\!\!\!+} A▹qΔ⊸+A.
    • prepayGiralf e composes e:▹qΔ⊸ ⁣ ⁣ ⁣ ⁣+Ae : \triangleright^q \Delta \mathrel{\multimap\!\!\!\!+} Ae:▹qΔ⊸+A with (save⟨q⟩Δ).toLax:Δ⊸ ⁣ ⁣ ⁣ ⁣+▹qΔ(\mathsf{save}\langle q \rangle_\Delta).\mathsf{toLax} : \Delta \mathrel{\multimap\!\!\!\!+} \triangleright^q \Delta(save⟨q⟩Δ​).toLax:Δ⊸+▹qΔ.
    • Given p+q′≤qp + q' \le qp+q′≤q and e:▹q′Δ⊸ ⁣ ⁣ ⁣ ⁣+Ae : \triangleright^{q'} \Delta \mathrel{\multimap\!\!\!\!+} Ae:▹q′Δ⊸+A, giralfStore h e : GiralfSemJudgment Delta (CreditComp p A) q has the same underlying maps (e⊤,eabs)(e_\top, e_{\mathsf{abs}})(e⊤​,eabs​), and giralfSpend h e : GiralfSemJudgment Delta A q post-composes giralfStore h e with (spend⟨p⟩A).toLax(\mathsf{spend}\langle p \rangle_A).\mathsf{toLax}(spend⟨p⟩A​).toLax.
    • giralfPair, giralfProj1, and giralfProj2 provide pairing and projections for ProdComp A₁ A₂ at grade qqq.
    • Given q′≤p+qq' \le p + qq′≤p+q and e:▹q′Δ⊸ ⁣ ⁣ ⁣ ⁣+Ae : \triangleright^{q'} \Delta \mathrel{\multimap\!\!\!\!+} Ae:▹q′Δ⊸+A, giralfGet h e : GiralfSemJudgment Delta (DebitComp p A) q has concrete map d↦(e⊤(d),eabs(chargeΔabs(q,αΔ(d))))d \mapsto (e_\top(d), e_{\mathsf{abs}}(\mathsf{charge}_{\Delta_{\mathsf{abs}}}(q, \alpha_\Delta(d))))d↦(e⊤​(d),eabs​(chargeΔabs​​(q,αΔ​(d)))) and abstract map eabse_{\mathsf{abs}}eabs​. Given p+q′≤qp + q' \le qp+q′≤q and e:▹q′Δ⊸ ⁣ ⁣ ⁣ ⁣+◃pAe : \triangleright^{q'} \Delta \mathrel{\multimap\!\!\!\!+} \triangleleft^p Ae:▹q′Δ⊸+◃pA, giralfPay h e : GiralfSemJudgment Delta A q post-composes giralfStore h e with the counit εp:▹p◃pA⊸ ⁣ ⁣ ⁣ ⁣+A\varepsilon_p : \triangleright^p \triangleleft^p A \mathrel{\multimap\!\!\!\!+} Aεp​:▹p◃pA⊸+A.
  2. Positive AARA Type Grammar and Potential Semantics:

    • AARAType : Type 1 is the inductive grammar generated by free (X : Type), top, tensor A B, sum A B, credit (p : ℕ) A, clist1 (p : ℕ) A, and clist2 (p₁ p₂ : ℕ) A.
    • aaraValues : AARAType → Type assigns the underlying value type ⟦A⟧\llbracket A \rrbracket[[A]]: ⟦free X⟧=X\llbracket \mathsf{free}\,X \rrbracket = X[[freeX]]=X, ⟦top⟧=1\llbracket \mathsf{top} \rrbracket = \mathbf{1}[[top]]=1, ⟦A⊗B⟧=⟦A⟧×⟦B⟧\llbracket A \otimes B \rrbracket = \llbracket A \rrbracket \times \llbracket B \rrbracket[[A⊗B]]=[[A]]×[[B]], ⟦A+B⟧=⟦A⟧⊕⟦B⟧\llbracket A + B \rrbracket = \llbracket A \rrbracket \oplus \llbracket B \rrbracket[[A+B]]=[[A]]⊕[[B]], ⟦▹pA⟧=⟦A⟧\llbracket \triangleright^p A \rrbracket = \llbracket A \rrbracket[[▹pA]]=[[A]], and ⟦clist1 p A⟧=⟦clist2 p1 p2 A⟧=List ⟦A⟧\llbracket \mathsf{clist1}\,p\,A \rrbracket = \llbracket \mathsf{clist2}\,p_1\,p_2\,A \rrbracket = \mathsf{List}\,\llbracket A \rrbracket[[clist1pA]]=[[clist2p1​p2​A]]=List[[A]].
    • Using listPotentialSum (∑x∈lϕ(x)\sum_{x \in l} \phi(x)∑x∈l​ϕ(x)), aaraPotential : (A : AARAType) → aaraValues A → ℕ assigns the potential ΦA:⟦A⟧→N\Phi_A : \llbracket A \rrbracket \to \mathbb{N}ΦA​:[[A]]→N: 000 on free and top, ΦA(a)+ΦB(b)\Phi_A(a) + \Phi_B(b)ΦA​(a)+ΦB​(b) on tensor, summand-wise on sum, p+ΦA(a)p + \Phi_A(a)p+ΦA​(a) on credit p A, ∣l∣⋅p+∑x∈lΦA(x)|l| \cdot p + \sum_{x \in l} \Phi_A(x)∣l∣⋅p+∑x∈l​ΦA​(x) on clist1 p A, and ∣l∣⋅p1+binom2(∣l∣)⋅p2+∑x∈lΦA(x)|l| \cdot p_1 + \mathsf{binom2}(|l|) \cdot p_2 + \sum_{x \in l} \Phi_A(x)∣l∣⋅p1​+binom2(∣l∣)⋅p2​+∑x∈l​ΦA​(x) on clist2 p₁ p₂ A.
    • interpAARAType : AARAType → CompType interprets free X as FreeComp X, top as TopComp, tensor A B as PotentialType(⟦A⟧×⟦B⟧,(a,b)↦ΦA(a)+ΦB(b))\mathsf{PotentialType}(\llbracket A \rrbracket \times \llbracket B \rrbracket, (a, b) \mapsto \Phi_A(a) + \Phi_B(b))PotentialType([[A]]×[[B]],(a,b)↦ΦA​(a)+ΦB​(b)), sum A B as SumComp(interpAARAType A,interpAARAType B)\mathsf{SumComp}(\mathsf{interpAARAType}\,A, \mathsf{interpAARAType}\,B)SumComp(interpAARATypeA,interpAARATypeB), credit p A as ▹p(interpAARAType A)\triangleright^p(\mathsf{interpAARAType}\,A)▹p(interpAARATypeA), clist1 p A as PotentialType(List ⟦A⟧,Φclist1 p A)\mathsf{PotentialType}(\mathsf{List}\,\llbracket A \rrbracket, \Phi_{\mathsf{clist1}\,p\,A})PotentialType(List[[A]],Φclist1pA​), and clist2 p₁ p₂ A as PotentialType(CList2Ind ⟦A⟧ p1 p2,t↦t.credits+clist2ElemPotentialSum ΦA t)\mathsf{PotentialType}(\mathsf{CList2Ind}\,\llbracket A \rrbracket\,p_1\,p_2, t \mapsto t.\mathsf{credits} + \mathsf{clist2ElemPotentialSum}\,\Phi_A\,t)PotentialType(CList2Ind[[A]]p1​p2​,t↦t.credits+clist2ElemPotentialSumΦA​t).
  3. Cost-Instrumented Insertion Sort and Cost Inference Annotations:

    • For x∈Nx \in \mathbb{N}x∈N and l:List Nl : \mathsf{List}\,\mathbb{N}l:ListN, insertCostTop x l : ℕ × List ℕ returns (0,[x])(0, [x])(0,[x]) on [][][], and on y::ysy :: \mathit{ys}y::ys returns (1,x::y::ys)(1, x :: y :: \mathit{ys})(1,x::y::ys) if x≤yx \le yx≤y and (1+r1,y::r2)(1 + r_1, y :: r_2)(1+r1​,y::r2​) where (r1,r2)=insertCostTop(x,ys)(r_1, r_2) = \mathsf{insertCostTop}(x, \mathit{ys})(r1​,r2​)=insertCostTop(x,ys) if x>yx > yx>y. insertPure x l performs the same insertion returning only the list. isortCostTop : List ℕ → ℕ × List ℕ maps []↦(0,[])[] \mapsto (0, [])[]↦(0,[]) and x::xs↦(t1+i1,i2)x :: \mathit{xs} \mapsto (t_1 + i_1, i_2)x::xs↦(t1​+i1​,i2​) where (t1,t2)=isortCostTop(xs)(t_1, t_2) = \mathsf{isortCostTop}(\mathit{xs})(t1​,t2​)=isortCostTop(xs) and (i1,i2)=insertCostTop(x,t2)(i_1, i_2) = \mathsf{insertCostTop}(x, t_2)(i1​,i2​)=insertCostTop(x,t2​), while isortPure iterates insertPure. These lift to free cost algebra homomorphisms insertTopHom x, insertAbsHom x, isortTopHom, and isortAbsHom on F(List N)F(\mathsf{List}\,\mathbb{N})F(ListN).
    • For f:FX⊸FYf : FX \multimap FYf:FX⊸FY, IsZeroCostFreeHom f : Prop asserts ∀c∈N,x∈X,  (f(c,x))1=c\forall c \in \mathbb{N}, x \in X,\; (f(c, x))_1 = c∀c∈N,x∈X,(f(c,x))1​=c.
    • Given e⊤,eabs:FX⊸FYe_\top, e_{\mathsf{abs}} : FX \multimap FYe⊤​,eabs​:FX⊸FY, CostInferenceCertificate e_top e_abs packages computation types Ainf,Binf∈CA_{\mathrm{inf}}, B_{\mathrm{inf}} \in \mathcal{C}Ainf​,Binf​∈C, a grade q∈Nq \in \mathbb{N}q∈N, and a judgment einf:▹qAinf⊸ ⁣ ⁣ ⁣ ⁣+Binfe_{\mathrm{inf}} : \triangleright^q A_{\mathrm{inf}} \mathrel{\multimap\!\!\!\!+} B_{\mathrm{inf}}einf​:▹qAinf​⊸+Binf​ such that (Ainf)⊤=(Ainf)abs=FX(A_{\mathrm{inf}})_\top = (A_{\mathrm{inf}})_{\mathsf{abs}} = FX(Ainf​)⊤​=(Ainf​)abs​=FX, (Binf)⊤=(Binf)abs=FY(B_{\mathrm{inf}})_\top = (B_{\mathrm{inf}})_{\mathsf{abs}} = FY(Binf​)⊤​=(Binf​)abs​=FY, (einf)⊤≃HEqe⊤(e_{\mathrm{inf}})_\top \simeq_{\mathsf{HEq}} e_\top(einf​)⊤​≃HEq​e⊤​, (einf)abs≃HEqeabs(e_{\mathrm{inf}})_{\mathsf{abs}} \simeq_{\mathsf{HEq}} e_{\mathsf{abs}}(einf​)abs​≃HEq​eabs​, and IsZeroCostFreeHom(eabs)\mathsf{IsZeroCostFreeHom}(e_{\mathsf{abs}})IsZeroCostFreeHom(eabs​).
    • SkelType (top, sum, prod, list) and SkelAnnotation represent skeletal types and their numeric credit/debit assignments equipped with componentwise addition SkelAnnotation.add and scalar multiplication SkelAnnotation.smul.
    • ListRecAnnotation is a 6-tuple of natural numbers (in_lin,in_quad,ctx_q,debit,out_lin,out_quad)(\mathit{in\_lin}, \mathit{in\_quad}, \mathit{ctx\_q}, \mathit{debit}, \mathit{out\_lin}, \mathit{out\_quad})(in_lin,in_quad,ctx_q,debit,out_lin,out_quad) with componentwise addition ListRecAnnotation.add (⊕\oplus⊕) and scalar multiplication ListRecAnnotation.smul (k⊙−k \odot -k⊙−). For stepCost∈N\mathit{stepCost} \in \mathbb{N}stepCost∈N and A:ListRecAnnotationA : \mathsf{ListRecAnnotation}A:ListRecAnnotation, ValidSnocRecAnnotation stepCost A is the proposition:
∀n∈N,n⋅stepCost+((n+1)⋅A.out_lin+binom2(n+1)⋅A.out_quad)≤(n⋅A.in_lin+binom2(n)⋅A.in_quad)+A.ctx_q+A.debit.\forall n \in \mathbb{N},\quad n \cdot \mathit{stepCost} + \bigl((n + 1) \cdot A.\mathit{out\_lin} + \mathsf{binom2}(n + 1) \cdot A.\mathit{out\_quad}\bigr) \le \bigl(n \cdot A.\mathit{in\_lin} + \mathsf{binom2}(n) \cdot A.\mathit{in\_quad}\bigr) + A.\mathit{ctx\_q} + A.\mathit{debit}.∀n∈N,n⋅stepCost+((n+1)⋅A.out_lin+binom2(n+1)⋅A.out_quad)≤(n⋅A.in_lin+binom2(n)⋅A.in_quad)+A.ctx_q+A.debit.
  • The basis annotations are snocAnnCA:=(1,0,0,0,0,0)\mathsf{snocAnnCA} := (1, 0, 0, 0, 0, 0)snocAnnCA:=(1,0,0,0,0,0), snocAnnCF1:=(1,0,0,1,1,0)\mathsf{snocAnnCF1} := (1, 0, 0, 1, 1, 0)snocAnnCF1:=(1,0,0,1,1,0), snocAnnCF2:=(1,1,0,0,0,1)\mathsf{snocAnnCF2} := (1, 1, 0, 0, 0, 1)snocAnnCF2:=(1,1,0,0,0,1), and snocSymbolicAnnotation(p1,p2):=(snocAnnCA⊕(p1⊙snocAnnCF1))⊕(p2⊙snocAnnCF2)\mathsf{snocSymbolicAnnotation}(p_1, p_2) := (\mathsf{snocAnnCA} \oplus (p_1 \odot \mathsf{snocAnnCF1})) \oplus (p_2 \odot \mathsf{snocAnnCF2})snocSymbolicAnnotation(p1​,p2​):=(snocAnnCA⊕(p1​⊙snocAnnCF1))⊕(p2​⊙snocAnnCF2).

Assessment: sound

Concerns: None

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

    GiralfSemJudgment is the semantic lax homomorphism with no term syntax or typing rules; interpAARAType builds tensor and CList1 as PotentialType directly, presupposing Theorem 5.7.

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