{"repo":"pvsnp","rev":"346abe536d053a5c1b61722fd9b9ea5144e9a85c","blob":{"commit":"346abe536d053a5c1b61722fd9b9ea5144e9a85c","path":"PvsNP/Machine/Lift.lean","oid":"4a616a70c69e6e93841275e90f266d650635f69a","size":7105,"range_start":0,"range_end":7105,"truncated":false,"binary":false,"encoding":"utf8","content":"import PvsNP.Machine.Defs\n\n/-!\n# Running a machine on more tapes\n\nA machine with `k + 1` tapes can run inside a machine with `k' + 1` tapes: an `Embedding` says which\nof the larger machine's tapes stand for which of the smaller one's. The lifted machine does exactly\nwhat the original does on those tapes and leaves every other tape untouched.\n\nTwo embeddings are used:\n- `Embedding.castLE`: tape `i` goes to tape `i`; the new tapes come last.\n- `Embedding.skipOne`: tape `0` stays, tape `i + 1` goes to tape `i + 2`. Tape `1` is left alone, so a\n  certificate written there survives the run (used for P ⊆ NP and for reductions into NP).\n\nThe main result, `Machine.run_lift`, is a step-by-step simulation.\n-/\n\nnamespace PvsNP\n\n/-- How the tapes of a `k + 1`-tape machine sit among `k' + 1` tapes: `emb` places each small tape,\nand `back` recognises the places (`none` for a tape the small machine does not use). -/\nstructure Embedding (k k' : Nat) where\n  emb : Fin (k + 1) → Fin (k' + 1)\n  back : Fin (k' + 1) → Option (Fin (k + 1))\n  back_emb : ∀ i, back (emb i) = some i\n\nnamespace Embedding\n\n/-- Tape `i` stays tape `i`. -/\ndef castLE {k k' : Nat} (h : k ≤ k') : Embedding k k' where\n  emb i := ⟨i.val, by omega⟩\n  back j := if hj : j.val < k + 1 then some ⟨j.val, hj⟩ else none\n  back_emb i := by simp [i.isLt]\n\n/-- Tape `0` stays; tape `i + 1` becomes tape `i + 2`. Tape `1` is not used. -/\ndef skipOne {k k' : Nat} (h : k + 1 ≤ k') : Embedding k k' where\n  emb i := if i.val = 0 then ⟨0, by omega⟩ else ⟨i.val + 1, by omega⟩\n  back j :=\n    if j.val = 0 then some ⟨0, by omega⟩\n    else if j.val = 1 then none\n    else if hj : j.val - 1 < k + 1 then some ⟨j.val - 1, hj⟩ else none\n  back_emb i := by\n    by_cases hi : i.val = 0\n    · simp only [hi, ↓reduceIte, Option.some.injEq]\n      exact Fin.ext hi.symm\n    · have h1 : i.val + 1 ≠ 0 := by omega\n      have h2 : i.val + 1 ≠ 1 := by omega\n      have h3 : i.val + 1 - 1 < k + 1 := by omega\n      simp only [hi, h1, h2, h3, ↓reduceIte, ↓reduceDIte, Option.some.injEq]\n      exact Fin.ext (by simp)\n\nend Embedding\n\nvariable {α : Type} {k k' : Nat}\n\n/-- The tapes of the large machine: the small machine's tapes where `E` places them, `rest` elsewhere. -/\ndef liftTapes (E : Embedding k k') (T : Fin (k + 1) → Tape α) (rest : Fin (k' + 1) → Tape α) :\n    Fin (k' + 1) → Tape α :=\n  fun j => match E.back j with\n    | some i => T i\n    | none => rest j\n\ntheorem liftTapes_emb (E : Embedding k k') (T : Fin (k + 1) → Tape α) (rest : Fin (k' + 1) → Tape α)\n    (i : Fin (k + 1)) : liftTapes E T rest (E.emb i) = T i := by\n  simp [liftTapes, E.back_emb]\n\n/-- To identify lifted tapes, compare them where `E` places a small tape and elsewhere. -/\ntheorem liftTapes_eq (E : Embedding k k') {T : Fin (k + 1) → Tape α}\n    {rest T' : Fin (k' + 1) → Tape α}\n    (h₁ : ∀ j i, E.back j = some i → T' j = T i) (h₂ : ∀ j, E.back j = none → T' j = rest j) :\n    liftTapes E T rest = T' := by\n  funext j\n  unfold liftTapes\n  split\n  · next i hi => exact (h₁ j i hi).symm\n  · next hj => exact (h₂ j hj).symm\n\nnamespace Machine\n\n/-- `M` running on the tapes `E` picks out; every other tape is read, written back, and not moved. -/\ndef lift (M : Machine α k) (E : Embedding k k') : Machine α k' where\n  Q := M.Q\n  finite := M.finite\n  start := M.start\n  halted := M.halted\n  accepting := M.accepting\n  δ q r :=\n    let o := M.δ q (fun i => r (E.emb i))\n    (o.1,\n     fun j => match E.back j with\n       | some i => o.2.1 i\n       | none => r j,\n     fun j => match E.back j with\n       | some i => o.2.2 i\n       | none => .stay)\n\n/-- The lifted machine's configuration standing for `c`, with `rest` on the other tapes. -/\ndef liftCfg (M : Machine α k) (E : Embedding k k') (rest : Fin (k' + 1) → Tape α) (c : Cfg α k M.Q) :\n    Cfg α k' (M.lift E).Q :=\n  ⟨c.state, liftTapes E c.tapes rest⟩\n\ntheorem step_lift (M : Machine α k) (E : Embedding k k') (rest : Fin (k' + 1) → Tape α)\n    (c : Cfg α k M.Q) : (M.lift E).step (M.liftCfg E rest c) = M.liftCfg E rest (M.step c) := by\n  unfold step\n  by_cases h : M.halted c.state = true\n  · have h' : (M.lift E).halted (M.liftCfg E rest c).state = true := h\n    simp only [h, h', ↓reduceIte]\n  · have h' : ¬ (M.lift E).halted (M.liftCfg E rest c).state = true := h\n    simp only [h, h', ↓reduceIte, Bool.false_eq_true]\n    have hr : (fun i => reads (liftTapes E c.tapes rest) (E.emb i)) = reads c.tapes := by\n      funext i\n      simp [reads, liftTapes_emb]\n    simp only [lift, liftCfg, Cfg.mk.injEq]\n    refine ⟨by rw [hr], ?_⟩\n    funext j\n    simp only [apply, liftTapes]\n    split\n    · next i hi => simp only [hr]\n    · next hj => simp [hj, reads, liftTapes]\n\n/-- **Simulation.** The lifted machine runs exactly like the original on its tapes. -/\ntheorem run_lift (M : Machine α k) (E : Embedding k k') (rest : Fin (k' + 1) → Tape α)\n    (c : Cfg α k M.Q) (t : Nat) :\n    (M.lift E).run (M.liftCfg E rest c) t = M.liftCfg E rest (M.run c t) := by\n  induction t generalizing c with\n  | zero => rfl\n  | succ t ih => rw [run_succ, step_lift, ih, run_succ]\n\nend Machine\n\n/-! ## How the embeddings act on start tapes -/\n\ntheorem liftTapes_castLE_inputTapes (h : k ≤ k') (y x : List α) :\n    liftTapes (Embedding.castLE h) (inputTapes y) (inputTapes x : Fin (k' + 1) → Tape α) =\n      inputTapes y := by\n  apply liftTapes_eq\n  · intro j i hi\n    simp only [Embedding.castLE] at hi\n    split at hi\n    · cases hi; simp [inputTapes]\n    · cases hi\n  · intro j hj\n    simp only [Embedding.castLE] at hj\n    split at hj\n    · cases hj\n    · have : j.val ≠ 0 := by omega\n      simp [inputTapes, this]\n\ntheorem liftTapes_castLE_verifierTapes (h : k ≤ k') (x w : List α) :\n    liftTapes (Embedding.castLE h) (verifierTapes x w)\n        (verifierTapes x w : Fin (k' + 1) → Tape α) = verifierTapes x w := by\n  apply liftTapes_eq\n  · intro j i hi\n    simp only [Embedding.castLE] at hi\n    split at hi\n    · cases hi; simp [verifierTapes]\n    · cases hi\n  · intro j _; rfl\n\ntheorem liftTapes_skipOne_inputTapes (h : k + 1 ≤ k') (y x w : List α) :\n    liftTapes (Embedding.skipOne h) (inputTapes y) (verifierTapes x w : Fin (k' + 1) → Tape α) =\n      verifierTapes y w := by\n  apply liftTapes_eq\n  · intro j i hi\n    simp only [Embedding.skipOne] at hi\n    by_cases h0 : j.val = 0\n    · simp only [h0, ↓reduceIte, Option.some.injEq] at hi\n      subst hi\n      simp [verifierTapes, inputTapes, h0]\n    · by_cases h1 : j.val = 1\n      · simp [h1] at hi\n      · simp only [h0, h1, ↓reduceIte] at hi\n        split at hi\n        · cases hi\n          have : j.val - 1 ≠ 0 := by omega\n          simp [verifierTapes, inputTapes, h0, h1, this]\n        · cases hi\n  · intro j hj\n    simp only [Embedding.skipOne] at hj\n    by_cases h0 : j.val = 0\n    · simp [h0] at hj\n    · by_cases h1 : j.val = 1\n      · simp [verifierTapes, h1]\n      · simp only [h0, h1, ↓reduceIte] at hj\n        split at hj\n        · cases hj\n        · simp [verifierTapes, h0, h1]\n\nend PvsNP\n"},"raw":"/r/pvsnp/raw/346abe536d053a5c1b61722fd9b9ea5144e9a85c/PvsNP/Machine/Lift.lean","jit":null,"api":"/v1/repos/pvsnp/blob?rev=346abe536d053a5c1b61722fd9b9ea5144e9a85c&path=PvsNP%2FMachine%2FLift.lean","api_url":"https://git.neoopensource.com/v1/repos/pvsnp/blob?rev=346abe536d053a5c1b61722fd9b9ea5144e9a85c&path=PvsNP%2FMachine%2FLift.lean"}