Assembling a machine that guesses #
Complexitylib.Models.TuringMachine.GuessStream reduces building a nondeterministic machine to
building a deterministic one that satisfies TM.GuessProtocol on its last work tape. This file
supplies the parts.
TM.liftLast puts an ordinary machine to work on the first tapes and leaves the guess tape
untouched, so every existing subroutine can be used verbatim. TM.GuessProtocol is then closed
under TM.seqTM and TM.loopTM, which is what lets the guessing and the not-guessing be
assembled into one machine: all three of its conditions are pointwise properties of the transition
function, and the combinators' own steps preserve every tape they are not driving.
Main definitions #
TM.liftLast— run a machine on the first work tapes, holding the last one stillTM.guessReadTM— copy the guess cell onto a work tape and advance the guess headTM.guessWriteTM— the same, advancing the target head too, so a block of guesses can be writtenTM.guessBlockTM— a fixed number of those in sequenceTM.guessBlocksTM— and a block written into each of several registersTM.guessThenTM— guess some blocks, then run a guess-free machine on themTM.guessStageTM— and the stage every construction is built from: guess, then rewind the guessed registers so a scan can read themTM.guessWriteTapes,TM.guessBlockTapes— the tapes one guess-write, and a block of them, leave behind
Main results #
TM.guessProtocol_liftLast— a lifted machine never consults the guess tapeTM.liftLast_reachesIn— and its runs are the original'sTM.lift4,TM.lift4_hoareTime— the same, four tapes at a timeTM.guessProtocol_seqTM,TM.guessProtocol_loopTM— the protocol survives compositionTM.guessProtocol_guessReadTM,TM.guessReadTM_stepCfg,TM.guessProtocol_guessWriteTM,TM.guessWriteTM_stepCfg— what the primitives doTM.guessProtocol_guessBlockTM,TM.guessProtocol_guessBlocksTM,TM.guessProtocol_guessThenTM— and these respect the protocolTM.skipTM_hoareTime',TM.guessBlocksTM_hoareTime,TM.guessThenTM_hoareTime— and their contractsTM.guessWriteTM_hoareTime,TM.guessBlockTM_hoareTime— their contractsTM.guessWriteTapes_last,TM.guessWriteTapes_target,TM.guessWriteTapes_other— what one guess-write does to each tape, off the left markerTM.guessWriteTapes_target_head,TM.guessWriteTapes_target_cells,TM.guessWriteTapes_target_cells_ne— where the guessed bit landsTM.guessWriteTapes_startInvariant,TM.guessWriteTapes_head_pos— and that the invariants survive itTM.GuessFrom,TM.guessFrom_after— what the guess tape still holds, and that a stage consumes a prefix and leaves the restTM.guessBlocksTapes_spec— what several blocks leave behind, when the registers are distinctTM.guessList,TM.guessList_getElem— the guess stream that realizes a family of blocksTM.StageBlocks,TM.exists_stageBlocks,TM.blocks_of_stageBlocks— and one that feeds every stage of a loop, with each stage reading its own blocks off itTM.guessBlockTapes_spec— what a whole block leaves behind: both heads advanced by the number of bits, those bits on the target, nothing else touched
Lifting a machine past the guess tape #
A lifted machine never consults the guess tape, so it advances nowhere.
A Hoare triple for a lifted machine. Everything the original guarantees still holds, and the guess tape comes out exactly as it went in.
A lifted-by-four machine never consults the last tape.
A lifted-by-four machine's contract. Four fresh tapes come through untouched.
Lift a machine past r fresh tapes, holding them still. The original tapes
keep their indices — Fin.castAdd r — and the fresh ones are appended.
TM.liftTM in Models/TuringMachine/Lift.lean widens a machine the same way,
but its interface carries blank extra tapes, for lifting whole decision
procedures. This one is the iterated liftLast, so its Hoare rule below carries
arbitrary extra tapes through untouched — what an assembled subroutine needs.
Instances For
A padded machine's contract. The r fresh tapes come through
untouched, and the original tapes keep their meaning under Fin.castAdd.
A lifted machine keeps the one-way-output discipline.
A padded machine keeps the one-way-output discipline.
The protocol survives composition #
The guess protocol survives sequential composition.
The advancing states of a loop: each part's own, with the combinator's own control phases and handoff steps excluded.
Equations
- One or more equations did not get rendered due to their size.
Instances For
The guess protocol survives looping.
Reading a guess #
Control states of TM.guessReadTM.
- read : GuessPhase
Copy the guess cell onto the target tape.
- done : GuessPhase
Halt.
Instances For
Equations
- Complexity.TM.instFintypeGuessPhase = { elems := {Complexity.TM.GuessPhase.read, Complexity.TM.GuessPhase.done}, complete := ⋯ }
The advancing state of the guess primitives: only read consumes a guess bit.
Stated on GuessPhase itself so instance search never has to see through a
machine's Q projection.
Equations
Instances For
The guess-reading primitive. In one step it copies the symbol under the guess head onto
work tape j, advances the guess head, and halts. This is the only place a machine assembled
with TM.liftLast ever consults the guess tape.
Equations
- One or more equations did not get rendered due to their size.
Instances For
The guess-reading primitive advances exactly in its one working state.
One step of the guess-reading primitive: the guess symbol lands on tape j, the guess head
advances, and every other tape is left where it was.
The guess-writing primitive. Like TM.guessReadTM, but it also advances the target head,
so that repeating it writes a block of guessed bits.
Equations
- One or more equations did not get rendered due to their size.
Instances For
The guess-writing primitive advances exactly in its one working state.
One step of the guess-writing primitive: the guess symbol lands on tape j, which then
advances, the guess head advances, and every other tape is left where it was.
The tapes after one guess-write: the target takes the guessed symbol and advances, the guess tape advances, and every other tape passes through the phase transition unchanged.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Writing a block of guesses #
A parameter block is a fixed number of bits — fixed because it holds a state, a choice bit and one symbol per head, none of which grows with the input — so the machine that writes it can be built by recursion on that number rather than by a counted loop.
Write n + 1 guessed bits onto work tape j, advancing it.
Equations
Instances For
Its advancing states: every state of every stage.
Equations
Instances For
The tapes a block of guess-writes leaves behind. Each stage writes, then the composition's
own handoff step passes every tape through TM.transitionTape.
Equations
- Complexity.TM.guessBlockTapes j 0 x✝ = Complexity.TM.guessWriteTapes j x✝
- Complexity.TM.guessBlockTapes j n.succ x✝ = Complexity.TM.guessBlockTapes j n fun (i : Fin (k + 1)) => Complexity.TM.transitionTape (Complexity.TM.guessWriteTapes j x✝ i)
Instances For
The input tape a block of guess-writes leaves behind.
Equations
Instances For
The output tape a block of guess-writes leaves behind.
Equations
Instances For
What a block of guesses actually leaves behind #
On tapes whose heads are off the left marker — which is how every stage of an assembled machine runs — the phase transitions are the identity, and the description collapses.
A block of guess-writes preserves the left-marker invariant.
What a block of guess-writes leaves behind. The guess tape and the target have both advanced by the number of bits written, the target's cells hold those bits, its earlier cells and every other tape are untouched.
A block of guesses respects the protocol.
Several blocks of guesses #
A configuration is spread over several registers, so guessing one means writing a block into each. The registers are addressed by index, so no placement is involved.
The do-nothing machine never consults the guess tape.
A block of guess-writes leaves every head off the left marker.
Where block p's guesses sit on the guess tape: each block consumes one cell per bit plus
one for the block's final advance.
Equations
- Complexity.TM.guessOffset w 0 = 0
- Complexity.TM.guessOffset w r.succ = Complexity.TM.guessOffset w r + (w r + 1)
Instances For
Offsets grow with the number of blocks.
The bits of a family of blocks, laid end to end: this is the guess stream that makes each
register of TM.guessBlocksTM hold what the caller wants it to hold.
Equations
- Complexity.TM.guessList w b 0 = []
- Complexity.TM.guessList w b r.succ = Complexity.TM.guessList w b r ++ List.ofFn fun (q : Fin (w r + 1)) => b r ↑q
Instances For
Write a block of guesses into each of t registers in turn.
Equations
- Complexity.TM.guessBlocksTM j w 0 = Complexity.TM.skipTM
- Complexity.TM.guessBlocksTM j w r.succ = (Complexity.TM.guessBlocksTM j w r).seqTM (Complexity.TM.guessBlockTM (j r) (w r))
Instances For
Its advancing states.
Equations
- Complexity.TM.guessBlocksAdv j w 0 = fun (x : (Complexity.TM.guessBlocksTM j w 0).Q) => false
- Complexity.TM.guessBlocksAdv j w r.succ = Complexity.TM.seqAdv (Complexity.TM.guessBlocksAdv j w r) (Complexity.TM.guessBlockAdv (j r) (w r))
Instances For
The tapes several blocks of guesses leave behind.
Equations
- Complexity.TM.guessBlocksTapes j w 0 x✝ = fun (i : Fin (k + 1)) => Complexity.TM.transitionTape (x✝ i)
- Complexity.TM.guessBlocksTapes j w n.succ x✝ = Complexity.TM.guessBlockTapes (j n) (w n) fun (i : Fin (k + 1)) => Complexity.TM.transitionTape (Complexity.TM.guessBlocksTapes j w n x✝ i)
Instances For
What several blocks of guesses leave behind. When the target registers are distinct, each one ends up holding its own block of guessed bits, read off the guess tape at that block's offset — which is what lets a stage guess a whole structured object at once.
The input tape they leave behind.
Equations
Instances For
The output tape they leave behind.
Equations
Instances For
How long several blocks of guesses take.
Equations
- Complexity.TM.guessBlocksTime w 0 = 1
- Complexity.TM.guessBlocksTime w r.succ = Complexity.TM.guessBlocksTime w r + 1 + (2 * w r + 1)
Instances For
The contract of several blocks of guesses.
Several blocks of guesses respect the protocol.
Guess, then check. Every stage of a guess-and-verify machine has this shape: write some blocks of guesses onto the registers, then run a guess-free machine on them.
Equations
- Complexity.TM.guessThenTM j w t D = (Complexity.TM.guessBlocksTM j w t).seqTM D.liftLast
Instances For
A guess-and-check stage respects the protocol.
The contract of a guess-and-check stage. The caller says what the guessed tapes give the checking machine; this composes the two halves.
What the guess tape still holds #
A stage consumes a prefix of the guess tape and leaves the rest for the stages after it. Stating that as a predicate on the tape lets a loop carry it as an invariant: each iteration shifts the stream by the bits it used.
The guess tape carries the bits of g from its head onward.
Equations
- Complexity.TM.GuessFrom g t = ∀ (q : ℕ), t.cells (t.head + q) = Complexity.Γ.ofBool (g q)
Instances For
A loaded guess tape carries its stream.
A stage consumes a prefix and leaves the rest.
A stream that feeds every stage #
A machine that guesses in a loop consumes one stage's worth of bits per pass. Saying what the
whole guess tape must contain is then a statement about a doubly-indexed family: stage s, block
p, bit q. Such a stream always exists — the offsets of distinct stages and blocks never
collide.
The stream gives stage s its block p's bit q.
Equations
- Complexity.TM.StageBlocks w t b g = ∀ (s p q : ℕ), p < t → q ≤ w p → g (s * Complexity.TM.guessOffset w t + (Complexity.TM.guessOffset w p + q)) = b s p q
Instances For
A stage reads its own blocks off the stream. This is the hypothesis a guess stage's contract asks for, supplied by the loop invariant's guess-tape clause.
A whole guess-and-rewind stage #
A guess stage. Write a block of guesses onto each of t registers, then bring the named
registers' heads back to cell one — the form every scan expects to read. The input head is left
alone: a machine simulating another one keeps its input head where the simulation put it.
Equations
- Complexity.TM.guessStageTM j w t targets = Complexity.TM.guessThenTM j w t (Complexity.TM.parkRewindWorkTM targets)
Instances For
A guess stage respects the protocol.
The contract of a guess stage.