Turing machines #
This file defines the library's base model of computation: multi-tape deterministic and nondeterministic Turing machines over a fixed four-symbol alphabet, with named input/work/output tapes. The model's shape follows Arora–Barak (Computational Complexity: A Modern Approach, Definitions 1.1–1.4 and 2.1), with the conventions below enforced structurally.
Main definitions #
Γ— the tape alphabet{0, 1, □, ▷}(read alphabet)Γw— the writable alphabet{0, 1, □}(write alphabet;▷cannot be written)Dir3— three-way tape head direction (left, right, stay)Tape— a one-sided infinite tape; cell 0 is leftmost and permanently▷Cfg— a machine configuration with named tapes (input, work, output)TM— a deterministic multi-tape Turing machine (AB Definition 1.1)NTM— a nondeterministic TM with two transition functions (AB Definition 2.1)TM.stepRel,TM.reaches,TM.reachesIn— deterministic step relation and reachabilityNTM.trace— execute an NTM for a fixed choice sequence (canonical NTM execution)Tape.HasOutput— predicate: tape contains a given binary string as outputTM.ComputesInTime— computing a function in bounded time (AB Definition 1.4)TM.Computes— computing a function (existential over time bound)TM.Accepts,TM.AcceptsInTime— deterministic acceptanceNTM.Accepts,NTM.AcceptsInTime— nondeterministic acceptance (existential)Cfg.WithinAuxSpace,Cfg.WithinDecisionSpace— honest tape-space boundsTM.DecidesInTime,NTM.DecidesInTime— deciding a language within a time boundTM.DecidesInTimeSpace— deciding with simultaneous time and space boundsNTM.acceptCount,NTM.acceptProb— counting/probabilistic acceptanceTM.toNTM— embed a DTM into an NTM
Design notes #
- One-sided tapes:
Tapeuseshead : ℕandcells : ℕ → Γ. Cell 0 is leftmost; moving left at position 0 is a no-op (Natsubtraction saturates). - Immutable cell 0:
Tape.writeis a no-op when the head is at position 0, ensuring▷at cell 0 is permanent. Combined withΓw(which excludes▷), this guarantees▷appears only at cell 0 on every tape. - Read vs write alphabet: The transition function reads
Γ = {0, 1, □, ▷}but writesΓw = {0, 1, □}, soδstructurally cannot write▷. - Finite state:
Qcarries[Fintype Q]; the state space is finite. - Output: Read from cell 1 of the output tape (first cell after
▷); machine output is the binary string written after▷. - Named tapes:
Cfghasinput,work,outputfields rather thanFin k → Tape, making the read-only/read-write distinction structural. - NTM execution: Defined via
trace(a fixed choice sequence), not a relational step.
Equations
- Complexity.instReprΓ = { reprPrec := Complexity.instReprΓ.repr }
Equations
- Complexity.instReprΓ.repr Complexity.Γ.zero prec✝ = Repr.addAppParen (Std.Format.nest (if prec✝ ≥ 1024 then 1 else 2) (Std.Format.text "Complexity.Γ.zero")).group prec✝
- Complexity.instReprΓ.repr Complexity.Γ.one prec✝ = Repr.addAppParen (Std.Format.nest (if prec✝ ≥ 1024 then 1 else 2) (Std.Format.text "Complexity.Γ.one")).group prec✝
- Complexity.instReprΓ.repr Complexity.Γ.blank prec✝ = Repr.addAppParen (Std.Format.nest (if prec✝ ≥ 1024 then 1 else 2) (Std.Format.text "Complexity.Γ.blank")).group prec✝
- Complexity.instReprΓ.repr Complexity.Γ.start prec✝ = Repr.addAppParen (Std.Format.nest (if prec✝ ≥ 1024 then 1 else 2) (Std.Format.text "Complexity.Γ.start")).group prec✝
Instances For
Equations
Equations
- Complexity.instInhabitedΓ = { default := Complexity.Γ.blank }
Equations
- Complexity.instReprΓw = { reprPrec := Complexity.instReprΓw.repr }
Equations
- Complexity.instReprΓw.repr Complexity.Γw.zero prec✝ = Repr.addAppParen (Std.Format.nest (if prec✝ ≥ 1024 then 1 else 2) (Std.Format.text "Complexity.Γw.zero")).group prec✝
- Complexity.instReprΓw.repr Complexity.Γw.one prec✝ = Repr.addAppParen (Std.Format.nest (if prec✝ ≥ 1024 then 1 else 2) (Std.Format.text "Complexity.Γw.one")).group prec✝
- Complexity.instReprΓw.repr Complexity.Γw.blank prec✝ = Repr.addAppParen (Std.Format.nest (if prec✝ ≥ 1024 then 1 else 2) (Std.Format.text "Complexity.Γw.blank")).group prec✝
Instances For
Equations
Embed a writable symbol into the full alphabet.
Equations
Instances For
Equations
- Complexity.instCoeΓwΓ = { coe := Complexity.Γw.toΓ }
Convert a boolean to an alphabet symbol.
Instances For
Convert a boolean to a writable symbol.
Equations
Instances For
Equations
- Complexity.instReprDir3.repr Complexity.Dir3.left prec✝ = Repr.addAppParen (Std.Format.nest (if prec✝ ≥ 1024 then 1 else 2) (Std.Format.text "Complexity.Dir3.left")).group prec✝
- Complexity.instReprDir3.repr Complexity.Dir3.right prec✝ = Repr.addAppParen (Std.Format.nest (if prec✝ ≥ 1024 then 1 else 2) (Std.Format.text "Complexity.Dir3.right")).group prec✝
- Complexity.instReprDir3.repr Complexity.Dir3.stay prec✝ = Repr.addAppParen (Std.Format.nest (if prec✝ ≥ 1024 then 1 else 2) (Std.Format.text "Complexity.Dir3.stay")).group prec✝
Instances For
Equations
- Complexity.instReprDir3 = { reprPrec := Complexity.instReprDir3.repr }
Equations
A one-sided infinite tape. Cell 0 is the leftmost cell and permanently
contains ▷. The head cannot move left of cell 0 (moving left at position 0
is a no-op via Nat subtraction). Writing at cell 0 is a no-op,
preserving ▷.
- head : ℕ
The head position; cell 0 is the leftmost cell.
The tape contents, one symbol per cell.
Instances For
Move the head according to a three-way direction.
Moving left at position 0 stays at 0 (Nat subtraction saturates).
Equations
Instances For
Write a symbol and move in one step.
Equations
- t.writeAndMove s d = (t.write s).move d
Instances For
t.StartInvariant says the left-end marker ▷ sits at cell 0 and
nowhere else — the standing shape of every tape reachable from an
initial configuration, since writes exclude ▷ and cell 0 is
immutable.
Equations
- t.StartInvariant = (t.cells 0 = Complexity.Γ.start ∧ ∀ (j : ℕ), 1 ≤ j → t.cells j ≠ Complexity.Γ.start)
Instances For
Under the invariant, a head at position ≥ 1 never reads ▷.
Writing (any Γw symbol) preserves the invariant.
Moving preserves the invariant (cells unchanged).
Writing a Γw symbol and moving preserves the invariant.
One write-and-move step advances the head by at most one.
Initialize a tape: ▷ at cell 0, contents at cells 1, 2, ..., □ elsewhere.
Head starts at position 0 (on ▷).
Equations
Instances For
Moving an empty initialized tape to cell one reads blank.
An initialized tape whose contents avoid ▷ satisfies the invariant.
A Boolean-initialized tape satisfies the invariant.
The empty initialized tape satisfies the invariant.
A language is a set of binary strings.
Equations
Instances For
A configuration is halted when its state equals the halt state.
Equations
- Complexity.Cfg.isHalted qhalt c = (c.state = qhalt)
Instances For
The auxiliary-space bound for a configuration on an input of length
inputLength. Work-tape cells through space are available, while the
input itself and its first trailing blank are free; travel farther into the
input tape's blank tail is charged against space.
This predicate deliberately omits the output tape. It is suitable for
function computation only when the machine also satisfies the one-way
output discipline TM.IsTransducer.
Equations
Instances For
The space bound for a language-decider configuration. In addition to
WithinAuxSpace, the output head is bounded by space + 1: cell 1 is the
free verdict cell, and any farther two-way output-tape travel is charged.
Equations
- c.WithinDecisionSpace inputLength space = (c.WithinAuxSpace inputLength space ∧ c.output.head ≤ space + 1)
Instances For
A deterministic Turing machine with n work tapes.
The machine has a read-only input tape, n read-write work tapes, and a read-write
output tape. The transition function reads Γ from all tape heads but writes only
Γw (excluding ▷) to work and output tapes. Q is finite.
- Q : Type
The (finite) type of machine states.
- decEq : DecidableEq self.Q
- qstart : self.Q
The designated start state.
- qhalt : self.Q
The designated halt state.
The transition function: from the current state and the symbols under the input, work, and output heads, produce the next state, the symbols to write on the work and output tapes, and a direction for every head.
- δ_right_of_start (q : self.Q) (iHead : Γ) (wHeads : Fin n → Γ) (oHead : Γ) : match self.δ q iHead wHeads oHead with | (fst, fst_1, fst_2, inDir, workDirs, outDir) => (iHead = Γ.start → inDir = Dir3.right) ∧ (∀ (i : Fin n), wHeads i = Γ.start → workDirs i = Dir3.right) ∧ (oHead = Γ.start → outDir = Dir3.right)
Reading the left-end marker forces that head to move right, so no head ever falls off the left edge.
Instances For
A nondeterministic Turing machine with two transition functions.
The same structure is used for probabilistic TMs — only the acceptance
criterion differs (existential for NTM, counting for PTM). Q is finite.
- Q : Type
The (finite) type of machine states.
- decEq : DecidableEq self.Q
- qstart : self.Q
The designated start state.
- qhalt : self.Q
The designated halt state.
- δ_right_of_start (b : Bool) (q : self.Q) (iHead : Γ) (wHeads : Fin n → Γ) (oHead : Γ) : match self.δ b q iHead wHeads oHead with | (fst, fst_1, fst_2, inDir, workDirs, outDir) => (iHead = Γ.start → inDir = Dir3.right) ∧ (∀ (i : Fin n), wHeads i = Γ.start → workDirs i = Dir3.right) ∧ (oHead = Γ.start → outDir = Dir3.right)
Reading the left-end marker forces that head to move right, on both branches.
Instances For
DTM computes function f in time T(n):
for every input x, the machine halts within T(|x|) steps with f(x)
written on the output tape.
Equations
Instances For
DTM computes function f (existential version of ComputesInTime).
Equations
- tm.Computes f = ∃ (T : ℕ → ℕ), tm.ComputesInTime f T
Instances For
DTM decides L using at most S(|x|) auxiliary space. Every reachable
configuration has work heads at position at most S(|x|), input head at
position at most |x| + S(|x|) + 1, and output head at position at most
S(|x|) + 1. Thus the input region, its first trailing blank, and output
verdict cell 1 are free, but neither infinite tape can become uncharged
two-way workspace. The machine halts on all inputs with correct output.
Equations
- One or more equations did not get rendered due to their size.
Instances For
DTM decides L within time T(|x|) and space S(|x|) simultaneously:
a single machine halts in bounded time with correct output, and every
reachable configuration satisfies the honest auxiliary-space convention of
Cfg.WithinDecisionSpace.
Equations
- One or more equations did not get rendered due to their size.
Instances For
The output tape head never moves left — the machine is a transducer.
This prevents earlier output from being reread as workspace while allowing
unbounded output length. ComputesInSpace requires this discipline; the
decision-space predicates separately bound two-way output-head travel.
Equations
- One or more equations did not get rendered due to their size.
Instances For
DTM computes function f using at most S(|x|) auxiliary space.
Work-tape travel and input-head travel beyond the input's first trailing
blank are bounded by Cfg.WithinAuxSpace. The output length is not bounded:
instead, IsTransducer makes the output one-way so it cannot serve as
read-write workspace.
Equations
- One or more equations did not get rendered due to their size.
Instances For
AcceptsInTime implies Accepts — forget the time bound.
DTM acceptance is monotone in the time bound.
DTM decision is monotone under pointwise enlargement of the time bound.
DTM computation is monotone under pointwise enlargement of the time bound.
Execute an NTM for T steps with a fixed choice sequence.
Stops early if the machine reaches qhalt.
Instances For
NTM accepts x: there exists a time bound and choice sequence leading to
qhalt with output cell 1 = 1.
Equations
Instances For
NTM accepts x within T steps: there exists a choice sequence of length T
leading to qhalt with output cell 1 = 1.
Equations
Instances For
AcceptsInTime implies Accepts — package the time bound existentially.
Accepts is exactly ∃ T, AcceptsInTime x T.
Running the NTM trace for more steps preserves the final configuration:
if the machine halts within T steps and the extended choice sequence
agrees with the original on the first T positions, the extra steps are
no-ops.
NTM acceptance is monotone in the time bound: AcceptsInTime x T implies
AcceptsInTime x T' for any T' ≥ T. Extra steps are no-ops once halted.
All computation paths of the NTM halt within T(|x|) steps, for every
input x and every choice sequence. This is the core time-boundedness
condition shared by DecidesInTime, BPTIME, and NTM.IsPPT.
Equations
Instances For
NTM decides L within time bound T(n):
all computation paths halt within T(|x|) steps, and accepting paths exist
iff x ∈ L (AB Definition 2.1).
Equations
- tm.DecidesInTime L T = (tm.AllPathsHaltIn T ∧ ∀ (x : List Bool), x ∈ L ↔ tm.AcceptsInTime x (T x.length))
Instances For
All-paths halting is monotone in the time bound: once halted, the extra steps are no-ops.
With all paths halting within T, timed acceptance transfers DOWN from any
pointwise-larger bound: by T(|x|) every path is already frozen.
Deciding within T transfers to any pointwise-larger bound T': halting
is monotone, and acceptance transfers both ways (up by monotonicity, down
by the all-paths-halt freeze).
Count of accepting choice sequences of length T.
Meaningful when the machine halts on all paths within T steps — use in conjunction
with NTM.DecidesInTime or an explicit all-paths-halt hypothesis.
Equations
Instances For
Acceptance probability = |accepting paths| / 2^T.
Meaningful when the machine halts on all paths within T steps.
Equations
- tm.acceptProb x T = ↑(tm.acceptCount x T) / 2 ^ T
Instances For
Count of choice sequences of length T on which the machine halts with
output y (using Tape.HasOutput).
Meaningful when the machine halts on all paths within T steps.
Equations
Instances For
Probability that the machine outputs y on input x =
|output-matching paths| / 2^T.
Meaningful when the machine halts on all paths within T steps.
This generalizes acceptProb from accept/reject to arbitrary output
strings, as needed for cryptographic definitions.
Equations
- tm.outputProb x T y = ↑(tm.outputCount x T y) / 2 ^ T
Instances For
NTM decides L using at most S(|x|) auxiliary space. Every intermediate
configuration on every path obeys Cfg.WithinDecisionSpace: work heads are
bounded by S, the finite input plus first blank is free but farther input
travel is charged, and only output verdict cell 1 is free. There exists a
time bound within which all paths halt and decide correctly.
Equations
- One or more equations did not get rendered due to their size.
Instances For
The output tape head never moves left — the machine is a transducer. This prevents earlier output from being reread as workspace while allowing unbounded output length. Decision-space predicates separately bound two-way output-head travel.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Embed a DTM into an NTM by using the same transition for both choices.