A loop invariant with an explicit total cost
ProvedLight.Ends.while3sumalgorithmsfine-grained-complexitythree-sum-1962word-ram
Fix resource limits, a program, a call depth, an initial state , a condition , and a loop body . Let be a state predicate for each , let be the number of iterations, and let bound iteration 's body cost. Assume . Whenever and holds, the condition is safe and true, and the body terminates within steps in a state satisfying . Whenever holds, the condition is safe and false and the desired postcondition holds.
The loop terminates with , within the cost
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