Documentation

Complexitylib.Models.TuringMachine.UTM.Internal.BodyMatch

Body machine: match-phase loop lemmas #

Closed-form loop lemmas for the body machine's match phase (design appendix, phases 0 and 3): the halt-check lockstep compare (hc1), the key-scan state-field compare (cmpQ), the six key-symbol cells (cmpS), the q'-field countdown copy (copyQ', trick 1), and the ten action cells (copyAct).

All lemmas follow the ghost-cell style of BodyInternal.lean: tape contents are given as cell functions with ▷-freeness hypotheses, every idle tape is exactly preserved (idle_tape_id frames), and scratch extension is stated as an update-on-an-interval of the ghost cells (as in dfCopy_loop).

theorem Complexity.TM.UTMBody.step_idle {c : Cfg 6 bodyTM.Q} (hne : c.state ≠ BodyQ.bodyDone) {q' : BodyQ} (h : bodyδ c.state c.input.read (fun (i : Fin 6) => (c.work i).read) c.output.read = idle q' c.input.read (fun (i : Fin 6) => (c.work i).read) c.output.read) (hin : c.input.read ≠ Γ.start) (hout : c.output.read ≠ Γ.start) (hwk : ∀ (i : Fin 6), (c.work i).read ≠ Γ.start) :
bodyTM.step c = some { state := q', input := c.input, work := c.work, output := c.output }

One step whose arm is idle q' changes only the control state, provided every head reads a non-▷ symbol.

Writing back a non-▷ read leaves the cells unchanged (write-only form of tape_readBackWrite_preserves).

theorem Complexity.TM.UTMBody.lockstep_agree_loop {cur : BodyQ} (hcur : cur ≠ BodyQ.bodyDone) (hδ : ∀ (iH : Γ) (wH : Fin 6 → Γ) (oH : Γ), wH stT ≠ Γ.blank → wH dsT ≠ Γ.blank → wH stT = wH dsT → bodyδ cur iH wH oH = act2 cur iH wH oH stT (readBackWrite (wH stT)) Dir3.right dsT (readBackWrite (wH dsT)) Dir3.right) (S W : ℕ → Γ) (hSns : ∀ (j : ℕ), 1 ≤ j → S j ≠ Γ.start) (hWns : ∀ (j : ℕ), 1 ≤ j → W j ≠ Γ.start) (n a b : ℕ) :
1 ≤ a → 1 ≤ b → (∀ j < n, S (a + j) = W (b + j) ∧ S (a + j) ≠ Γ.blank) → ∀ (c : Cfg 6 bodyTM.Q), c.state = cur → (c.work stT).cells = S → (c.work stT).head = a → (c.work dsT).cells = W → (c.work dsT).head = b → c.input.read ≠ Γ.start → c.output.read ≠ Γ.start → (∀ (i : Fin 6), i ≠ stT → i ≠ dsT → (c.work i).read ≠ Γ.start) → ∃ (c' : Cfg 6 bodyTM.Q), bodyTM.reachesIn n c c' ∧ c'.state = cur ∧ c'.work stT = { head := a + n, cells := S } ∧ c'.work dsT = { head := b + n, cells := W } ∧ c'.input = c.input ∧ c'.output = c.output ∧ ∀ (i : Fin 6), i ≠ stT → i ≠ dsT → c'.work i = c.work i

Generic lockstep compare loop for any state whose transition, on agreeing non-□ state/desc reads, steps both the state and desc heads right and stays in cur (the shape shared by hc1 and cmpQ f): while the state tape (ghost cells S, head from a) and the desc tape (ghost cells W, head from b) agree on n non-□ symbols, the machine advances both heads n cells in n steps. All cells and every other tape are exactly preserved.

theorem Complexity.TM.UTMBody.hc1_match_loop (S W : ℕ → Γ) (hSns : ∀ (j : ℕ), 1 ≤ j → S j ≠ Γ.start) (hWns : ∀ (j : ℕ), 1 ≤ j → W j ≠ Γ.start) (n a b : ℕ) (ha : 1 ≤ a) (hb : 1 ≤ b) (hagree : ∀ j < n, S (a + j) = W (b + j) ∧ S (a + j) ≠ Γ.blank) (hSbl : S (a + n) = Γ.blank) (hWbl : W (b + n) = Γ.blank) (c : Cfg 6 bodyTM.Q) (hst : c.state = BodyQ.hc1) (hcS : (c.work stT).cells = S) (hheadS : (c.work stT).head = a) (hcW : (c.work dsT).cells = W) (hheadW : (c.work dsT).head = b) (hin : c.input.read ≠ Γ.start) (hout : c.output.read ≠ Γ.start) (hoth : ∀ (i : Fin 6), i ≠ stT → i ≠ dsT → (c.work i).read ≠ Γ.start) :
∃ (c' : Cfg 6 bodyTM.Q), bodyTM.reachesIn (n + 1) c c' ∧ c'.state = BodyQ.haltRewS ∧ c'.work stT = { head := a + n, cells := S } ∧ c'.work dsT = { head := b + n, cells := W } ∧ c'.input = c.input ∧ c'.output = c.output ∧ ∀ (i : Fin 6), i ≠ stT → i ≠ dsT → c'.work i = c.work i

Halt-check compare, match case: from hc1 with the state tape (ghost S, head a) and desc tape (ghost W, head b) agreeing on n non-□ symbols and then both hitting □ simultaneously, reach haltRewS in n + 1 steps with heads at a + n and b + n. All cells and every other tape are exactly preserved.

theorem Complexity.TM.UTMBody.hc1_mismatch_loop (S W : ℕ → Γ) (hSns : ∀ (j : ℕ), 1 ≤ j → S j ≠ Γ.start) (hWns : ∀ (j : ℕ), 1 ≤ j → W j ≠ Γ.start) (n a b : ℕ) (ha : 1 ≤ a) (hb : 1 ≤ b) (hagree : ∀ j < n, S (a + j) = W (b + j) ∧ S (a + j) ≠ Γ.blank) (hmm1 : ¬(S (a + n) = Γ.blank ∧ W (b + n) = Γ.blank)) (hmm2 : ¬(S (a + n) ≠ Γ.blank ∧ W (b + n) ≠ Γ.blank ∧ S (a + n) = W (b + n))) (c : Cfg 6 bodyTM.Q) (hst : c.state = BodyQ.hc1) (hcS : (c.work stT).cells = S) (hheadS : (c.work stT).head = a) (hcW : (c.work dsT).cells = W) (hheadW : (c.work dsT).head = b) (hin : c.input.read ≠ Γ.start) (hout : c.output.read ≠ Γ.start) (hoth : ∀ (i : Fin 6), i ≠ stT → i ≠ dsT → (c.work i).read ≠ Γ.start) :
∃ (c' : Cfg 6 bodyTM.Q), bodyTM.reachesIn (n + 1) c c' ∧ c'.state = BodyQ.preRewS ∧ c'.work stT = { head := a + n, cells := S } ∧ c'.work dsT = { head := b + n, cells := W } ∧ c'.input = c.input ∧ c'.output = c.output ∧ ∀ (i : Fin 6), i ≠ stT → i ≠ dsT → c'.work i = c.work i

Halt-check compare, mismatch case: same lockstep compare as hc1_match_loop, but at offset n the two tapes disagree for the first time (not both □, and not both-non-□-and-equal): reach preRewS in n + 1 steps with heads at a + n and b + n. All cells and every other tape are exactly preserved.

theorem Complexity.TM.UTMBody.cmpQ_match_loop (f : VFlags) (S W : ℕ → Γ) (hSns : ∀ (j : ℕ), 1 ≤ j → S j ≠ Γ.start) (hWns : ∀ (j : ℕ), 1 ≤ j → W j ≠ Γ.start) (n a b : ℕ) (ha : 1 ≤ a) (hb : 1 ≤ b) (hagree : ∀ j < n, S (a + j) = W (b + j) ∧ S (a + j) ≠ Γ.blank) (hSbl : S (a + n) = Γ.blank) (c : Cfg 6 bodyTM.Q) (hst : c.state = BodyQ.cmpQ f) (hcS : (c.work stT).cells = S) (hheadS : (c.work stT).head = a) (hcW : (c.work dsT).cells = W) (hheadW : (c.work dsT).head = b) (hin : c.input.read ≠ Γ.start) (hout : c.output.read ≠ Γ.start) (hoth : ∀ (i : Fin 6), i ≠ stT → i ≠ dsT → (c.work i).read ≠ Γ.start) :
∃ (c' : Cfg 6 bodyTM.Q), bodyTM.reachesIn (n + 1) c c' ∧ c'.state = BodyQ.cmpS f 0 ∧ c'.work stT = { head := a + n, cells := S } ∧ c'.work dsT = { head := b + n, cells := W } ∧ c'.input = c.input ∧ c'.output = c.output ∧ ∀ (i : Fin 6), i ≠ stT → i ≠ dsT → c'.work i = c.work i

Key-field compare, match case: from cmpQ f with the state tape (ghost S, head a) and desc tape (ghost W, head b) agreeing on n non-□ symbols, and the state tape hitting □ at offset n (the state's key part is consumed — the exit fires regardless of the desc symbol), reach cmpS f 0 in n + 1 steps with heads at a + n and b + n (the exit step moves neither head). All cells and every other tape are exactly preserved.

theorem Complexity.TM.UTMBody.cmpQ_mismatch_loop (f : VFlags) (S W : ℕ → Γ) (hSns : ∀ (j : ℕ), 1 ≤ j → S j ≠ Γ.start) (hWns : ∀ (j : ℕ), 1 ≤ j → W j ≠ Γ.start) (n a b : ℕ) (ha : 1 ≤ a) (hb : 1 ≤ b) (hagree : ∀ j < n, S (a + j) = W (b + j) ∧ S (a + j) ≠ Γ.blank) (hSnb : S (a + n) ≠ Γ.blank) (hmm : ¬(W (b + n) ≠ Γ.blank ∧ S (a + n) = W (b + n))) (c : Cfg 6 bodyTM.Q) (hst : c.state = BodyQ.cmpQ f) (hcS : (c.work stT).cells = S) (hheadS : (c.work stT).head = a) (hcW : (c.work dsT).cells = W) (hheadW : (c.work dsT).head = b) (hin : c.input.read ≠ Γ.start) (hout : c.output.read ≠ Γ.start) (hoth : ∀ (i : Fin 6), i ≠ stT → i ≠ dsT → (c.work i).read ≠ Γ.start) :
∃ (c' : Cfg 6 bodyTM.Q), bodyTM.reachesIn (n + 1) c c' ∧ c'.state = BodyQ.skipSeg f ∧ c'.work stT = { head := a + n, cells := S } ∧ c'.work dsT = { head := b + n, cells := W } ∧ c'.input = c.input ∧ c'.output = c.output ∧ ∀ (i : Fin 6), i ≠ stT → i ≠ dsT → c'.work i = c.work i

Key-field compare, mismatch case: same lockstep compare as cmpQ_match_loop, but at offset n the state symbol is non-□ and fails to match the desc symbol (desc □ or different): reach skipSeg f in n + 1 steps with heads at a + n and b + n (the exit step moves neither head). All cells and every other tape are exactly preserved.

theorem Complexity.TM.UTMBody.cmpS_match_loop (f : VFlags) (v0 v1 v2 : Γ) (W V : ℕ → Γ) (hWns : ∀ (j : ℕ), 1 ≤ j → W j ≠ Γ.start) (hVns : ∀ (j : ℕ), 1 ≤ j → V j ≠ Γ.start) (k : ℕ) (idx : Fin 6) :
↑idx + k = 5 → ∀ (a b : ℕ), 1 ≤ a → 1 ≤ b → (∀ j ≤ k, ∀ (hj6 : ↑idx + j < 6), W (b + j) = keyCell f v0 v1 v2 ⟨↑idx + j, hj6⟩ ∧ W (b + j) ≠ Γ.blank) → ∀ (c : Cfg 6 bodyTM.Q), c.state = BodyQ.cmpS f idx → (c.work vIn).read = v0 → (c.work vWk).read = v1 → (c.work vOut).read = v2 → (c.work stT).cells = V → (c.work stT).head = a → (c.work dsT).cells = W → (c.work dsT).head = b → c.input.read ≠ Γ.start → c.output.read ≠ Γ.start → (∀ (i : Fin 6), i ≠ stT → i ≠ dsT → (c.work i).read ≠ Γ.start) → ∃ (c' : Cfg 6 bodyTM.Q), bodyTM.reachesIn (k + 1) c c' ∧ c'.state = BodyQ.copyQ' f ∧ c'.work stT = { head := a - 1, cells := V } ∧ c'.work dsT = { head := b + k + 1, cells := W } ∧ c'.input = c.input ∧ c'.output = c.output ∧ ∀ (i : Fin 6), i ≠ stT → i ≠ dsT → c'.work i = c.work i

Key-symbol compare, match case: from cmpS f idx with the desc tape (ghost W, head b) holding the expected keyCell symbols (all non-□) at offsets 0..k (idx.val + k = 5), reach copyQ' f in k + 1 steps with the desc head at b + k + 1. The three virtual tapes are stationary, so their live reads v0/v1/v2 stay valid throughout. On the final step the state head (ghost V, head a) takes trick 1's pre-step one cell left. All cells and every other tape are exactly preserved.

theorem Complexity.TM.UTMBody.cmpS_mismatch_loop (f : VFlags) (v0 v1 v2 : Γ) (W : ℕ → Γ) (hWns : ∀ (j : ℕ), 1 ≤ j → W j ≠ Γ.start) (n : ℕ) (idx : Fin 6) :
↑idx + n ≤ 5 → ∀ (b : ℕ), 1 ≤ b → (∀ j < n, ∀ (hj6 : ↑idx + j < 6), W (b + j) = keyCell f v0 v1 v2 ⟨↑idx + j, hj6⟩ ∧ W (b + j) ≠ Γ.blank) → (∀ (hn6 : ↑idx + n < 6), ¬(W (b + n) = keyCell f v0 v1 v2 ⟨↑idx + n, hn6⟩ ∧ W (b + n) ≠ Γ.blank)) → ∀ (c : Cfg 6 bodyTM.Q), c.state = BodyQ.cmpS f idx → (c.work vIn).read = v0 → (c.work vWk).read = v1 → (c.work vOut).read = v2 → (c.work dsT).cells = W → (c.work dsT).head = b → c.input.read ≠ Γ.start → c.output.read ≠ Γ.start → (∀ (i : Fin 6), i ≠ dsT → (c.work i).read ≠ Γ.start) → ∃ (c' : Cfg 6 bodyTM.Q), bodyTM.reachesIn (n + 1) c c' ∧ c'.state = BodyQ.skipSeg f ∧ c'.work dsT = { head := b + n, cells := W } ∧ c'.input = c.input ∧ c'.output = c.output ∧ ∀ (i : Fin 6), i ≠ dsT → c'.work i = c.work i

Key-symbol compare, mismatch case: from cmpS f idx with the desc tape (ghost W, head b) matching the expected keyCell symbols at offsets < n but failing at offset n (idx.val + n ≤ 5; the cell differs from the expected symbol or is □), reach skipSeg f in n + 1 steps with the desc head at b + n. The state head does not move. All cells and every other tape are exactly preserved.

theorem Complexity.TM.UTMBody.copyQ'_copy_loop (f : VFlags) (S W : ℕ → Γ) (hS0 : S 0 = Γ.start) (hSns : ∀ (j : ℕ), 1 ≤ j → S j ≠ Γ.start) (hWns : ∀ (j : ℕ), 1 ≤ j → W j ≠ Γ.start) (s : ℕ) (E : ℕ → Γ) :
(∀ (j : ℕ), 1 ≤ j → E j ≠ Γ.start) → ∀ (b e : ℕ), 1 ≤ b → 1 ≤ e → (∀ j < s, W (b + j) ≠ Γ.blank) → ∀ (c : Cfg 6 bodyTM.Q), c.state = BodyQ.copyQ' f → (c.work stT).cells = S → (c.work stT).head = s → (c.work dsT).cells = W → (c.work dsT).head = b → (c.work scT).cells = E → (c.work scT).head = e → c.input.read ≠ Γ.start → c.output.read ≠ Γ.start → (∀ (i : Fin 6), i ≠ stT → i ≠ dsT → i ≠ scT → (c.work i).read ≠ Γ.start) → ∃ (c' : Cfg 6 bodyTM.Q), bodyTM.reachesIn (s + 1) c c' ∧ c'.state = BodyQ.copyAct f 0 ∧ c'.work stT = { head := 1, cells := S } ∧ c'.work dsT = { head := b + s, cells := W } ∧ c'.work scT = { head := e + s, cells := fun (j : ℕ) => if e ≤ j ∧ j < e + s then W (b + (j - e)) else E j } ∧ c'.input = c.input ∧ c'.output = c.output ∧ ∀ (i : Fin 6), i ≠ stT → i ≠ dsT → i ≠ scT → c'.work i = c.work i

q'-field copy, clean case (trick 1): from copyQ' f with the state tape (ghost S, ▷ at cell 0 only, head counting down from s), the desc tape (ghost W, head b) holding s non-□ cells, and the scratch tape (ghost E, frontier e), copy the s desc cells to scratch cells e..e+s-1 while the state head walks left; when it hits ▷ the forced bounce lands it at cell 1 and the machine enters copyAct f 0 — s + 1 steps in total. State and desc cells and every other tape are exactly preserved.

theorem Complexity.TM.UTMBody.copyQ'_blank_loop (f : VFlags) (S W : ℕ → Γ) (hSns : ∀ (j : ℕ), 1 ≤ j → S j ≠ Γ.start) (hWns : ∀ (j : ℕ), 1 ≤ j → W j ≠ Γ.start) (n : ℕ) (E : ℕ → Γ) :
(∀ (j : ℕ), 1 ≤ j → E j ≠ Γ.start) → ∀ (s b e : ℕ), n < s → 1 ≤ b → 1 ≤ e → (∀ j < n, W (b + j) ≠ Γ.blank) → W (b + n) = Γ.blank → ∀ (c : Cfg 6 bodyTM.Q), c.state = BodyQ.copyQ' f → (c.work stT).cells = S → (c.work stT).head = s → (c.work dsT).cells = W → (c.work dsT).head = b → (c.work scT).cells = E → (c.work scT).head = e → c.input.read ≠ Γ.start → c.output.read ≠ Γ.start → (∀ (i : Fin 6), i ≠ stT → i ≠ dsT → i ≠ scT → (c.work i).read ≠ Γ.start) → ∃ (c' : Cfg 6 bodyTM.Q), bodyTM.reachesIn (n + 1) c c' ∧ c'.state = BodyQ.skipSeg f ∧ c'.work stT = { head := s - n, cells := S } ∧ c'.work dsT = { head := b + n, cells := W } ∧ c'.work scT = { head := e + n, cells := fun (j : ℕ) => if e ≤ j ∧ j < e + n then W (b + (j - e)) else E j } ∧ c'.input = c.input ∧ c'.output = c.output ∧ ∀ (i : Fin 6), i ≠ stT → i ≠ dsT → i ≠ scT → c'.work i = c.work i

q'-field copy, early-□ case: from copyQ' f counting down from state head s, the desc tape hits □ after only n < s copied cells: the machine exits to skipSeg f in n + 1 steps (the □-exit step is an idle transition) with the state head at s - n, the desc head at b + n (on the □), and scratch extended by the n copied cells. State and desc cells and every other tape are exactly preserved.

theorem Complexity.TM.UTMBody.copyAct_copy_loop (f : VFlags) (W : ℕ → Γ) (hWns : ∀ (j : ℕ), 1 ≤ j → W j ≠ Γ.start) (k : ℕ) (j : Fin 10) :
↑j + k = 9 → ∀ (E : ℕ → Γ), (∀ (i : ℕ), 1 ≤ i → E i ≠ Γ.start) → ∀ (b e : ℕ), 1 ≤ b → 1 ≤ e → (∀ i ≤ k, W (b + i) ≠ Γ.blank) → ∀ (c : Cfg 6 bodyTM.Q), c.state = BodyQ.copyAct f j → (c.work dsT).cells = W → (c.work dsT).head = b → (c.work scT).cells = E → (c.work scT).head = e → c.input.read ≠ Γ.start → c.output.read ≠ Γ.start → (∀ (i : Fin 6), i ≠ dsT → i ≠ scT → (c.work i).read ≠ Γ.start) → ∃ (c' : Cfg 6 bodyTM.Q), bodyTM.reachesIn (k + 1) c c' ∧ c'.state = BodyQ.appRewScr f ∧ c'.work dsT = { head := b + k + 1, cells := W } ∧ c'.work scT = { head := e + k + 1, cells := fun (i : ℕ) => if e ≤ i ∧ i < e + k + 1 then W (b + (i - e)) else E i } ∧ c'.input = c.input ∧ c'.output = c.output ∧ ∀ (i : Fin 6), i ≠ dsT → i ≠ scT → c'.work i = c.work i

Action-cell copy, clean case: from copyAct f j with the desc tape (ghost W, head b) holding k + 1 non-□ cells (j.val + k = 9, so k + 1 = 10 - j.val) and the scratch tape (ghost E, frontier e), copy the remaining action cells to scratch cells e..e+k and enter appRewScr f in k + 1 steps, with the desc head at b + k + 1 and the scratch frontier at e + k + 1. Desc cells and every other tape are exactly preserved.

theorem Complexity.TM.UTMBody.copyAct_blank_loop (f : VFlags) (W : ℕ → Γ) (hWns : ∀ (j : ℕ), 1 ≤ j → W j ≠ Γ.start) (n : ℕ) (j : Fin 10) :
↑j + n ≤ 9 → ∀ (E : ℕ → Γ), (∀ (i : ℕ), 1 ≤ i → E i ≠ Γ.start) → ∀ (b e : ℕ), 1 ≤ b → 1 ≤ e → (∀ i < n, W (b + i) ≠ Γ.blank) → W (b + n) = Γ.blank → ∀ (c : Cfg 6 bodyTM.Q), c.state = BodyQ.copyAct f j → (c.work dsT).cells = W → (c.work dsT).head = b → (c.work scT).cells = E → (c.work scT).head = e → c.input.read ≠ Γ.start → c.output.read ≠ Γ.start → (∀ (i : Fin 6), i ≠ dsT → i ≠ scT → (c.work i).read ≠ Γ.start) → ∃ (c' : Cfg 6 bodyTM.Q), bodyTM.reachesIn (n + 1) c c' ∧ c'.state = BodyQ.skipSeg f ∧ c'.work dsT = { head := b + n, cells := W } ∧ c'.work scT = { head := e + n, cells := fun (i : ℕ) => if e ≤ i ∧ i < e + n then W (b + (i - e)) else E i } ∧ c'.input = c.input ∧ c'.output = c.output ∧ ∀ (i : Fin 6), i ≠ dsT → i ≠ scT → c'.work i = c.work i

Action-cell copy, early-□ case: from copyAct f j, the desc tape hits □ at offset n (j.val + n ≤ 9) after n copied cells: the machine exits to skipSeg f in n + 1 steps (the □-exit step is an idle transition) with the desc head at b + n (on the □) and scratch extended by the n copied cells. Desc cells and every other tape are exactly preserved.