\section{Machines, counting packages and the engine}\label{sec:engine} This section states the counting theorem that Theorems~A--C rely on. It has three layers. The bottom layer is the algorithm of \cite{A} for evaluating tensor networks (\S\ref{sec:networks}). The middle layer is the interface of \cite{T1}: \emph{counting packages}, uniform families of networks of linear total size, and a compiler that produces them from systems of arithmetic constraints whose variables have linear total width (\S\ref{sec:packages}). The top layer is new: polynomially many adaptive queries, adaptive calls to a machine, and linear padding of the input (\S\ref{sec:batches}). We state the first two layers as black boxes, with their Lean names, and prove the third. Throughout, for a natural number $n$ we write \[ X_A(n)=\frac{A\,(n+2)}{\sqrt{\log_2(n+2)}} , \] so that every running time below has the form $2^{X_A(n)}$ for a constant $A$, where $n$ is the length of the input word. Note that $2^{X_A(n)}=2^{O(n/\sqrt{\log n})}$ and that every polynomial in $n$ is bounded by $2^{X_A(n)}$ for a suitable $A$. \subsection{Machines and stepped programs}\label{sec:machines-model} Our final statements concern Mathlib's \lean{TM2} machines: deterministic machines with finitely many stacks over finite alphabets. Natural numbers are written in binary, least significant bit first, and $0$ is the empty word. For a machine $M$, words $w,y$ and $T\in\N$, $\lean{Outputs}\,M\,w\,y\,T$ means that $M$ started on $w$ halts within $T$ steps with $y$ on its output stack (\lean{TM2OutputsInTime}). Algorithms are first written as \emph{stepped programs} in the sense of \cite{A}: a set of states, an initialization map, a step map and an output map. We write $\mathcal P\colon x\Downarrow_{T,S}y$ if on input $x$ the program $\mathcal P$ halts after at most $T$ steps with output $y$ and all its states have size at most $S$. A program has \emph{polynomial-time steps} if its three maps are computable in polynomial time. Stepped programs compose: sequential composition, mapping the input through a polynomial-time function, and iteration a polynomial-time computable number of times (\lean{Stepped.bind}, \lean{Stepped.mapIn}, \lean{Srch.iterRange}). \begin{lemma}[Compilation {\cite{T1}}]\label{lem:compile} Let $\mathcal P$ be a stepped program on words with polynomial-time steps, $f$ a function on words and $A$ a constant such that for every word $w$ of length $n$ there are $T,S$ with $\mathcal P\colon w\Downarrow_{T,S}f(w)$ and $T,S,n\le2^{X_A(n)}$. Then there are a \lean{TM2} machine $M$ with finite alphabets and a constant $C$ such that for every $w$, \[ \lean{Outputs}\,M\,w\,f(w)\,\Bigl\lfloor C\cdot 2^{\,C\,n/\sqrt{\log_2(n+2)}}\Bigr\rfloor . \] \end{lemma} Lean: \lean{GR.TM\_of\_runs}. The proof simulates each step of $\mathcal P$ by a polynomial number of machine steps; the polynomial overhead and the additive constant $2$ are absorbed into $C$. \subsection{Networks and the algorithm of \texorpdfstring{\cite{A}}{[A]}} \label{sec:networks} We recall Definitions~2--4 and Lemma~1 of \cite{A}. \begin{definition}[{\cite[Definition~2]{A}}]\label{def:network} Let $P,B,\tau\ge1$. A \emph{network on $[g]$ with parameters $P,B,\tau$} consists of, for each node $a\in[g]$, a set $S_a\subseteq\{0,1\}^{P_a}$ of \emph{settings} with $P_a\le P$ and a function $W_a\colon S_a\to\Z$; and for each pair of distinct nodes $a,b$, a symmetric function $\Psi_{a,b}\colon S_a\times S_b\to\Z$, an integer $c_{a,b}=c_{b,a}\ge0$ and a factorization $\Psi_{a,b}(z,z')=\sum_{\theta\in\{0,1\}^{c_{a,b}}}f_{a,b}(z,\theta) f_{b,a}(z',\theta)$. It is required that (i) $\sum_{b\ne a}c_{a,b}\le P$ for every $a$; (ii) all values of all $W_a$, $\Psi_{a,b}$, $f_{a,b}$ are less than $2^B$ in absolute value; and (iii) an algorithm given $P$, $B$, the $P_a$ and the $c_{a,b}$ decides membership in $S_a$ and computes every value of $W_a$, $\Psi_{a,b}$, $f_{a,b}$ within $\tau$ operations. The \emph{value} of the network is \[ Z=\sum_{z\in\prod_aS_a}\ \prod_aW_a(z_a)\prod_{\{a,b\}}\Psi_{a,b}(z_a,z_b), \] and its \emph{total} is $U=\sum_{a\ne b}c_{a,b}$, the number of shared bits counted from both ends. \end{definition} For $K\ge2$ and $\lambda\ge1$, the \emph{$(K,\lambda)$-clique} with tables $W_1,\dots,W_K$ is the network on $[K]$ in which every edge carries a string of $\lambda$ bits, each node holds copies of the strings on its $K-1$ edges, and $\Psi$ checks that the two copies on each edge agree \cite[Definition~3]{A}. The pair $(K,e)$ is \emph{achievable} \cite[Definition~4]{A} if one algorithm computes the value of every $(K,\lambda)$-clique with tables bounded by $2^B$ within $C\cdot2^{eK^2\lambda}(B+\lambda)^C$ operations. \begin{theorem}[{\cite[Theorem~A(b) and Lemma~1]{A}}]\label{thm:A} There are a constant $C_0\ge1$, integers $2\le K_0\}$. Constants are given by their limbs. A \emph{leveled} description assigns a level $k\ge0$ to every variable and every chain. For a size parameter $n$, a variable of level $k$ ranges over a box $[0,2^{b\ell_k})$, where $b=b(n)$ and the widths $\ell_k=\ell_k(n)$ decrease geometrically with $k$ (\lean{LDesc}). The boxes are large enough: every $y$ with $y^{2^k}<2^n$ lies in the box of level $k$, so in particular the box of level $0$ contains $[0,2^n)$ (\lean{lbox\_spec}). The \emph{count} $\#_n(D)$ is the number of points of the product of the boxes that satisfy all constraints. The description is \emph{spread with constant $c$} if, for each kind of object (variables, products, chains, comparisons), the $i$-th object ($i=0,1,\dots$) has level at least $\floor{i/c}$ (\lean{LDesc.Spread}); so at most $c(k+1)$ objects of each kind have level at most $k$, and since the widths decrease geometrically the total width of all boxes is linear in $n$: $\#_n(D)\le2^{2^{13}c(n+2)}$ (\lean{countL\_le}). It is \emph{well formed at $n$} if all indices are in range, every product lies in a chain of its own level, operands have compatible levels, and all constants and patterns fit the limb parameters of $n$ (\lean{LDesc.WF}). \begin{theorem}[From descriptions to packages {\cite{T1}}]\label{thm:desc} Let $D(w,\pi)$ be a leveled description computed from $(w,\pi)$ in polynomial time, let $n_f(w)\le\abs w$ be given in unary, and let $R(w)\le\abs w$ be computable in polynomial time. Suppose that for a constant $c_S\ge1$ every $D(w,\pi)$ is spread with constant $c_S$ and has at most $\nu(n_f(w))+3$ levels, and that $D(w,\pi)$ is well formed at $n_f(w)$ whenever $\abs\pi\le R(w)$. Then there is a counting package with constant $c_0=2^{22}c_S$, with this $R$, with $\mathrm{Ok}$ always true, with $q_b=2^{13}c_S+1$ and with $Q(w,\pi)=\#_{n_f(w)}(D(w,\pi))$. \end{theorem} Lean: \lean{ArithLSpec.pkg\_exists}, in the library of \cite{T1} at commit \lean{4193596}, which also proves that the reader of the package is computable in polynomial time. The compiler distributes the bits of the variables over the $g$ nodes so as to meet condition~(3) of Definition~\ref{def:package}. The descriptions of Section~\ref{sec:network} are read with $n_f(w)=R(w)= \abs w$ and spread constant $c_S=132$. This gives the network constant $c_0=2^{22}\cdot132$ quoted in the introduction and $q_b=2^{13}\cdot132+1$ (\lean{FourKernel.package\_exists}, \lean{FourKernel.package\_exact\_exists}). \subsection{Batches of adaptive queries, calls to machines, and padding} \label{sec:batches} The results of this subsection are new. Proposition~\ref{prop:onequery} answers one query. Theorems~A--C need polynomially many, and for Theorem~C they depend on earlier answers. The essential point is that the exponent is governed by the length of the input word $w$ given to the package, which must stay linear, while the number of queries and all bookkeeping may be polynomial. \begin{definition}[Adaptive batch]\label{def:batch} An \emph{adaptive batch} for a counting package consists of functions $r$ (number of rounds), $\sigma_0$ (initial state), $\mathrm{qry}$, $\mathrm{upd}$ and $\mathrm{fin}$. On an input word $w$ it defines states $\sigma_0(w)$ and \[ \pi_i=\mathrm{qry}(w,i,\sigma_i),\qquad \sigma_{i+1}=\mathrm{upd}\bigl((w,i,\sigma_i),Q(w,\pi_i)\bigr) \qquad(0\le i