| 1 | import PvsNP.Machine.Defs |
| 2 | |
| 3 | /-! |
| 4 | # Running a machine on more tapes |
| 5 | |
| 6 | A machine with `k + 1` tapes can run inside a machine with `k' + 1` tapes: an `Embedding` says which |
| 7 | of the larger machine's tapes stand for which of the smaller one's. The lifted machine does exactly |
| 8 | what the original does on those tapes and leaves every other tape untouched. |
| 9 | |
| 10 | Two embeddings are used: |
| 11 | - `Embedding.castLE`: tape `i` goes to tape `i`; the new tapes come last. |
| 12 | - `Embedding.skipOne`: tape `0` stays, tape `i + 1` goes to tape `i + 2`. Tape `1` is left alone, so a |
| 13 | certificate written there survives the run (used for P ⊆ NP and for reductions into NP). |
| 14 | |
| 15 | The main result, `Machine.run_lift`, is a step-by-step simulation. |
| 16 | -/ |
| 17 | |
| 18 | namespace PvsNP |
| 19 | |
| 20 | /-- How the tapes of a `k + 1`-tape machine sit among `k' + 1` tapes: `emb` places each small tape, |
| 21 | and `back` recognises the places (`none` for a tape the small machine does not use). -/ |
| 22 | structure Embedding (k k' : Nat) where |
| 23 | emb : Fin (k + 1) → Fin (k' + 1) |
| 24 | back : Fin (k' + 1) → Option (Fin (k + 1)) |
| 25 | back_emb : ∀ i, back (emb i) = some i |
| 26 | |
| 27 | namespace Embedding |
| 28 | |
| 29 | |
| 30 | def castLE {k k' : Nat} (h : k ≤ k') : Embedding k k' where |
| 31 | emb i := ⟨i.val, by omega⟩ |
| 32 | back j := if hj : j.val < k + 1 then some ⟨j.val, hj⟩ else none |
| 33 | back_emb i := by simp [i.isLt] |
| 34 | |
| 35 | |
| 36 | def skipOne {k k' : Nat} (h : k + 1 ≤ k') : Embedding k k' where |
| 37 | emb i := if i.val = 0 then ⟨0, by omega⟩ else ⟨i.val + 1, by omega⟩ |
| 38 | back j := |
| 39 | if j.val = 0 then some ⟨0, by omega⟩ |
| 40 | else if j.val = 1 then none |
| 41 | else if hj : j.val - 1 < k + 1 then some ⟨j.val - 1, hj⟩ else none |
| 42 | back_emb i := by |
| 43 | by_cases hi : i.val = 0 |
| 44 | · simp only [hi, ↓reduceIte, Option.some.injEq] |
| 45 | exact Fin.ext hi.symm |
| 46 | · have h1 : i.val + 1 ≠ 0 := by omega |
| 47 | have h2 : i.val + 1 ≠ 1 := by omega |
| 48 | have h3 : i.val + 1 - 1 < k + 1 := by omega |
| 49 | simp only [hi, h1, h2, h3, ↓reduceIte, ↓reduceDIte, Option.some.injEq] |
| 50 | exact Fin.ext (by simp) |
| 51 | |
| 52 | end Embedding |
| 53 | |
| 54 | variable {α : Type} {k k' : Nat} |
| 55 | |
| 56 | |
| 57 | def liftTapes (E : Embedding k k') (T : Fin (k + 1) → Tape α) (rest : Fin (k' + 1) → Tape α) : |
| 58 | Fin (k' + 1) → Tape α := |
| 59 | fun j => match E.back j with |
| 60 | | some i => T i |
| 61 | | none => rest j |
| 62 | |
| 63 | theorem liftTapes_emb (E : Embedding k k') (T : Fin (k + 1) → Tape α) (rest : Fin (k' + 1) → Tape α) |
| 64 | (i : Fin (k + 1)) : liftTapes E T rest (E.emb i) = T i := by |
| 65 | simp [liftTapes, E.back_emb] |
| 66 | |
| 67 | |
| 68 | theorem liftTapes_eq (E : Embedding k k') {T : Fin (k + 1) → Tape α} |
| 69 | {rest T' : Fin (k' + 1) → Tape α} |
| 70 | (h₁ : ∀ j i, E.back j = some i → T' j = T i) (h₂ : ∀ j, E.back j = none → T' j = rest j) : |
| 71 | liftTapes E T rest = T' := by |
| 72 | funext j |
| 73 | unfold liftTapes |
| 74 | split |
| 75 | · next i hi => exact (h₁ j i hi).symm |
| 76 | · next hj => exact (h₂ j hj).symm |
| 77 | |
| 78 | namespace Machine |
| 79 | |
| 80 | |
| 81 | def lift (M : Machine α k) (E : Embedding k k') : Machine α k' where |
| 82 | Q := M.Q |
| 83 | finite := M.finite |
| 84 | start := M.start |
| 85 | halted := M.halted |
| 86 | accepting := M.accepting |
| 87 | δ q r := |
| 88 | let o := M.δ q (fun i => r (E.emb i)) |
| 89 | (o.1, |
| 90 | fun j => match E.back j with |
| 91 | | some i => o.2.1 i |
| 92 | | none => r j, |
| 93 | fun j => match E.back j with |
| 94 | | some i => o.2.2 i |
| 95 | | none => .stay) |
| 96 | |
| 97 | |
| 98 | def liftCfg (M : Machine α k) (E : Embedding k k') (rest : Fin (k' + 1) → Tape α) (c : Cfg α k M.Q) : |
| 99 | Cfg α k' (M.lift E).Q := |
| 100 | ⟨c.state, liftTapes E c.tapes rest⟩ |
| 101 | |
| 102 | theorem step_lift (M : Machine α k) (E : Embedding k k') (rest : Fin (k' + 1) → Tape α) |
| 103 | (c : Cfg α k M.Q) : (M.lift E).step (M.liftCfg E rest c) = M.liftCfg E rest (M.step c) := by |
| 104 | unfold step |
| 105 | by_cases h : M.halted c.state = true |
| 106 | · have h' : (M.lift E).halted (M.liftCfg E rest c).state = true := h |
| 107 | simp only [h, h', ↓reduceIte] |
| 108 | · have h' : ¬ (M.lift E).halted (M.liftCfg E rest c).state = true := h |
| 109 | simp only [h, h', ↓reduceIte, Bool.false_eq_true] |
| 110 | have hr : (fun i => reads (liftTapes E c.tapes rest) (E.emb i)) = reads c.tapes := by |
| 111 | funext i |
| 112 | simp [reads, liftTapes_emb] |
| 113 | simp only [lift, liftCfg, Cfg.mk.injEq] |
| 114 | refine ⟨by rw [hr], ?_⟩ |
| 115 | funext j |
| 116 | simp only [apply, liftTapes] |
| 117 | split |
| 118 | · next i hi => simp only [hr] |
| 119 | · next hj => simp [hj, reads, liftTapes] |
| 120 | |
| 121 | |
| 122 | theorem run_lift (M : Machine α k) (E : Embedding k k') (rest : Fin (k' + 1) → Tape α) |
| 123 | (c : Cfg α k M.Q) (t : Nat) : |
| 124 | (M.lift E).run (M.liftCfg E rest c) t = M.liftCfg E rest (M.run c t) := by |
| 125 | induction t generalizing c with |
| 126 | | zero => rfl |
| 127 | | succ t ih => rw [run_succ, step_lift, ih, run_succ] |
| 128 | |
| 129 | end Machine |
| 130 | |
| 131 | |
| 132 | |
| 133 | theorem liftTapes_castLE_inputTapes (h : k ≤ k') (y x : List α) : |
| 134 | liftTapes (Embedding.castLE h) (inputTapes y) (inputTapes x : Fin (k' + 1) → Tape α) = |
| 135 | inputTapes y := by |
| 136 | apply liftTapes_eq |
| 137 | · intro j i hi |
| 138 | simp only [Embedding.castLE] at hi |
| 139 | split at hi |
| 140 | · cases hi; simp [inputTapes] |
| 141 | · cases hi |
| 142 | · intro j hj |
| 143 | simp only [Embedding.castLE] at hj |
| 144 | split at hj |
| 145 | · cases hj |
| 146 | · have : j.val ≠ 0 := by omega |
| 147 | simp [inputTapes, this] |
| 148 | |
| 149 | theorem liftTapes_castLE_verifierTapes (h : k ≤ k') (x w : List α) : |
| 150 | liftTapes (Embedding.castLE h) (verifierTapes x w) |
| 151 | (verifierTapes x w : Fin (k' + 1) → Tape α) = verifierTapes x w := by |
| 152 | apply liftTapes_eq |
| 153 | · intro j i hi |
| 154 | simp only [Embedding.castLE] at hi |
| 155 | split at hi |
| 156 | · cases hi; simp [verifierTapes] |
| 157 | · cases hi |
| 158 | · intro j _; rfl |
| 159 | |
| 160 | theorem liftTapes_skipOne_inputTapes (h : k + 1 ≤ k') (y x w : List α) : |
| 161 | liftTapes (Embedding.skipOne h) (inputTapes y) (verifierTapes x w : Fin (k' + 1) → Tape α) = |
| 162 | verifierTapes y w := by |
| 163 | apply liftTapes_eq |
| 164 | · intro j i hi |
| 165 | simp only [Embedding.skipOne] at hi |
| 166 | by_cases h0 : j.val = 0 |
| 167 | · simp only [h0, ↓reduceIte, Option.some.injEq] at hi |
| 168 | subst hi |
| 169 | simp [verifierTapes, inputTapes, h0] |
| 170 | · by_cases h1 : j.val = 1 |
| 171 | · simp [h1] at hi |
| 172 | · simp only [h0, h1, ↓reduceIte] at hi |
| 173 | split at hi |
| 174 | · cases hi |
| 175 | have : j.val - 1 ≠ 0 := by omega |
| 176 | simp [verifierTapes, inputTapes, h0, h1, this] |
| 177 | · cases hi |
| 178 | · intro j hj |
| 179 | simp only [Embedding.skipOne] at hj |
| 180 | by_cases h0 : j.val = 0 |
| 181 | · simp [h0] at hj |
| 182 | · by_cases h1 : j.val = 1 |
| 183 | · simp [verifierTapes, h1] |
| 184 | · simp only [h0, h1, ↓reduceIte] at hj |
| 185 | split at hj |
| 186 | · cases hj |
| 187 | · simp [verifierTapes, h0, h1] |
| 188 | |
| 189 | end PvsNP |