Complexitylib: a blueprint for computational complexity in Lean

6 Space, alternation, and QBF

This chapter covers space-bounded computation: the parametric classes \(\mathsf{DSPACE}\) and \(\mathsf{NSPACE}\), the logarithmic-space classes \(\mathsf{L}\), \(\mathsf{NL}\), \(\mathsf{coNL}\) and \(\mathsf{FL}\), polynomial space, and quantified Boolean formulas. Space is measured by an honest auxiliary-space convention in which neither the input tape nor the output tape can serve as hidden workspace. Formalized so far: the elementary containments, closure of \(\mathsf{PSPACE}\) under complement, regular languages in \(\mathsf{L}\), \(\mathsf{L} \subseteq \mathsf{P}\), \(\mathsf{FL} \subseteq \mathsf{FP}\), \(\mathsf{NL} \subseteq \mathsf{P}\), Savitch’s theorem in the polynomial-union form \(\mathsf{PSPACE} = \mathsf{NPSPACE}\), \(\mathsf{PSPACE} \subseteq \mathsf{EXP}\), the syntax and semantics of quantified Boolean formulas, space-aware Hoare contracts, and a reduction of the Immerman–Szelepcsényi theorem to the construction of one counting machine. Savitch’s theorem and \(\mathsf{IP} \subseteq \mathsf{PSPACE}\) both go through one lever: iterating a polynomial-time step function on a polynomially bounded state stays in \(\mathsf{PSPACE}\). The main open directions are the unconditional \(\mathsf{NL} = \mathsf{coNL}\), a log-space programming layer, PSPACE-completeness of TQBF, alternation, and the space hierarchy theorems. Throughout, \(\log n\) is \(\lfloor \log _2 n \rfloor \) and \(f = O(g)\) is eventual domination up to a constant factor.

6.1 Space-bounded deciders and classes

A configuration on an input of length \(n\) is within auxiliary space \(s\) when every work head is at position at most \(s\) and the input head is at position at most \(n + s + 1\): the input and its first trailing blank are free, and travel farther into the blank tail is charged. The output head is not constrained. A configuration is within decision space \(s\) when in addition its output head is at position at most \(s + 1\); only the verdict cell \(1\) is free.

A DTM decides \(L\) in space \(S\) if every configuration reachable from its initial configuration on \(x\) is within decision space \(S(|x|)\) and, for every \(x\), some reachable configuration is halted with verdict \(1\) if \(x \in L\) and \(0\) if \(x \notin L\). An NTM decides \(L\) in space \(S\) if there is a time bound \(T\) such that every choice sequence halts within \(T(|x|)\) steps, \(x \in L\) exactly when some choice sequence of length \(T(|x|)\) ends halted with verdict \(1\), and every configuration along every path, up to time \(T(|x|)\), is within decision space \(S(|x|)\). A machine is a transducer if its transition function never moves the output head left (for an NTM, under either choice bit); the two deciding predicates do not require this. A DTM computes \(f\) in space \(S\) if it is a transducer, every reachable configuration is within auxiliary space \(S(|x|)\), and on every input \(x\) it halts with output \(f(x)\); the output length is not charged.

Definition 192 Deterministic space
✓
#

\(\mathsf{DSPACE}(S)\) is the class of languages decided by some DTM, with any number of work tapes, in space \(f\) for some \(f = O(S)\).

Definition 193 Nondeterministic space
✓
#

\(\mathsf{NSPACE}(S)\) is the class of languages decided by some NTM, with any number of work tapes, in space \(f\) for some \(f = O(S)\).

Definition 194 Polynomial space
✓
#

\(\mathsf{PSPACE} = \bigcup _{k} \mathsf{DSPACE}(n^k)\).

Definition 195 Nondeterministic polynomial space
✓
#

\(\mathsf{NPSPACE} = \bigcup _{k} \mathsf{NSPACE}(n^k)\).

Definition 196 Logarithmic space
✓
#

\(\mathsf{L}\) is the class of languages decided by a deterministic transducer, with any number of work tapes, in space \(f\) for some \(f = O(\log n)\).

Definition 197 Nondeterministic logarithmic space
✓
#

\(\mathsf{NL}\) is the class of languages decided by a nondeterministic transducer, with any number of work tapes, in space \(f\) for some \(f = O(\log n)\).

Definition 198 Complement of NL
✓
#

\(\mathsf{coNL}\) is the class of languages whose complements are in \(\mathsf{NL}\).

Definition 199 Log-space functions and search problems
✓

\(\mathsf{FL}\) is the class of functions \(\{ 0,1\} ^* \to \{ 0,1\} ^*\) computed by a DTM in space \(S\) for some \(S = O(\log n)\). The output is unbounded in length and one-way. \(\mathsf{FNL}\) is the class of polynomially balanced relations whose pair language is in \(\mathsf{L}\), and \(\mathsf{TFNL}\) consists of the relations in \(\mathsf{FNL}\) that are total: every \(x\) has some \(y\) with \(R(x, y)\).

Definition 200 Simultaneous time and space; SC
✓

A DTM decides \(L\) in time \(T\) and space \(S\) if every reachable configuration on \(x\) is within decision space \(S(|x|)\) and on every input it halts within \(T(|x|)\) steps with the correct verdict. \(\mathsf{DTISP}(T, S)\) is the class of languages decided by a single DTM in time \(t\) and space \(s\) for some \(t = O(T)\) and \(s = O(S)\). Steve’s class is \(\mathsf{SC} = \bigcup _{k, j} \mathsf{DTISP}(n^k, (\log n)^j)\).

6.2 Elementary containments and closure

If a DTM decides \(L\) in space \(S\), then so does its embedding as an NTM whose two transition functions coincide, and the embedding of a transducer is a transducer. Hence for every \(S\), \(\mathsf{DSPACE}(S) \subseteq \mathsf{NSPACE}(S)\). If \(S_1 = O(S_2)\) then \(\mathsf{DSPACE}(S_1) \subseteq \mathsf{DSPACE}(S_2)\) and \(\mathsf{NSPACE}(S_1) \subseteq \mathsf{NSPACE}(S_2)\). Consequently \(\mathsf{L} \subseteq \mathsf{NL}\) and \(\mathsf{PSPACE} \subseteq \mathsf{NPSPACE}\).

Proof ▶

The embedded NTM follows the same run on every choice sequence; its uniform time bound is the largest halting time over the finitely many inputs of each length. Monotonicity is transitivity of \(O\).

For every \(T\), \(\mathsf{DTIME}(T) \subseteq \mathsf{DSPACE}(T)\) and \(\mathsf{DTIME}(T) \subseteq \mathsf{NSPACE}(T)\). Hence \(\mathsf{P} \subseteq \mathsf{PSPACE}\) and \(\mathsf{P} \subseteq \mathsf{NPSPACE}\).

Proof ▶

Every head moves at most one cell per step and a decider in time \(f\) halts within \(f(|x|)\) steps, so every reachable configuration of the same machine is within decision space \(f(|x|)\). The nondeterministic statements compose this with Lemma 201.

Lemma 203 Closure under complement
✓

If the constant function \(1\) is \(O(S)\), then \(\mathsf{DSPACE}(S)\) is closed under complement. In particular \(\mathsf{PSPACE}\) is closed under complement.

Proof ▶

Run the decider, rewind the output head to the verdict cell, and flip the bit. The rewind costs one extra cell, so the new machine decides the complement in space \(f + 1\); the delicate point is that the space predicate constrains every reachable configuration, not only the last.

Lemma 204 PSPACE as an explicit polynomial window
✓

\(L \in \mathsf{PSPACE}\) if and only if there are a DTM \(M\) and a polynomial \(q\) with natural-number coefficients such that every configuration \(M\) reaches on \(x\) is within decision space \(q(|x|)\) and \(M\) halts on every input with the correct verdict.

Proof ▶

Unfold the union and replace the asymptotic bound by a dominating polynomial. The iteration lemma, Savitch’s theorem, and the \(\mathsf{PP} \subseteq \mathsf{PSPACE}\) and \(\mathsf{PH} \subseteq \mathsf{PSPACE}\) machines enter \(\mathsf{PSPACE}\) through the converse direction.

Every regular binary language, in the sense shared by Mathlib and CSLib (accepted by a finite deterministic automaton), is in \(\mathsf{DSPACE}(0)\), hence in \(\mathsf{DSPACE}(S)\) for every \(S\), and is in \(\mathsf{L}\). The languages of CSLib’s finite nondeterministic automata, one-way or two-way, the languages of regular expressions over \(\{ 0,1\} \), and the preimages of subsets of finite monoids under monoid homomorphisms from binary strings are in \(\mathsf{L}\).

Proof ▶

The finite-state scanner of Theorem 105 has no work tapes, never moves its input head past the first blank after the input, and never moves its output head past the verdict cell, so it decides the language in space \(0\). Its output head never moves left, so it is also a transducer, as membership in \(\mathsf{L}\) requires. For the other models, CSLib first shows that the language is regular.

6.3 Configuration graphs and space-to-time

The configuration graph of an NTM has an edge from each non-halted configuration to each of its two successors, one per choice bit. \(\mathrm{ReachesCfg}\) is the reflexive-transitive closure of this relation, \(\mathrm{ReachesCfgIn}(t)\) and \(\mathrm{ReachesCfgLe}(t)\) are reachability in exactly and in at most \(t\) steps, and \(\mathrm{reachSet}(c_0, t)\) is the set of configurations reached from \(c_0\) within \(t\) rounds of successor closure, that is, the state of breadth-first search after \(t\) rounds.

Fix an input \(x\) of length \(n\) and a space bound \(s\), and consider configurations with state set \(Q\) and \(k\) work tapes. A configuration is windowed if its input tape holds the initial input contents, every work cell beyond position \(s\) is blank, and every output cell beyond position \(s + 1\) is blank; the initial configuration is windowed. The code of a configuration records its state, its input head clamped to at most \(n + s + 1\), each work head clamped to at most \(s\) together with work cells \(0, \dots , s\), and its output head clamped to at most \(s + 1\) together with output cells \(0, \dots , s + 1\). Two windowed configurations within decision space \(s\) with the same code are equal. There are exactly \(|Q|\, (n + s + 2)\, \bigl((s + 1)\, 4^{s + 1}\bigr)^k\, (s + 2)\, 4^{s + 2}\) codes, which is at most \(2^{|Q| + (n + s + 2) + 3k(s + 1) + 3(s + 2)}\). A DTM step, or an NTM step under either choice bit, from a windowed configuration within decision space \(s\) gives a windowed configuration. Hence, if every configuration reachable in the configuration graph from a windowed configuration is within decision space \(s\), then all of them are windowed. These declarations live in proof-internal modules and are shared by \(\mathsf{PSPACE} \subseteq \mathsf{EXP}\), \(\mathsf{NL} \subseteq \mathsf{P}\), Savitch’s theorem and the inductive-counting certificate. The sharper count for transducers under auxiliary space, in Theorem 208, is separate.

Let \(M\) be a deterministic transducer with state set \(Q\) and \(k\) work tapes, and let \(B(n, s) = 8\, |Q|\, (n + s + 2)\, \bigl((s + 1)\, 4^{s + 1}\bigr)^k\), the exact number of reduced snapshots (state, input head, bounded view of each work tape, whether the output head is on the left marker, and the symbol under the output head). If every configuration \(M\) reaches from its initial configuration on \(x\) is within auxiliary space \(s\), and \(M\) reaches a halted configuration in \(t\) steps, then \(t + 1 \le B(|x|, s)\); in particular \(t \le B(|x|, s)\). Consequently, if \(M\) decides \(L\) in space \(S\) then \(M\) decides \(L\) in time \(n \mapsto B(n, S(n))\), and if \(M\) computes \(f\) in space \(S\) then \(M\) computes \(f\) in time \(n \mapsto B(n, S(n))\).

Proof ▶

For a one-way output tape the written prefix cannot influence future execution, so a halting run cannot repeat a reduced snapshot.

Theorem 209 \(\mathsf{L} \subseteq \mathsf{P}\) and \(\mathsf{FL} \subseteq \mathsf{FP}\)
✓

If \(S = O(\log n)\), then for every DTM with \(k\) work tapes there is \(c\) with \(B(n, S(n)) = O(n^{1 + (2c + 1)k})\), where \(B\) is the snapshot count of Theorem 208. Hence \(\mathsf{L} \subseteq \mathsf{P}\) and \(\mathsf{FL} \subseteq \mathsf{FP}\).

Proof ▶

The same machine runs within the reduced-snapshot bound, which is polynomial for logarithmic space. The same argument places every language of \(\mathsf{L}\) in \(\bigcup _k \mathsf{DTISP}(n^k, \log n) \subseteq \mathsf{SC}\), with the same machine as witness; that corollary is not yet stated in the library.

\(\mathsf{PSPACE} \subseteq \mathsf{EXP}\). More precisely, if a DTM \(M\) with state set \(Q\) and \(k\) work tapes decides \(L\) in space \(f\), transducer or not, then \(M\) decides \(L\) in time \(n \mapsto |Q|\, (n + f(n) + 2)\, \bigl((f(n) + 1)\, 4^{f(n) + 1}\bigr)^k\, (f(n) + 2)\, 4^{f(n) + 2}\), and if \(f = O(n^m)\) this bound is \(O(2^{n^j})\) for some \(j\).

Proof ▶

Every configuration of the run is windowed, so distinct configurations of the run have distinct codes, and a deterministic halting run visits no configuration twice before it halts. The halting time is therefore below the number of codes, and the same machine decides the language in exponential time.

6.4 Nondeterministic logarithmic space

If an NTM \(M\) decides \(L\) in some space bound, then \(x \in L\) if and only if some configuration reachable from \(\mathrm{init}(x)\) in the configuration graph of \(M\) is halted with verdict \(1\). For every \(L \in \mathsf{NL}\) there are an NTM \(M\) and constants \(A, B\) such that for every \(x\): \(x \in L\) if and only if some configuration in \(\mathrm{reachSet}(\mathrm{init}(x), A(|x| + 1)^B)\) is halted with verdict \(1\). Equivalently, \(x \notin L\) if and only if no configuration of that set is halted with verdict \(1\).

Proof ▶

A log-space machine has polynomially many configuration codes inside its window, so breadth-first search saturates within polynomially many rounds.

\(\mathsf{NL} \subseteq \mathsf{P}\) and \(\mathsf{coNL} \subseteq \mathsf{P}\). The library also records a reduction of \(\mathsf{NL} \subseteq \mathsf{P}\) to one machine, which the proof below does not use: if for every NTM \(M\) deciding some language in space \(S = O(\log n)\) and all constants \(A, B\) some DTM decides, within an explicit polynomial time bound, the set of \(x\) such that some configuration in \(\mathrm{reachSet}(\mathrm{init}(x), A(|x| + 1)^B)\) of \(M\) is halted with verdict \(1\), then \(\mathsf{NL} \subseteq \mathsf{P}\).

Proof ▶

The worklist search over encoded configurations is programmed in Cobham’s function algebra and run for polynomially many rounds, and the verdict function gives membership in \(\mathsf{P}\). The \(\mathsf{coNL}\) case follows because \(\mathsf{P}\) is closed under complement; it does not use the Immerman–Szelepcsényi theorem.

(1) If \(T \subseteq \mathrm{reachSet}(c_0, i)\) and \(|\mathrm{reachSet}(c_0, i)| \le |T|\) then \(T = \mathrm{reachSet}(c_0, i)\). The coded search starts from one configuration code and in each round adds the codes of the successors of the configurations the current codes decode to. (2) A code lies in round \(i\) of the coded search from \(a_0\) if and only if some sequence of \(i\) steps from \(a_0\), each staying put or moving to such a successor code, reaches it. (3) If a list of distinct members of round \(i\) is at least as long as the round, every code missing from the list is outside the round. (4) For every \(L \in \mathsf{NL}\) there are an NTM and constants \(C, D, A, B\) such that \(x \notin L\) if and only if round \(A(|x| + 1)^B\) of the coded search from the code of \(\mathrm{init}(x)\), in the window \(C \log |x| + D\), can be listed in this sense with no member decoding to a halted configuration with verdict \(1\).

Proof ▶

A subset of a finite set that is at least as large is the whole set; the codes are the fixed-width configuration codes of Definition 207.

\(\mathsf{NL} \subseteq \mathsf{coNL}\) if and only if \(\mathsf{coNL} \subseteq \mathsf{NL}\). In particular, if \(\mathsf{NL} \subseteq \mathsf{coNL}\) then \(\mathsf{NL} = \mathsf{coNL}\).

Proof ▶

Complement both sides of the inclusion.

Proposition 215 NL is closed under complement, given a counting machine
✓

Suppose that for every NTM \(M\) deciding some language in space \(S\) with \(S = O(\log n)\), and all constants \(A, B\), there are constants \(C, D\) and a nondeterministic transducer deciding, in space \(C \log n + D\), the set of inputs \(x\) such that no configuration in \(\mathrm{reachSet}(\mathrm{init}(x), A(|x| + 1)^B)\) of \(M\) is halted with verdict \(1\). Then \(\mathsf{NL} \subseteq \mathsf{coNL}\).

Proof ▶

Apply the hypothesis to the machine and bounds supplied by Lemma 211; the decided set is exactly the complement.

Theorem 216 Immerman–Szelepcsényi

\(\mathsf{NL} = \mathsf{coNL}\).

Proof ▶

It remains to build the counting machine required by Proposition 215: a nondeterministic log-space machine that guesses the certificate of Lemma 213 entry by entry, with three nested bounded loops (rounds, codes of a round, steps of a walk) over a constant number of logarithmically wide registers. The planned route builds it deterministically over a guess tape and converts it to an NTM. The statement \(\mathsf{NL} \subseteq \mathsf{coNL}\) is recorded as the proposition NLSubsetCoNL.

6.5 Savitch’s theorem and the iteration lever

Lemma 217 Iterating a polynomial-time function in polynomial space
✓

Let \(G \in \mathsf{FP}\), let \(r, w\) be polynomials with natural-number coefficients, and let \(N : \{ 0,1\} ^* \to \mathbb {N}\) satisfy \(1 \le N(x) \le 2^{w(|x|)}\). Write \(y_i = G^i(\mathrm{pair}(\varepsilon , x))\). Suppose \(|y_i| \le r(|x|)\) for \(i \le N(x) + 1\), \(y_i\) does not begin with \(1\) (it is empty or begins with \(0\)) for \(0 {\lt} i {\lt} N(x)\), \(y_{N(x)}\) begins with \(1\) (the done flag), and \(y_{N(x)+1}\) is nonempty. If \(x \in L\) exactly when \(y_{N(x)+1}\) begins with \(1\), then \(L \in \mathsf{PSPACE}\).

Proof ▶

The machine stores only the current state and a binary iteration counter of polynomially many bits, applies \(G\) in place until the done flag appears, and publishes the verdict after one more application. This is the polynomial-space counterpart of the iteration lemma for \(\mathsf{FP}\) in Cobham’s algebra.

(1) For an NTM, \(c\) reaches \(c'\) within \(2^{i+1}\) steps if and only if some midpoint is reachable from \(c\) within \(2^i\) steps and reaches \(c'\) within \(2^i\) steps. (2) If a map into a finite type of cardinality at most \(N\) separates the configurations reachable from \(c_0\), then \(c\) is reachable from \(c_0\) if and only if it is reachable within \(N\) steps. (3) For every \(L \in \mathsf{NPSPACE}\) there are an NTM and a polynomial \(q\) with natural-number coefficients such that \(x \in L\) if and only if some halted configuration with verdict \(1\) is reachable from \(\mathrm{init}(x)\) within \(2^{q(|x|)}\) steps.

Proof ▶

A walk longer than the number of codes repeats a configuration, and the loop can be cut out; halving splits a walk at its midpoint. For (3), membership is reachability of an accepting configuration, and the configuration codes number at most \(2\) to a polynomial.

\(\mathsf{NPSPACE} \subseteq \mathsf{PSPACE}\), hence \(\mathsf{PSPACE} = \mathsf{NPSPACE}\).

Proof ▶

Savitch’s recursion is written as a stack machine whose single step is a polynomial-time function, built in Cobham’s algebra, on a polynomially long encoded stack; the midpoint enumeration ranges over every string of the configuration-code width. The step is iterated \(2^{\mathrm{poly}}\) times by Lemma 217. The reverse inclusion is the embedding of DTMs into NTMs. No bound of the form \(O(S^2)\) is extracted.

Theorem 220 Savitch, parametric form

If \(S\) is space-constructible and \(S(n) \ge \log n\), then \(\mathsf{NSPACE}(S) \subseteq \mathsf{DSPACE}(S^2)\).

Proof ▶

The formalized theorem is only the polynomial-union corollary. The parametric form needs the recursion to run in space \(O(S^2)\) with \(S\) computed in space \(O(S)\), rather than through a polynomial-time step function.

Theorem 221 Polynomial-depth game trees are evaluated in PSPACE

Fix polynomials \(d\) and \(\ell \). Suppose that on input \(x\) a game tree has nodes that are strings of length at most \(\ell (|x|)\) and depth at most \(d(|x|)\); the children of a node are enumerated in order by a polynomial-time successor function, so the branching may be exponential; leaves carry polynomial-time values; and each internal node folds its children’s values with a polynomial-time operation (such as maximum, minimum, sum, \(\vee \) or \(\wedge \)) whose intermediate values have polynomial length. Then every language defined by a polynomial-time predicate of the root value is in \(\mathsf{PSPACE}\).

Proof ▶

Depth-first evaluation with a stack of at most \(d(|x|)\) frames, each holding a node, a child cursor and an accumulator. One step of the walk is polynomial time, so Lemma 217 applies. Instances: Savitch’s theorem (an \(\vee \) over midpoints of an \(\wedge \) of two subcalls), \(\mathsf{IP} \subseteq \mathsf{PSPACE}\) (maximum over prover replies, sum over verifier messages), \(\mathsf{PH} \subseteq \mathsf{PSPACE}\) (\(\vee \) and \(\wedge \) over witnesses), \(\mathsf{PP} \subseteq \mathsf{PSPACE}\) (a sum over choice sequences), \(\mathsf{TQBF} \in \mathsf{PSPACE}\), and \(\mathsf{NP} \cup \mathsf{coNP} \subseteq \mathsf{PSPACE}\), which is not yet stated. Today Savitch’s theorem and \(\mathsf{IP} \subseteq \mathsf{PSPACE}\) each program their own stack walk on Lemma 217, and \(\mathsf{PH} \subseteq \mathsf{PSPACE}\) and \(\mathsf{PP} \subseteq \mathsf{PSPACE}\) assemble dedicated machines; one generic theorem would subsume all four.

6.6 Quantified Boolean formulas and alternation

A QBF is built from variables \(x_i\) (\(i \in \mathbb {N}\)), the constants \(\top \) and \(\bot \), negation, conjunction, disjunction, and the quantifiers \(\exists x_i\) and \(\forall x_i\). It is evaluated under an assignment \(\alpha : \mathbb {N} \to \{ 0,1\} \); a quantifier over \(x_i\) evaluates its body under both updates of \(\alpha \) at \(i\) and takes the disjunction (\(\exists \)) or conjunction (\(\forall \)) of the two values. A formula is closed when it has no free variables, and true when it evaluates to \(1\) under the all-false assignment. The quantifier depth is the largest number of quantifiers on a root-to-leaf path; it bounds, but does not count, alternations, and a formula is quantifier-free when its depth is \(0\).

\(\exists x_i\, \varphi \) is true under \(\alpha \) if and only if \(\varphi \) is true under \(\alpha [i \mapsto b]\) for some \(b\), and dually for \(\forall \). Evaluation depends only on the free variables, so a closed formula has an assignment-independent value, and updating or quantifying a variable that is not free changes nothing. Under every assignment, \(\neg \exists x_i\, \varphi \) has the value of \(\forall x_i\, \neg \varphi \), and \(\neg \forall x_i\, \varphi \) that of \(\exists x_i\, \neg \varphi \). Truth commutes with negation, conjunction and disjunction, and if \(\forall x_i\, \varphi \) is true then so is \(\exists x_i\, \varphi \). A conjunction is quantifier-free exactly when both conjuncts are.

Proof ▶

Structural induction on the formula.

Definition 224 TQBF

A bit-string codec for QBF with polynomial-time decoding and a polynomial size bound, and the language \(\mathsf{TQBF}\) of codes of closed true formulas.

Theorem 225 \(\mathsf{TQBF} \in \mathsf{PSPACE}\)

\(\mathsf{TQBF} \in \mathsf{PSPACE}\).

Proof ▶

Each quantifier is a binary node of a game tree of depth at most the formula size.

\(\mathsf{TQBF} \in \mathsf{PSPACE}\), and every \(L \in \mathsf{PSPACE}\) satisfies \(L \le _p \mathsf{TQBF}\) under polynomial-time many-one reductions.

Proof ▶

Hardness: express “\(c\) reaches \(c'\) within \(2^i\) steps” by a formula that quantifies universally over the pair of halves, so its size is linear in \(i\) rather than exponential, over a formula for one step between configuration codes.

Definition 227 Alternating machines

An alternating TM partitions its non-halting states into existential and universal states; acceptance is the least fixed point of the induced \(\vee \)/\(\wedge \) labelling of the configuration graph, or equivalently a bounded game between two players. \(\mathsf{AP}\) is the class of languages decided by an alternating machine all of whose paths halt in polynomial time.

Theorem 228 \(\mathsf{AP} = \mathsf{PSPACE}\)

\(\mathsf{AP} = \mathsf{PSPACE}\).

Proof ▶

\(\mathsf{AP} \subseteq \mathsf{PSPACE}\) is an instance of game-tree evaluation; \(\mathsf{PSPACE} \subseteq \mathsf{AP}\) follows by evaluating TQBF alternately, or directly by alternating Savitch’s midpoint recursion.

6.7 Space hierarchy

Definition 229 Space-constructible functions

\(S\) is space-constructible if some DTM maps \(1^n\) to the binary representation of \(S(n)\) in space \(O(S(n))\). This is distinct from the library’s time-constructibility and from the randomness-bound condition used in the PCP theorem.

Theorem 230 Deterministic space hierarchy

If \(S'\) is space-constructible, \(S'(n) \ge \log n\) and \(S = o(S')\), then \(\mathsf{DSPACE}(S) \subsetneq \mathsf{DSPACE}(S')\).

Proof ▶

Diagonalize against machines with a space-bounded universal simulation, reusing the diagonal-machine infrastructure of the time hierarchy theorem.

Theorem 231 Nondeterministic space hierarchy

If \(S'\) is space-constructible, \(S'(n) \ge \log n\) and \(S = o(S')\), then \(\mathsf{NSPACE}(S) \subsetneq \mathsf{NSPACE}(S')\).

Proof ▶

Closure of nondeterministic space under complement, by inductive counting, makes the diagonal argument go through.

6.8 A log-space programming layer

Membership in \(\mathsf{FL}\) is currently established machine by machine, for example for the tableau serializer behind logspace-uniform circuits, and the only structural fact about \(\mathsf{FL}\) is \(\mathsf{FL} \subseteq \mathsf{FP}\). Two layers already support writing such machines. Space-aware Hoare contracts (Definition 232) pair a time-bounded triple with an all-reachable auxiliary-space bound, compose sequentially at the larger of the two budgets, and package per-input contracts of a transducer into a \(\mathrm{ComputesInSpace}\) statement. An experimental layer of proof-carrying binary register routines (BinaryRoutine) composes sequentially, supports loops with width certificates that collapse to logarithmic space, and yields a \(\mathrm{ComputesInSpace}\) contract; its programs see only the input length, not the input bits. The planned nodes below would make log-space programming as routine as Cobham’s algebra makes polynomial time.

Definition 232 Space-aware Hoare contracts
✓

For a DTM \(M\), a precondition \(\mathit{pre}\) on (input tape, work tapes, output tape), an input length \(n\) and a space bound \(s\), the space contract holds if, whenever \(M\) starts in its start state on tapes satisfying \(\mathit{pre}\), every configuration it reaches is within auxiliary space \(s\) for input length \(n\). The time-and-space contract with bounds \(b\), \(n\) and \(s\) is the conjunction of the time-bounded triple \(\{ \mathit{pre}\} \, M\, \{ \mathit{post}\} _b\) with this space contract.

(1) A configuration predicate that holds at every start satisfying \(\mathit{pre}\), is preserved by every step, and implies auxiliary space \(s\) gives the space contract. (2) If \(\{ \mathit{pre}\} \, M\, \{ \mathit{post}\} _b\) holds and every start configuration satisfying \(\mathit{pre}\) is within auxiliary space \(s_0\), then the time-and-space contract holds with space \(s_0 + b\). (3) A time-and-space contract survives strengthening the precondition, weakening the postcondition, and enlarging the time bound, the input length and the space bound. (4) The sequential composition of two transducers is a transducer. If \(M_1\) satisfies the time-and-space contract from \(\mathit{pre}\) to \(\mathit{mid}\) with bounds \(b_1, n, s_1\), the phase-boundary normalization maps tapes satisfying \(\mathit{mid}\) to tapes satisfying \(\mathit{mid}'\), and \(M_2\) satisfies the contract from \(\mathit{mid}'\) to \(\mathit{post}\) with bounds \(b_2, n, s_2\), then \(\mathrm{seq}(M_1, M_2)\) satisfies the contract from \(\mathit{pre}\) to \(\mathit{post}\) with bounds \(b_1 + 1 + b_2\), \(n\) and \(\max (s_1, s_2)\). (5) If a transducer satisfies, for every input \(x\), the time-and-space contract from the initial tapes on \(x\) to “the output tape holds \(f(x)\)” with bounds \(T(|x|)\), \(|x|\) and \(S(|x|)\), then it computes \(f\) in space \(S\).

Proof ▶

(1) is induction along the reachable configurations. For (2), the run halts within \(b\) steps, so every reachable configuration is at most \(b\) steps from its start, and each step moves each head by at most one cell. For (4), the time component is Theorem 17; the first phase is covered by the contract of \(M_1\), and the normalized start of \(M_2\) and everything after it by the contract of \(M_2\).

Theorem 234 FL is closed under composition

If \(f, g \in \mathsf{FL}\) then \(g \circ f \in \mathsf{FL}\). If moreover \(A \in \mathsf{L}\), then \(f^{-1}(A) \in \mathsf{L}\).

Proof ▶

The recomputation argument: simulate the machine for \(g\), keeping the position of its virtual input head in a binary counter, and whenever it reads bit \(j\) of \(f(x)\), rerun the machine for \(f\) from the start, discarding output until position \(j\). By \(\mathsf{FL} \subseteq \mathsf{FP}\), \(|f(x)|\) is polynomial, so the counters need \(O(\log n)\) bits. Under the library’s convention the virtual input head may travel one cell past \(f(x)\) for free and is charged beyond that.

Write \(A \le _{\log } B\) when some \(f \in \mathsf{FL}\) satisfies \(x \in A \iff f(x) \in B\). Then \(\le _{\log }\) is reflexive and transitive, \(A \le _{\log } B\) implies \(A \le _p B\), and \(\mathsf{L}\) and \(\mathsf{NL}\) are closed downward under \(\le _{\log }\).

Proof ▶

Transitivity and closure of \(\mathsf{L}\) are Theorem 234; closure of \(\mathsf{NL}\) is the same recomputation with a nondeterministic outer machine.

Theorem 236 PATH is NL-complete

Directed \(s\)–\(t\) reachability, under an adjacency-matrix codec, is in \(\mathsf{NL}\) and every language in \(\mathsf{NL}\) log-space reduces to it.

Proof ▶

The reduction writes out the configuration graph of the log-space machine on the given input, one adjacency bit at a time.

Definition 237 Log-space loop programs

A program has a fixed finite set of registers, each holding a natural number of at most \(c \log n\) bits for a program constant \(c\). Instructions are read-only access to the input bit at a register-held index, the input length, successor, comparison and addition on registers, conditionals, bounded loops whose bounds are register values, sequencing, and appending a bit to a write-only output stream.

Theorem 238 Compiling loop programs to log-space transducers

The function computed by every log-space loop program is in \(\mathsf{FL}\), and a program that outputs a single bit decides a language in \(\mathsf{L}\).

Proof ▶

Each register becomes a binary work tape, input access becomes a seek driven by a counter, and loops become binary counters, reusing the Hoare-style binary-counter subroutines and the space-aware contracts of Lemma 233. The counting machine of Theorem 216 and the tableau serializer would then be written as programs.