A left fold is polynomial-time if each step lengthens the accumulator's code by at most a multiple of the element's code plus a polynomial in the input length
ProvedFptBridge.poly_foldl_growSetting. 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 are a number and a polynomial such that for every input , accumulator and element , . Then (xs a).foldl (op a) (g0 a) is Automatic.Poly. Here is the length that the element contributes to the code of a list containing it.
Use. Loops that copy or append the current element a bounded number of times: summing a list of numbers, filtering, concatenating, removing duplicates. With the hypothesis says that a step lengthens the code of the accumulator by at most : counters, flags, a list that receives one entry of bounded size per step.
Relation to the runtime. This is the hypothesis of PolyProgram.listFoldLinear of the definition module, with an input that the step may read and the additional term , 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
/-- **Fold with growth bounded by the element.** A left fold with a polynomial-time step over
a polynomial-time list is polynomial-time if one step enlarges the code of the accumulator by at
most `c` times the length `2 * |b| + 1` that the element `b` contributes to the code of a list,
plus `D(|a|)` for a polynomial `D`. With `c = 0` this is a fold with additive growth. -/
theorem poly_foldl_grow {α β γ : 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)
(c : ℕ) (D : Polynomial ℕ)
(hD : ∀ a g b, (enc (op a g b)).length ≤
(enc g).length + c * (2 * (enc b).length + 1) + D.eval (enc a).length) :
Automatic.Poly (fun a => (xs a).foldl (op a) (g0 a)) := by sorry
end FptBridge