# PvsNP/Machine/Defs.lean @ main

````
import PvsNP.Basic.Finite
import PvsNP.Machine.Tape

/-!
# Multi-tape Turing machines

A `Machine α k` is a deterministic Turing machine with `k + 1` tapes over the tape alphabet `Option α`
(the input symbols and the blank). Tape `0` holds the input. Its control is a *finite* type of states
`Q`; in each step it reads the symbol under every head, and the transition function `δ` gives the next
state, a symbol to write on every tape and a move for every head.

A machine stops in the states marked `halted`; `accepting` says which of those accept. Running a halted
configuration leaves it unchanged, so "halted after `t` steps" means "halted within `t` steps".

Design notes:
- The tape alphabet is `Option α`: machines have no extra work symbols. For `|α| ≥ 2` this changes
  running times only polynomially; it keeps machines over one alphabet composable without re-encoding
  tapes.
- Finiteness of `Q` is part of the structure (`finite`). Without it a state could store the whole input
  and the model would decide anything in one step.
-/

namespace PvsNP

/-- A deterministic Turing machine with `k + 1` tapes over the tape alphabet `Option α`. -/
structure Machine (α : Type) (k : Nat) where
  /-- The states. -/
  Q : Type
  /-- There are finitely many states. -/
  finite : IsFinite Q
  /-- The start state. -/
  start : Q
  /-- The states in which the machine stops. -/
  halted : Q → Bool
  /-- The halted states that accept. -/
  accepting : Q → Bool
  /-- The transition: from a state and the symbols under the heads, the next state, the symbols to
  write and the head moves. -/
  δ : Q → (Fin (k + 1) → Option α) → Q × (Fin (k + 1) → Option α) × (Fin (k + 1) → Dir)

/-- A configuration: a state and the contents of every tape. -/
structure Cfg (α : Type) (k : Nat) (Q : Type) where
  state : Q
  tapes : Fin (k + 1) → Tape α

namespace Machine

variable {α : Type} {k : Nat}

/-- Apply one transition to every tape at once: write, then move. -/
def apply (tapes : Fin (k + 1) → Tape α) (w : Fin (k + 1) → Option α) (d : Fin (k + 1) → Dir) :
    Fin (k + 1) → Tape α :=
  fun i => ((tapes i).write (w i)).move (d i)

/-- The symbols under the heads. -/
def reads (tapes : Fin (k + 1) → Tape α) : Fin (k + 1) → Option α :=
  fun i => (tapes i).read

/-- One step. A halted configuration does not move. -/
def step (M : Machine α k) (c : Cfg α k M.Q) : Cfg α k M.Q :=
  if M.halted c.state then c
  else
    let o := M.δ c.state (reads c.tapes)
    ⟨o.1, apply c.tapes o.2.1 o.2.2⟩

/-- `t` steps. -/
def run (M : Machine α k) (c : Cfg α k M.Q) : Nat → Cfg α k M.Q
  | 0 => c
  | t + 1 => M.run (M.step c) t

/-- The configuration in which a machine starts, given its tapes. -/
def init (M : Machine α k) (tapes : Fin (k + 1) → Tape α) : Cfg α k M.Q := ⟨M.start, tapes⟩

variable (M : Machine α k)

@[simp] theorem run_zero (c : Cfg α k M.Q) : M.run c 0 = c := rfl

theorem run_succ (c : Cfg α k M.Q) (t : Nat) : M.run c (t + 1) = M.run (M.step c) t := rfl

theorem run_succ' (c : Cfg α k M.Q) (t : Nat) : M.run c (t + 1) = M.step (M.run c t) := by
  induction t generalizing c with
  | zero => rfl
  | succ t ih => rw [run_succ, ih, ← run_succ]

theorem run_add (c : Cfg α k M.Q) (s t : Nat) : M.run c (s + t) = M.run (M.run c s) t := by
  induction s generalizing c with
  | zero => simp
  | succ s ih => rw [Nat.succ_add, run_succ, ih, run_succ]

theorem step_of_halted {c : Cfg α k M.Q} (h : M.halted c.state = true) : M.step c = c := by
  simp [step, h]

theorem run_of_halted {c : Cfg α k M.Q} (h : M.halted c.state = true) (t : Nat) : M.run c t = c := by
  induction t with
  | zero => rfl
  | succ t ih => rw [run_succ', ih, step_of_halted M h]

/-- Once halted, a machine stays put: running longer changes nothing. -/
theorem run_stable {c : Cfg α k M.Q} {s t : Nat} (h : M.halted (M.run c s).state = true)
    (hst : s ≤ t) : M.run c t = M.run c s := by
  obtain ⟨u, rfl⟩ := Nat.exists_eq_add_of_le hst
  rw [run_add, run_of_halted M h]

/-- The same machine with other accepting states. -/
def withAccepting (a : M.Q → Bool) : Machine α k := { M with accepting := a }

/-- Acceptance plays no part in running. -/
theorem run_withAccepting (a : M.Q → Bool) (c : Cfg α k M.Q) (t : Nat) :
    (M.withAccepting a).run c t = M.run c t := by
  induction t generalizing c with
  | zero => rfl
  | succ t ih => exact ih (M.step c)

end Machine

/-! ## Initial tapes -/

/-- Input `x` on tape `0`, every other tape blank, every head at `0`. -/
def inputTapes {α : Type} {k : Nat} (x : List α) : Fin (k + 1) → Tape α :=
  fun i => if i.val = 0 then Tape.ofWord x else Tape.blank

/-- Input `x` on tape `0`, certificate `w` on tape `1` (when there is one), the rest blank. -/
def verifierTapes {α : Type} {k : Nat} (x w : List α) : Fin (k + 1) → Tape α :=
  fun i => if i.val = 0 then Tape.ofWord x else if i.val = 1 then Tape.ofWord w else Tape.blank

theorem verifierTapes_nil {α : Type} {k : Nat} (x : List α) :
    (verifierTapes x [] : Fin (k + 1) → Tape α) = inputTapes x := by
  funext i
  simp only [verifierTapes, inputTapes, Tape.ofWord_nil, ite_self]

end PvsNP

````

API: `https://git.neoopensource.com/v1/repos/pvsnp/blob?rev=main&path=PvsNP%2FMachine%2FDefs.lean`
