A left fold is polynomial-time if the accumulators along the run have polynomially bounded codes
ProvedFptBridge.poly_foldlSetting. Automatic.Poly is the polynomial-time calculus of the Boolean-stack runtime in Definitions.Def_QuantumMaxCutFiniteNumericRuntime: Automatic.Poly g says that some PolyProgram (a finite multi-stack machine with Boolean stacks, together with a polynomial) computes on the canonical codes enc of its arguments, halting on every argument within the polynomial evaluated at the length of the code of the argument.
Statement. Let xs (a list computed from the input ), g0 (the initial accumulator, computed from ) and op (the step, as a function of the triple of the input, the accumulator and an element of the list) be Automatic.Poly. Suppose there is a polynomial such that for every input and every prefix pre of the list xs a, the code of the accumulator obtained by folding pre from g0 a has length at most . Then (xs a).foldl (op a) (g0 a) is Automatic.Poly.
Use. The size condition is asked only along the actual run from the initial accumulator, which is what an invariant of the computation provides; it is not asked for arbitrary accumulators. This makes loops whose state can grow multiplicatively (tables of numbers, iterated products) provable polynomial-time once a bound on the entries is known.
Relation to the runtime. PolyProgram.listFold of the definition module folds a pair (list, initial value) given as the input, has no further parameter, and asks for the size bound for all lists and initial values. Here the list and the initial value are computed from an input that the step may also read, the bound is needed only for that list and that initial value, and the statement is in the Automatic.Poly form used with fun_prop.
import Mathlib import Definitions.Def_QuantumMaxCutFiniteNumericRuntime
namespace FptBridge
open OAI.MetricKMedian.Binary OAI.MetricKMedian.Binary.Automatic
/-- **Bounded fold.** A left fold with a polynomial-time step over a polynomial-time list is
polynomial-time if a polynomial `B` bounds the length of the code of every accumulator that
occurs in the run on the input `a`, that is, after folding any prefix of the list. -/
theorem poly_foldl {α β γ : Type} [Coding α] [Coding β] [Coding γ]
{op : α → γ → β → γ} {xs : α → List β} {g0 : α → γ}
(hop : Automatic.Poly (fun p : α × (γ × β) => op p.1 p.2.1 p.2.2))
(hxs : Automatic.Poly xs) (hg : Automatic.Poly g0)
(B : Polynomial ℕ)
(hB : ∀ a, ∀ pre suf, xs a = pre ++ suf →
(enc (pre.foldl (op a) (g0 a))).length ≤ B.eval (enc a).length) :
Automatic.Poly (fun a => (xs a).foldl (op a) (g0 a)) := by sorry
end FptBridge