1import PvsNP.Basic.Finite
2import PvsNP.Machine.Tape
3
4/-!
5# Multi-tape Turing machines
6
7A `Machine α k` is a deterministic Turing machine with `k + 1` tapes over the tape alphabet `Option α`
8(the input symbols and the blank). Tape `0` holds the input. Its control is a *finite* type of states
9`Q`; in each step it reads the symbol under every head, and the transition function `δ` gives the next
10state, a symbol to write on every tape and a move for every head.
11
12A machine stops in the states marked `halted`; `accepting` says which of those accept. Running a halted
13configuration leaves it unchanged, so "halted after `t` steps" means "halted within `t` steps".
14
15Design notes:
16- The tape alphabet is `Option α`: machines have no extra work symbols. For `|α| ≥ 2` this changes
17 running times only polynomially; it keeps machines over one alphabet composable without re-encoding
18 tapes.
19- Finiteness of `Q` is part of the structure (`finite`). Without it a machine could scan its input
20 once, accumulating it in the state, and then apply an arbitrary predicate: every language would be
21 decidable in linear time. The alphabet's finiteness is required by the complexity classes.
22-/
23
24namespace PvsNP
25
26/-- A deterministic Turing machine with `k + 1` tapes over the tape alphabet `Option α`. -/
27structure Machine (α : Type) (k : Nat) where
28 /-- The states. -/
29 Q : Type
30 /-- There are finitely many states. -/
31 finite : IsFinite Q
32 /-- The start state. -/
33 start : Q
34 /-- The states in which the machine stops. -/
35 halted : Q → Bool
36 /-- The halted states that accept. -/
37 accepting : Q → Bool
38 /-- The transition: from a state and the symbols under the heads, the next state, the symbols to
39 write and the head moves. -/
40 δ : Q → (Fin (k + 1) → Option α) → Q × (Fin (k + 1) → Option α) × (Fin (k + 1) → Dir)
41
42/-- A configuration: a state and the contents of every tape. -/
43structure Cfg (α : Type) (k : Nat) (Q : Type) where
44 state : Q
45 tapes : Fin (k + 1) → Tape α
46
47namespace Machine
48
49variable {α : Type} {k : Nat}
50
51/-- Apply one transition to every tape at once: write, then move. -/
52def apply (tapes : Fin (k + 1) → Tape α) (w : Fin (k + 1) → Option α) (d : Fin (k + 1) → Dir) :
53 Fin (k + 1) → Tape α :=
54 fun i => ((tapes i).write (w i)).move (d i)
55
56/-- The symbols under the heads. -/
57def reads (tapes : Fin (k + 1) → Tape α) : Fin (k + 1) → Option α :=
58 fun i => (tapes i).read
59
60/-- One step. A halted configuration does not move. -/
61def step (M : Machine α k) (c : Cfg α k M.Q) : Cfg α k M.Q :=
62 if M.halted c.state then c
63 else
64 let o := M.δ c.state (reads c.tapes)
65 ⟨o.1, apply c.tapes o.2.1 o.2.2⟩
66
67/-- `t` steps. -/
68def run (M : Machine α k) (c : Cfg α k M.Q) : Nat → Cfg α k M.Q
69 | 0 => c
70 | t + 1 => M.run (M.step c) t
71
72/-- The configuration in which a machine starts, given its tapes. -/
73def init (M : Machine α k) (tapes : Fin (k + 1) → Tape α) : Cfg α k M.Q := ⟨M.start, tapes⟩
74
75variable (M : Machine α k)
76
77@[simp] theorem run_zero (c : Cfg α k M.Q) : M.run c 0 = c := rfl
78
79theorem run_succ (c : Cfg α k M.Q) (t : Nat) : M.run c (t + 1) = M.run (M.step c) t := rfl
80
81theorem run_succ' (c : Cfg α k M.Q) (t : Nat) : M.run c (t + 1) = M.step (M.run c t) := by
82 induction t generalizing c with
83 | zero => rfl
84 | succ t ih => rw [run_succ, ih, ← run_succ]
85
86theorem run_add (c : Cfg α k M.Q) (s t : Nat) : M.run c (s + t) = M.run (M.run c s) t := by
87 induction s generalizing c with
88 | zero => simp
89 | succ s ih => rw [Nat.succ_add, run_succ, ih, run_succ]
90
91theorem step_of_halted {c : Cfg α k M.Q} (h : M.halted c.state = true) : M.step c = c := by
92 simp [step, h]
93
94theorem run_of_halted {c : Cfg α k M.Q} (h : M.halted c.state = true) (t : Nat) : M.run c t = c := by
95 induction t with
96 | zero => rfl
97 | succ t ih => rw [run_succ', ih, step_of_halted M h]
98
99/-- Once halted, a machine stays put: running longer changes nothing. -/
100theorem run_stable {c : Cfg α k M.Q} {s t : Nat} (h : M.halted (M.run c s).state = true)
101 (hst : s ≤ t) : M.run c t = M.run c s := by
102 obtain ⟨u, rfl⟩ := Nat.exists_eq_add_of_le hst
103 rw [run_add, run_of_halted M h]
104
105/-- The same machine with other accepting states. -/
106def withAccepting (a : M.Q → Bool) : Machine α k := { M with accepting := a }
107
108/-- Acceptance plays no part in running. -/
109theorem run_withAccepting (a : M.Q → Bool) (c : Cfg α k M.Q) (t : Nat) :
110 (M.withAccepting a).run c t = M.run c t := by
111 induction t generalizing c with
112 | zero => rfl
113 | succ t ih => exact ih (M.step c)
114
115end Machine
116
117/-! ## Initial tapes -/
118
119/-- Input `x` on tape `0`, every other tape blank, every head at `0`. -/
120def inputTapes {α : Type} {k : Nat} (x : List α) : Fin (k + 1) → Tape α :=
121 fun i => if i.val = 0 then Tape.ofWord x else Tape.blank
122
123/-- Input `x` on tape `0`, certificate `w` on tape `1` (when there is one), the rest blank. -/
124def verifierTapes {α : Type} {k : Nat} (x w : List α) : Fin (k + 1) → Tape α :=
125 fun i => if i.val = 0 then Tape.ofWord x else if i.val = 1 then Tape.ofWord w else Tape.blank
126
127theorem verifierTapes_nil {α : Type} {k : Nat} (x : List α) :
128 (verifierTapes x [] : Fin (k + 1) → Tape α) = inputTapes x := by
129 funext i
130 simp only [verifierTapes, inputTapes, Tape.ofWord_nil, ite_self]
131
132end PvsNP