TM Combinators #
This file provides TM constructions for composing machines, used to prove closure properties of complexity classes.
Main definitions #
TM.unionTM— Giventm₁ : TM n₁decidingL₁andtm₂ : TM n₂decidingL₂, construct aTM (n₁ + 1 + n₂)that decidesL₁ ∪ L₂.TM.complementTM— Given a TM decidingL, construct a TM (with the same number of work tapes) decidingLᶜby flipping the output bit.TM.seqTM— Sequential composition: runtm₁to completion, thentm₂on the same tapes.TM.ifTM— Conditional branching: run a test machine, then branch to a "then" or "else" machine based on its output.TM.loopTM— Loop combinator: repeatedly run a body machine then a test machine, halting when the test outputsΓ.one.TM.scannerTM— Generic finite-state scanner: fold a finite-state transition function over the input bits and emit a final symbol.TM.retargetInput— GivenM : TM k, construct aTM (k + 1)that runsMbut reads its "input" from work tapekinstead of the input tape.
Design #
The union machine has three phases:
- Phase 1: Simulate
tm₁, redirecting its output to work tapen₁(a "fake output" tape). The real output tape stays pristine. - Transition: Rewind the fake output to cell 1 and check the result.
If
Γ.one(tm₁ accepted), writeΓ.oneto the real output and halt. Otherwise rewind the input tape and reset Phase-2 tapes to cell 0. - Phase 2: Simulate
tm₂using work tapesn₁+1..n₁+n₂and the real output tape.
Work tape layout (0-indexed) #
0 .. n₁-1— Phase 1's work tapes (mirrorstm₁.work)n₁— Phase 1's redirected output (mirrorstm₁.output)n₁+1 .. n₁+n₂— Phase 2's work tapes (mirrorstm₂.work)
State space #
Q₁ ⊕ UnionPhase ⊕ Q₂ where UnionPhase encodes the four transition states
between Phase 1 and Phase 2.
Direction for an idle tape: move right if reading ▷, else stay.
Satisfies δ_right_of_start for tapes not involved in the current phase.
Equations
Instances For
Direction for a tape we want to move left: move left unless reading ▷,
in which case move right to satisfy δ_right_of_start. During actual
execution the tape won't be at cell 0, so this always moves left.
Equations
Instances For
idleDir moves right when reading the start symbol ▷.
If the head reads ▷, then idleDir moves right — the shape of the
δ_right_of_start obligation for idle tapes.
If the head reads ▷, then moveLeftDir moves right — the shape of the
δ_right_of_start obligation for tapes being rewound.
Write back the same symbol read from a tape, preserving cell contents.
Maps ▷ to □ since Tape.write at position 0 is a no-op anyway.
Equations
Instances For
The "do nothing" transition output: all writes are □, all directions
are idleDir. Used for states that only change the control state.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Intermediate states between Phase 1 and Phase 2 of the union machine.
- rewindOut : UnionPhase
- checkResult : UnionPhase
- rewindIn : UnionPhase
- setup2 : UnionPhase
Instances For
Equations
- One or more equations did not get rendered due to their size.
The state type for the union TM.
Equations
- Complexity.TM.UnionQ Q₁ Q₂ = (Q₁ ⊕ Complexity.TM.UnionPhase ⊕ Q₂)
Instances For
Index of the fake output tape (work tape n₁).
Equations
- Complexity.TM.fakeOutIdx = ⟨n₁, ⋯⟩
Instances For
Construct a TM deciding L₁ ∪ L₂ from TMs deciding L₁ and L₂.
The composite machine has n₁ + 1 + n₂ work tapes:
0 .. n₁-1fortm₁'s work tapesn₁fortm₁'s redirected outputn₁+1 .. n₁+n₂fortm₂'s work tapes
Equations
- One or more equations did not get rendered due to their size.
Instances For
Intermediate states for the complement machine's output-flipping phase.
- rewind : ComplementPhase
- flip : ComplementPhase
- done : ComplementPhase
Instances For
Equations
- One or more equations did not get rendered due to their size.
The state type for the complement TM.
Equations
Instances For
Flip a readable symbol: 1 ↔ 0, blanks stay blank.
Equations
Instances For
Construct a TM deciding Lᶜ from a TM deciding L.
The complement machine has the same number of work tapes as the original. It runs in three stages:
- Simulate: Run the original TM. When it halts, transition to
rewind. - Rewind: Move the output head left to
▷(cell 0), then right to cell 1. - Flip: Read output cell 1, write the flipped bit, and halt.
Equations
- One or more equations did not get rendered due to their size.
Instances For
The state type for the conditional branching TM.
Equations
- Complexity.TM.IfQ QT QThen QElse = (QT ⊕ Complexity.TM.IfPhase ⊕ QThen ⊕ QElse)
Instances For
Conditional branching: run tmTest to completion, read its output at
cell 1, then run tmThen (if output = Γ.one) or tmElse (otherwise).
All three machines share the same n work tapes, input tape, and output
tape. Work tape contents are preserved across all transitions via
readBackWrite, maintaining shared state for the branch machines.
Phases #
- Test: Simulate
tmTest. When it halts, enter rewind. - Rewind output: Move output head left to
▷(cell 0). - Check: Move output head right to cell 1, read the test result.
If
Γ.one, entertmThen.qstart. Otherwise, entertmElse.qstart. - Branch: Simulate
tmThenortmElse. When the branch machine halts, transition to thedonehalt state.
Time #
t_test + (output_head_pos + 2) + 1 + t_branch + 1 where
output_head_pos ≤ t_test. Total: at most 2·t_test + t_branch + 4.
Equations
- One or more equations did not get rendered due to their size.
Instances For
The state type for the sequential composition TM.
Equations
- Complexity.TM.SeqQ Q₁ Q₂ = (Q₁ ⊕ Q₂)
Instances For
Sequential composition: run tm₁ to completion, then tm₂ on the same tapes.
Both machines share the same n work tapes, input tape, and output tape.
When tm₁ halts, there is one transition step that:
- Changes state from
Q₁toQ₂(enteringtm₂.qstart) - Preserves all tape cell contents (via
readBackWrite) - Moves any tape head at position 0 to position 1 (forced by
δ_right_of_start) - Leaves all other tape head positions unchanged
After the transition, tm₂ runs from the resulting tape state.
Total time: t₁ + 1 + t₂ where t₁ and t₂ are the run times of
tm₁ and tm₂ respectively.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
- One or more equations did not get rendered due to their size.
The state type for the loop TM.
Equations
- Complexity.TM.LoopQ QBody QTest = (QBody ⊕ Complexity.TM.LoopPhase ⊕ QTest)
Instances For
Loop combinator: repeatedly run tmBody then tmTest, halting when
the test's output at cell 1 is Γ.one.
Both machines share the same n work tapes, input tape, and output tape.
Work tape contents are preserved across transitions (via readBackWrite),
allowing the body to accumulate state across iterations.
Phases #
- Body: Simulate
tmBody. When it halts, transition to test. - Test: Simulate
tmTest. When it halts, enter rewind. - Rewind output: Move output head left to
▷(cell 0). - Check: Move right to cell 1, read the test result.
If
Γ.one, enterdone(halt). Otherwise, transition back to body.
Use case #
The UTM's main loop: loopTM simStepTM checkHaltTM runs one simulation
step, then checks if the simulated machine has halted.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Control states of a generic finite-state scanner parameterized by a
user-supplied scan-state type S. The machine has a one-time start
step that advances off cell 0, then a stream of scan s states holding
the current scan state, and finally a done halt state.
- start {S : Type} : ScannerPhase S
- scan {S : Type} (s : S) : ScannerPhase S
- done {S : Type} : ScannerPhase S
Instances For
Equations
- Complexity.TM.instDecidableEqScannerPhase Complexity.TM.ScannerPhase.start Complexity.TM.ScannerPhase.start = isTrue ⋯
- Complexity.TM.instDecidableEqScannerPhase Complexity.TM.ScannerPhase.start (Complexity.TM.ScannerPhase.scan s) = isFalse ⋯
- Complexity.TM.instDecidableEqScannerPhase Complexity.TM.ScannerPhase.start Complexity.TM.ScannerPhase.done = isFalse ⋯
- Complexity.TM.instDecidableEqScannerPhase (Complexity.TM.ScannerPhase.scan s) Complexity.TM.ScannerPhase.start = isFalse ⋯
- Complexity.TM.instDecidableEqScannerPhase (Complexity.TM.ScannerPhase.scan s) Complexity.TM.ScannerPhase.done = isFalse ⋯
- Complexity.TM.instDecidableEqScannerPhase Complexity.TM.ScannerPhase.done Complexity.TM.ScannerPhase.start = isFalse ⋯
- Complexity.TM.instDecidableEqScannerPhase Complexity.TM.ScannerPhase.done (Complexity.TM.ScannerPhase.scan s) = isFalse ⋯
- Complexity.TM.instDecidableEqScannerPhase Complexity.TM.ScannerPhase.done Complexity.TM.ScannerPhase.done = isTrue ⋯
- Complexity.TM.instDecidableEqScannerPhase (Complexity.TM.ScannerPhase.scan s₁) (Complexity.TM.ScannerPhase.scan s₂) = if h : s₁ = s₂ then isTrue ⋯ else isFalse ⋯
Equations
- One or more equations did not get rendered due to their size.
Generic finite-state scanner.
A 0-work-tape TM parameterized by a scan-state type S, an initial
state s₀, a transition function scanStep : S → Bool → S (called on
each input bit), and a finalizer finalOutput : S → Γw (the symbol to
emit when end-of-input is reached).
Semantics: runs left-to-right through the input, folding scanStep over
the bits starting from s₀; when a blank is reached, writes
finalOutput (finalState) to output cell 1 and halts.
This captures the common "read once, fold into a fixed-size state"
pattern used by evenLength, allZeros/allOnes, containsZero/containsOne,
lengthDivBy k, lastBit, and similar regular-language scanners.
Halts in |x| + 2 steps on every input (1 start + |x| scans + 1 halt).
Equations
- One or more equations did not get rendered due to their size.
Instances For
Given a DTM M : TM k, construct a DTM retargetInput M : TM (k + 1)
that behaves like M but reads its "input" from work tape k (the last
work tape) instead of the real input tape.
Tape layout:
- Real input tape: ignored (moved idly each step, never read).
- Work tapes
0..k-1: mirrorM's work tapes. - Work tape
k: plays the role ofM's input tape (read-only, no writes except a no-opreadBackWritethat preserves cells).
When work tape k is initialized with Tape.init (z.map Γ.ofBool), the
machine simulates M on input z.
Used in witnessLang NTM constructions where the verifier DTM's
"input" (e.g. pair(x, y)) is built on a work tape rather than
supplied on the real input tape.
Equations
- One or more equations did not get rendered due to their size.