A counting-loop rule with a constant body budget
ProvedLight.Ends.for3sumalgorithmsfine-grained-complexitythree-sum-1962word-ram
Fix a program, resource limits, and a call depth. Consider a loop whose counter register starts at zero, whose upper-bound expression has fixed value , and whose body costs at most per iteration. Let be state predicates. Assume initialization establishes ; every body execution at counter preserves that counter and establishes after incrementing it; and counter with implies the postcondition . Also assume is safe and equals in every invariant state with , and fits in a word. If
the counting loop terminates within steps with . This rule includes initialization, increments, and all guard evaluations.
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_RemainingDefinitions
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.Abs
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
import Theorems.Thm_Light_Ends_while
set_option autoImplicit false
set_option relaxedAutoImplicit false
set_option Elab.async false
open Light
variable {lim : Limits} {P : Program} {d : ℕ}
Formal statement
theorem Light.Ends.for {σ : State} {i : ℕ} {hi : Expr} {body : Stmt} {T : ℕ} {Q : State → Prop}
(I : ℕ → State → Prop) (n b : ℕ)
(start : I 0 { σ with loc := Function.update σ.loc i 0 })
(round : ∀ (j : ℕ) (σ : State), j < n → σ.loc i = j → I j σ → Ends lim P d body σ b fun σ' =>
σ'.loc i = j ∧ I (j + 1) { σ' with loc := Function.update σ'.loc i ((j : ℤ) + 1) })
(done : ∀ σ : State, σ.loc i = n → I n σ → Q σ)
(bound : ∀ (j : ℕ) (σ : State), j ≤ n → σ.loc i = j → I j σ → hi.Safe lim σ ∧ hi.val σ = n)
(hn : (n : ℤ) ≤ lim.word := by omega)
(hT : n * (hi.cost + b + 7) + hi.cost + 5 ≤ T := by first
|
((simp only [Light.Stmt.blockCost, Light.Cond.cost, Light.Expr.cost,
List.map_cons, List.map_nil, List.sum_cons, List.sum_nil, ]);
(first
| omega
| ((ring_nf); (omega))))
| omega
|
(simp [] <;>
first
| omega
| ((ring_nf); (omega)))) :
Ends lim P d (Stmt.for i hi body) σ T Q := by
sorry
Source