# PvsNP/Machine/Lift.lean @ f5592d3aedb2a3159eb374b0c8fe8cc6eec1d4da

````
import PvsNP.Machine.Defs

/-!
# Running a machine on more tapes

A machine with `k + 1` tapes can run inside a machine with `k' + 1` tapes: an `Embedding` says which
of the larger machine's tapes stand for which of the smaller one's. The lifted machine does exactly
what the original does on those tapes and leaves every other tape untouched.

Two embeddings are used:
- `Embedding.castLE`: tape `i` goes to tape `i`; the new tapes come last.
- `Embedding.skipOne`: tape `0` stays, tape `i + 1` goes to tape `i + 2`. Tape `1` is left alone, so a
  certificate written there survives the run (used for P ⊆ NP and for reductions into NP).

The main result, `Machine.run_lift`, is a step-by-step simulation.
-/

namespace PvsNP

/-- How the tapes of a `k + 1`-tape machine sit among `k' + 1` tapes: `emb` places each small tape,
and `back` recognises the places (`none` for a tape the small machine does not use). -/
structure Embedding (k k' : Nat) where
  emb : Fin (k + 1) → Fin (k' + 1)
  back : Fin (k' + 1) → Option (Fin (k + 1))
  back_emb : ∀ i, back (emb i) = some i

namespace Embedding

/-- Tape `i` stays tape `i`. -/
def castLE {k k' : Nat} (h : k ≤ k') : Embedding k k' where
  emb i := ⟨i.val, by omega⟩
  back j := if hj : j.val < k + 1 then some ⟨j.val, hj⟩ else none
  back_emb i := by simp [i.isLt]

/-- Tape `0` stays; tape `i + 1` becomes tape `i + 2`. Tape `1` is not used. -/
def skipOne {k k' : Nat} (h : k + 1 ≤ k') : Embedding k k' where
  emb i := if i.val = 0 then ⟨0, by omega⟩ else ⟨i.val + 1, by omega⟩
  back j :=
    if j.val = 0 then some ⟨0, by omega⟩
    else if j.val = 1 then none
    else if hj : j.val - 1 < k + 1 then some ⟨j.val - 1, hj⟩ else none
  back_emb i := by
    by_cases hi : i.val = 0
    · simp only [hi, ↓reduceIte, Option.some.injEq]
      exact Fin.ext hi.symm
    · have h1 : i.val + 1 ≠ 0 := by omega
      have h2 : i.val + 1 ≠ 1 := by omega
      have h3 : i.val + 1 - 1 < k + 1 := by omega
      simp only [hi, h1, h2, h3, ↓reduceIte, ↓reduceDIte, Option.some.injEq]
      exact Fin.ext (by simp)

end Embedding

variable {α : Type} {k k' : Nat}

/-- The tapes of the large machine: the small machine's tapes where `E` places them, `rest` elsewhere. -/
def liftTapes (E : Embedding k k') (T : Fin (k + 1) → Tape α) (rest : Fin (k' + 1) → Tape α) :
    Fin (k' + 1) → Tape α :=
  fun j => match E.back j with
    | some i => T i
    | none => rest j

theorem liftTapes_emb (E : Embedding k k') (T : Fin (k + 1) → Tape α) (rest : Fin (k' + 1) → Tape α)
    (i : Fin (k + 1)) : liftTapes E T rest (E.emb i) = T i := by
  simp [liftTapes, E.back_emb]

/-- To identify lifted tapes, compare them where `E` places a small tape and elsewhere. -/
theorem liftTapes_eq (E : Embedding k k') {T : Fin (k + 1) → Tape α}
    {rest T' : Fin (k' + 1) → Tape α}
    (h₁ : ∀ j i, E.back j = some i → T' j = T i) (h₂ : ∀ j, E.back j = none → T' j = rest j) :
    liftTapes E T rest = T' := by
  funext j
  unfold liftTapes
  split
  · next i hi => exact (h₁ j i hi).symm
  · next hj => exact (h₂ j hj).symm

namespace Machine

/-- `M` running on the tapes `E` picks out; every other tape is read, written back, and not moved. -/
def lift (M : Machine α k) (E : Embedding k k') : Machine α k' where
  Q := M.Q
  finite := M.finite
  start := M.start
  halted := M.halted
  accepting := M.accepting
  δ q r :=
    let o := M.δ q (fun i => r (E.emb i))
    (o.1,
     fun j => match E.back j with
       | some i => o.2.1 i
       | none => r j,
     fun j => match E.back j with
       | some i => o.2.2 i
       | none => .stay)

/-- The lifted machine's configuration standing for `c`, with `rest` on the other tapes. -/
def liftCfg (M : Machine α k) (E : Embedding k k') (rest : Fin (k' + 1) → Tape α) (c : Cfg α k M.Q) :
    Cfg α k' (M.lift E).Q :=
  ⟨c.state, liftTapes E c.tapes rest⟩

theorem step_lift (M : Machine α k) (E : Embedding k k') (rest : Fin (k' + 1) → Tape α)
    (c : Cfg α k M.Q) : (M.lift E).step (M.liftCfg E rest c) = M.liftCfg E rest (M.step c) := by
  unfold step
  by_cases h : M.halted c.state = true
  · have h' : (M.lift E).halted (M.liftCfg E rest c).state = true := h
    simp only [h, h', ↓reduceIte]
  · have h' : ¬ (M.lift E).halted (M.liftCfg E rest c).state = true := h
    simp only [h, h', ↓reduceIte, Bool.false_eq_true]
    have hr : (fun i => reads (liftTapes E c.tapes rest) (E.emb i)) = reads c.tapes := by
      funext i
      simp [reads, liftTapes_emb]
    simp only [lift, liftCfg, Cfg.mk.injEq]
    refine ⟨by rw [hr], ?_⟩
    funext j
    simp only [apply, liftTapes]
    split
    · next i hi => simp only [hr]
    · next hj => simp [hj, reads, liftTapes]

/-- **Simulation.** The lifted machine runs exactly like the original on its tapes. -/
theorem run_lift (M : Machine α k) (E : Embedding k k') (rest : Fin (k' + 1) → Tape α)
    (c : Cfg α k M.Q) (t : Nat) :
    (M.lift E).run (M.liftCfg E rest c) t = M.liftCfg E rest (M.run c t) := by
  induction t generalizing c with
  | zero => rfl
  | succ t ih => rw [run_succ, step_lift, ih, run_succ]

end Machine

/-! ## How the embeddings act on start tapes -/

theorem liftTapes_castLE_inputTapes (h : k ≤ k') (y x : List α) :
    liftTapes (Embedding.castLE h) (inputTapes y) (inputTapes x : Fin (k' + 1) → Tape α) =
      inputTapes y := by
  apply liftTapes_eq
  · intro j i hi
    simp only [Embedding.castLE] at hi
    split at hi
    · cases hi; simp [inputTapes]
    · cases hi
  · intro j hj
    simp only [Embedding.castLE] at hj
    split at hj
    · cases hj
    · have : j.val ≠ 0 := by omega
      simp [inputTapes, this]

theorem liftTapes_castLE_verifierTapes (h : k ≤ k') (x w : List α) :
    liftTapes (Embedding.castLE h) (verifierTapes x w)
        (verifierTapes x w : Fin (k' + 1) → Tape α) = verifierTapes x w := by
  apply liftTapes_eq
  · intro j i hi
    simp only [Embedding.castLE] at hi
    split at hi
    · cases hi; simp [verifierTapes]
    · cases hi
  · intro j _; rfl

theorem liftTapes_skipOne_inputTapes (h : k + 1 ≤ k') (y x w : List α) :
    liftTapes (Embedding.skipOne h) (inputTapes y) (verifierTapes x w : Fin (k' + 1) → Tape α) =
      verifierTapes y w := by
  apply liftTapes_eq
  · intro j i hi
    simp only [Embedding.skipOne] at hi
    by_cases h0 : j.val = 0
    · simp only [h0, ↓reduceIte, Option.some.injEq] at hi
      subst hi
      simp [verifierTapes, inputTapes, h0]
    · by_cases h1 : j.val = 1
      · simp [h1] at hi
      · simp only [h0, h1, ↓reduceIte] at hi
        split at hi
        · cases hi
          have : j.val - 1 ≠ 0 := by omega
          simp [verifierTapes, inputTapes, h0, h1, this]
        · cases hi
  · intro j hj
    simp only [Embedding.skipOne] at hj
    by_cases h0 : j.val = 0
    · simp [h0] at hj
    · by_cases h1 : j.val = 1
      · simp [verifierTapes, h1]
      · simp only [h0, h1, ↓reduceIte] at hj
        split at hj
        · cases hj
        · simp [verifierTapes, h0, h1]

end PvsNP

````

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