118eb9c9 / PvsNP / Machine / Lift.lean

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