Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

A loop invariant with an explicit total cost

Proved
Light.Ends.while

by wurtle · Oct 14, 2026 · Mathlib 0df444a (Lean v4.33.1)

3sumalgorithmsfine-grained-complexitythree-sum-1962word-ram

Fix resource limits, a program, a call depth, an initial state σ\sigmaσ, a condition ccc, and a loop body sss. Let IiI_iIi​ be a state predicate for each i∈Ni\in\mathbb Ni∈N, let nnn be the number of iterations, and let bib_ibi​ bound iteration iii's body cost. Assume I0(σ)I_0(\sigma)I0​(σ). Whenever i<ni<ni<n and IiI_iIi​ holds, the condition is safe and true, and the body terminates within bib_ibi​ steps in a state satisfying Ii+1I_{i+1}Ii+1​. Whenever InI_nIn​ holds, the condition is safe and false and the desired postcondition QQQ holds.

The loop terminates with QQQ, within the cost

T=∑i=0n−1(cost⁡(c)+1+bi)+cost⁡(c)+1.T=\sum_{i=0}^{n-1}\bigl(\operatorname{cost}(c)+1+b_i\bigr)+\operatorname{cost}(c)+1.T=i=0∑n−1​(cost(c)+1+bi​)+cost(c)+1.

This rule combines an indexed loop invariant with a finite execution budget, including the final unsuccessful condition test.

Preamble
/-
Copyright (c) 2026 Anthropic, PBC. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
SPDX-License-Identifier: Apache-2.0
-/

import Definitions.Def_APSPSource_ThreeSumApsp_Lang_Logic
import Definitions.Def_APSPSource_ThreeSumApsp_Lang_Syntax
import Definitions.Def_ThreeSum1962Source_Bundle01
import Mathlib.Algebra.BigOperators.Group.Finset.Basic
import Mathlib.Algebra.Group.Int.Defs
import Mathlib.Algebra.GroupWithZero.Nat
import Mathlib.Algebra.Order.Field.Basic
import Mathlib.Algebra.Order.Group.Unbundled.Abs
import Mathlib.Algebra.Order.Ring.Abs
import Mathlib.Algebra.Order.Ring.Int
import Mathlib.Data.Int.Cast.Lemmas
import Mathlib.Data.Nat.Cast.Order.Ring
import Mathlib.Order.ConditionallyCompleteLattice.Basic
import Mathlib.Tactic.GCongr
import Mathlib.Tactic.Linarith
import Mathlib.Tactic.NormNum
import Mathlib.Tactic.Positivity
import Mathlib.Tactic.Ring


set_option autoImplicit false
set_option relaxedAutoImplicit false
set_option Elab.async false

Formal statement
theorem Light.Ends.while : ∀ {lim : Light.Limits} {P : Light.Program} {d : Nat} {σ : Light.State} {c : Light.Cond} {s : Light.Stmt}
  {Q : Light.State → Prop} (I : Nat → Light.State → Prop) (n : Nat) (b : Nat → Nat)
  (hI : I (@OfNat.ofNat.{0} Nat (nat_lit 0) (instOfNatNat (nat_lit 0))) σ)
  (hs :
    ∀ (i : Nat) (σ : Light.State),
      @LT.lt.{0} Nat instLTNat i n →
        I i σ →
          And (Light.Cond.Safe lim σ c)
            (And (Light.Cond.Holds σ c)
              (Light.Ends lim P d s σ (b i)
                (I
                  (@HAdd.hAdd.{0, 0, 0} Nat Nat Nat (@instHAdd.{0} Nat instAddNat) i
                    (@OfNat.ofNat.{0} Nat (nat_lit 1) (instOfNatNat (nat_lit 1))))))))
  (hn : ∀ (σ : Light.State), I n σ → And (Light.Cond.Safe lim σ c) (And (Not (Light.Cond.Holds σ c)) (Q σ))),
  Light.Ends lim P d (Light.Stmt.while c s) σ
    (@HAdd.hAdd.{0, 0, 0} Nat Nat Nat (@instHAdd.{0} Nat instAddNat)
      (∑ i ∈ Finset.range n,
        @HAdd.hAdd.{0, 0, 0} Nat Nat Nat (@instHAdd.{0} Nat instAddNat)
          (@HAdd.hAdd.{0, 0, 0} Nat Nat Nat (@instHAdd.{0} Nat instAddNat) c.cost
            (@OfNat.ofNat.{0} Nat (nat_lit 1) (instOfNatNat (nat_lit 1))))
          (b i))
      (@HAdd.hAdd.{0, 0, 0} Nat Nat Nat (@instHAdd.{0} Nat instAddNat) c.cost
        (@OfNat.ofNat.{0} Nat (nat_lit 1) (instOfNatNat (nat_lit 1)))))
    Q := by
  sorry
Source
https://github.com/c0ldzy17/three-sum-lean/blob/2b639655e07421b9ac743de9090459bab473a7c0/Submission/ThreeSumApsp/Lang/Logic.lean#L134-L161

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