| 1 | import PvsNP.Basic.Finite |
| 2 | import PvsNP.Machine.Tape |
| 3 | |
| 4 | /-! |
| 5 | # Multi-tape Turing machines |
| 6 | |
| 7 | A `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 |
| 10 | state, a symbol to write on every tape and a move for every head. |
| 11 | |
| 12 | A machine stops in the states marked `halted`; `accepting` says which of those accept. Running a halted |
| 13 | configuration leaves it unchanged, so "halted after `t` steps" means "halted within `t` steps". |
| 14 | |
| 15 | Design 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 state could store the whole input |
| 20 | and the model would decide anything in one step. |
| 21 | -/ |
| 22 | |
| 23 | namespace PvsNP |
| 24 | |
| 25 | |
| 26 | structure Machine (α : Type) (k : Nat) where |
| 27 | |
| 28 | Q : Type |
| 29 | |
| 30 | finite : IsFinite Q |
| 31 | |
| 32 | start : Q |
| 33 | |
| 34 | halted : Q → Bool |
| 35 | |
| 36 | accepting : Q → Bool |
| 37 | /-- The transition: from a state and the symbols under the heads, the next state, the symbols to |
| 38 | write and the head moves. -/ |
| 39 | δ : Q → (Fin (k + 1) → Option α) → Q × (Fin (k + 1) → Option α) × (Fin (k + 1) → Dir) |
| 40 | |
| 41 | |
| 42 | structure Cfg (α : Type) (k : Nat) (Q : Type) where |
| 43 | state : Q |
| 44 | tapes : Fin (k + 1) → Tape α |
| 45 | |
| 46 | namespace Machine |
| 47 | |
| 48 | variable {α : Type} {k : Nat} |
| 49 | |
| 50 | |
| 51 | def apply (tapes : Fin (k + 1) → Tape α) (w : Fin (k + 1) → Option α) (d : Fin (k + 1) → Dir) : |
| 52 | Fin (k + 1) → Tape α := |
| 53 | fun i => ((tapes i).write (w i)).move (d i) |
| 54 | |
| 55 | |
| 56 | def reads (tapes : Fin (k + 1) → Tape α) : Fin (k + 1) → Option α := |
| 57 | fun i => (tapes i).read |
| 58 | |
| 59 | |
| 60 | def step (M : Machine α k) (c : Cfg α k M.Q) : Cfg α k M.Q := |
| 61 | if M.halted c.state then c |
| 62 | else |
| 63 | let o := M.δ c.state (reads c.tapes) |
| 64 | ⟨o.1, apply c.tapes o.2.1 o.2.2⟩ |
| 65 | |
| 66 | |
| 67 | def run (M : Machine α k) (c : Cfg α k M.Q) : Nat → Cfg α k M.Q |
| 68 | | 0 => c |
| 69 | | t + 1 => M.run (M.step c) t |
| 70 | |
| 71 | |
| 72 | def init (M : Machine α k) (tapes : Fin (k + 1) → Tape α) : Cfg α k M.Q := ⟨M.start, tapes⟩ |
| 73 | |
| 74 | variable (M : Machine α k) |
| 75 | |
| 76 | @[simp] theorem run_zero (c : Cfg α k M.Q) : M.run c 0 = c := rfl |
| 77 | |
| 78 | theorem run_succ (c : Cfg α k M.Q) (t : Nat) : M.run c (t + 1) = M.run (M.step c) t := rfl |
| 79 | |
| 80 | theorem run_succ' (c : Cfg α k M.Q) (t : Nat) : M.run c (t + 1) = M.step (M.run c t) := by |
| 81 | induction t generalizing c with |
| 82 | | zero => rfl |
| 83 | | succ t ih => rw [run_succ, ih, ← run_succ] |
| 84 | |
| 85 | theorem run_add (c : Cfg α k M.Q) (s t : Nat) : M.run c (s + t) = M.run (M.run c s) t := by |
| 86 | induction s generalizing c with |
| 87 | | zero => simp |
| 88 | | succ s ih => rw [Nat.succ_add, run_succ, ih, run_succ] |
| 89 | |
| 90 | theorem step_of_halted {c : Cfg α k M.Q} (h : M.halted c.state = true) : M.step c = c := by |
| 91 | simp [step, h] |
| 92 | |
| 93 | theorem run_of_halted {c : Cfg α k M.Q} (h : M.halted c.state = true) (t : Nat) : M.run c t = c := by |
| 94 | induction t with |
| 95 | | zero => rfl |
| 96 | | succ t ih => rw [run_succ', ih, step_of_halted M h] |
| 97 | |
| 98 | |
| 99 | theorem run_stable {c : Cfg α k M.Q} {s t : Nat} (h : M.halted (M.run c s).state = true) |
| 100 | (hst : s ≤ t) : M.run c t = M.run c s := by |
| 101 | obtain ⟨u, rfl⟩ := Nat.exists_eq_add_of_le hst |
| 102 | rw [run_add, run_of_halted M h] |
| 103 | |
| 104 | |
| 105 | def withAccepting (a : M.Q → Bool) : Machine α k := { M with accepting := a } |
| 106 | |
| 107 | |
| 108 | theorem run_withAccepting (a : M.Q → Bool) (c : Cfg α k M.Q) (t : Nat) : |
| 109 | (M.withAccepting a).run c t = M.run c t := by |
| 110 | induction t generalizing c with |
| 111 | | zero => rfl |
| 112 | | succ t ih => exact ih (M.step c) |
| 113 | |
| 114 | end Machine |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | def inputTapes {α : Type} {k : Nat} (x : List α) : Fin (k + 1) → Tape α := |
| 120 | fun i => if i.val = 0 then Tape.ofWord x else Tape.blank |
| 121 | |
| 122 | |
| 123 | def verifierTapes {α : Type} {k : Nat} (x w : List α) : Fin (k + 1) → Tape α := |
| 124 | fun i => if i.val = 0 then Tape.ofWord x else if i.val = 1 then Tape.ofWord w else Tape.blank |
| 125 | |
| 126 | theorem verifierTapes_nil {α : Type} {k : Nat} (x : List α) : |
| 127 | (verifierTapes x [] : Fin (k + 1) → Tape α) = inputTapes x := by |
| 128 | funext i |
| 129 | simp only [verifierTapes, inputTapes, Tape.ofWord_nil, ite_self] |
| 130 | |
| 131 | end PvsNP |