{"ok":true,"data":{"commit":"68ee862ecf840b5859cdbb47a9a45c1bbaebcb39","path":"PvsNP/Machine/Defs.lean","oid":"1c75b6b3b2a349ce6b5ac98bc4c4967ccd93d1cb","size":5334,"range_start":0,"range_end":5334,"truncated":false,"binary":false,"encoding":"utf8","content":"import PvsNP.Basic.Finite\nimport PvsNP.Machine.Tape\n\n/-!\n# Multi-tape Turing machines\n\nA `Machine α k` is a deterministic Turing machine with `k + 1` tapes over the tape alphabet `Option α`\n(the input symbols and the blank). Tape `0` holds the input. Its control is a *finite* type of states\n`Q`; in each step it reads the symbol under every head, and the transition function `δ` gives the next\nstate, a symbol to write on every tape and a move for every head.\n\nA machine stops in the states marked `halted`; `accepting` says which of those accept. Running a halted\nconfiguration leaves it unchanged, so \"halted after `t` steps\" means \"halted within `t` steps\".\n\nDesign notes:\n- The tape alphabet is `Option α`: machines have no extra work symbols. For `|α| ≥ 2` this changes\n  running times only polynomially; it keeps machines over one alphabet composable without re-encoding\n  tapes.\n- Finiteness of `Q` is part of the structure (`finite`). Without it a machine could scan its input\n  once, accumulating it in the state, and then apply an arbitrary predicate: every language would be\n  decidable in linear time. The alphabet's finiteness is required by the complexity classes.\n-/\n\nnamespace PvsNP\n\n/-- A deterministic Turing machine with `k + 1` tapes over the tape alphabet `Option α`. -/\nstructure Machine (α : Type) (k : Nat) where\n  /-- The states. -/\n  Q : Type\n  /-- There are finitely many states. -/\n  finite : IsFinite Q\n  /-- The start state. -/\n  start : Q\n  /-- The states in which the machine stops. -/\n  halted : Q → Bool\n  /-- The halted states that accept. -/\n  accepting : Q → Bool\n  /-- The transition: from a state and the symbols under the heads, the next state, the symbols to\n  write and the head moves. -/\n  δ : Q → (Fin (k + 1) → Option α) → Q × (Fin (k + 1) → Option α) × (Fin (k + 1) → Dir)\n\n/-- A configuration: a state and the contents of every tape. -/\nstructure Cfg (α : Type) (k : Nat) (Q : Type) where\n  state : Q\n  tapes : Fin (k + 1) → Tape α\n\nnamespace Machine\n\nvariable {α : Type} {k : Nat}\n\n/-- Apply one transition to every tape at once: write, then move. -/\ndef apply (tapes : Fin (k + 1) → Tape α) (w : Fin (k + 1) → Option α) (d : Fin (k + 1) → Dir) :\n    Fin (k + 1) → Tape α :=\n  fun i => ((tapes i).write (w i)).move (d i)\n\n/-- The symbols under the heads. -/\ndef reads (tapes : Fin (k + 1) → Tape α) : Fin (k + 1) → Option α :=\n  fun i => (tapes i).read\n\n/-- One step. A halted configuration does not move. -/\ndef step (M : Machine α k) (c : Cfg α k M.Q) : Cfg α k M.Q :=\n  if M.halted c.state then c\n  else\n    let o := M.δ c.state (reads c.tapes)\n    ⟨o.1, apply c.tapes o.2.1 o.2.2⟩\n\n/-- `t` steps. -/\ndef run (M : Machine α k) (c : Cfg α k M.Q) : Nat → Cfg α k M.Q\n  | 0 => c\n  | t + 1 => M.run (M.step c) t\n\n/-- The configuration in which a machine starts, given its tapes. -/\ndef init (M : Machine α k) (tapes : Fin (k + 1) → Tape α) : Cfg α k M.Q := ⟨M.start, tapes⟩\n\nvariable (M : Machine α k)\n\n@[simp] theorem run_zero (c : Cfg α k M.Q) : M.run c 0 = c := rfl\n\ntheorem run_succ (c : Cfg α k M.Q) (t : Nat) : M.run c (t + 1) = M.run (M.step c) t := rfl\n\ntheorem run_succ' (c : Cfg α k M.Q) (t : Nat) : M.run c (t + 1) = M.step (M.run c t) := by\n  induction t generalizing c with\n  | zero => rfl\n  | succ t ih => rw [run_succ, ih, ← run_succ]\n\ntheorem run_add (c : Cfg α k M.Q) (s t : Nat) : M.run c (s + t) = M.run (M.run c s) t := by\n  induction s generalizing c with\n  | zero => simp\n  | succ s ih => rw [Nat.succ_add, run_succ, ih, run_succ]\n\ntheorem step_of_halted {c : Cfg α k M.Q} (h : M.halted c.state = true) : M.step c = c := by\n  simp [step, h]\n\ntheorem run_of_halted {c : Cfg α k M.Q} (h : M.halted c.state = true) (t : Nat) : M.run c t = c := by\n  induction t with\n  | zero => rfl\n  | succ t ih => rw [run_succ', ih, step_of_halted M h]\n\n/-- Once halted, a machine stays put: running longer changes nothing. -/\ntheorem run_stable {c : Cfg α k M.Q} {s t : Nat} (h : M.halted (M.run c s).state = true)\n    (hst : s ≤ t) : M.run c t = M.run c s := by\n  obtain ⟨u, rfl⟩ := Nat.exists_eq_add_of_le hst\n  rw [run_add, run_of_halted M h]\n\n/-- The same machine with other accepting states. -/\ndef withAccepting (a : M.Q → Bool) : Machine α k := { M with accepting := a }\n\n/-- Acceptance plays no part in running. -/\ntheorem run_withAccepting (a : M.Q → Bool) (c : Cfg α k M.Q) (t : Nat) :\n    (M.withAccepting a).run c t = M.run c t := by\n  induction t generalizing c with\n  | zero => rfl\n  | succ t ih => exact ih (M.step c)\n\nend Machine\n\n/-! ## Initial tapes -/\n\n/-- Input `x` on tape `0`, every other tape blank, every head at `0`. -/\ndef inputTapes {α : Type} {k : Nat} (x : List α) : Fin (k + 1) → Tape α :=\n  fun i => if i.val = 0 then Tape.ofWord x else Tape.blank\n\n/-- Input `x` on tape `0`, certificate `w` on tape `1` (when there is one), the rest blank. -/\ndef verifierTapes {α : Type} {k : Nat} (x w : List α) : Fin (k + 1) → Tape α :=\n  fun i => if i.val = 0 then Tape.ofWord x else if i.val = 1 then Tape.ofWord w else Tape.blank\n\ntheorem verifierTapes_nil {α : Type} {k : Nat} (x : List α) :\n    (verifierTapes x [] : Fin (k + 1) → Tape α) = inputTapes x := by\n  funext i\n  simp only [verifierTapes, inputTapes, Tape.ofWord_nil, ite_self]\n\nend PvsNP\n"},"meta":{"request_id":"08f64e33-829","took_us":129,"limits":{"limit":600,"remaining":44,"window_ms":60000,"reset_ms":55544,"cost":1,"streams":{"limit":16,"open":0}}}}