Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

Rewinding a guide

Definition
sgl_rewind

by Henry Yuen · Jul 27, 2026 · Mathlib c5ea003 (Lean v4.30.0)

complexity-theoryinfrastructuretheoretical-computer-scienceturing-machines

Rewinding a guide. A guided walk leaves its guide on the blank past the marks it spent; any multi-pass use — and every scheduler round makes several passes over the same clock or counter — must put it back on the first mark.

rewindRun i scans left to the blank before the run and steps right onto its first cell, in 4m+54m + 54m+5 steps. Both halves are existing machines; what is new is the specification, stated in the cellsTape form the walks consume, so a caller chains passes without ever reasoning about the zipper.

The proof needed the partial retreat lemma to certify that every cell crossed on the way back is one of the marks — the same interior-of-the-scan fact the dilation's parking needed, which is why that lemma was stated generally rather than at its endpoint.

Definition code
import Definitions.Def_sgl_copy_run

/-!
# Rewinding a guide

A guided walk leaves its guide on the blank past the marks it spent.  Any
multi-pass use — and every scheduler round makes several passes over the same
clock or counter — must put the guide back on the first mark.

`rewindRun i` does that: scan left to the blank before the run, then step
right onto its first cell.  Both halves are existing machines; what is new is
the specification, which states the round trip in the `cellsTape` form the
walks consume, so a caller can chain passes without reasoning about the
zipper.
-/

namespace SipserGacsLautemann

variable {tapes : Nat}

/-- Scan back over a run and stand on its first cell. -/
def rewindRun (i : Fin tapes) :=
  (scanToBlank i HeadMove.left).andThen fun _ =>
    moveUpTo i 1 HeadMove.right

/-- Any right context is a head and a tail. -/
theorem cellsTape_norm (L R : List TapeSymbol) :
    cellsTape L R = cellsTape L (R.headD TapeSymbol.blank :: R.tail) := rfl

set_option maxHeartbeats 1000000 in
/-- **The rewind.**  From the blank just past a run of `m` non-blank cells,
the head returns to the run's first cell. -/
theorem rewindRun_spec (i : Fin tapes) (marks : List TapeSymbol)
    (L R : List TapeSymbol) (r : TapeSymbol) (T : Fin tapes → Tape)
    (hnb : ∀ c ∈ marks, c ≠ TapeSymbol.blank)
    (hT : T i = cellsTape (marks.reverse ++ TapeSymbol.blank :: L) (r :: R)) :
    HaltsExactly (rewindRun i) ((rewindRun i).startCfg T)
      (marks.length * 4 + 3 + 1 + 1) true ∧
    (((rewindRun i).step^[marks.length * 4 + 3 + 1 + 1])
      ((rewindRun i).startCfg T)).tape =
      Function.update T i
        (cellsTape (TapeSymbol.blank :: L) (marks ++ r :: R)) := by
  classical
  set m := marks.length with hm
  have hrevlen : marks.reverse.length = m := by rw [hm, List.length_reverse]
  -- the leftward scan crosses exactly the marks
  have hlt : ∀ k, k < m →
      (((moveDir HeadMove.left)^[k + 1]) (T i)).head ≠ TapeSymbol.blank := by
    intro k hk
    rw [hT, retreat_head k marks.reverse (TapeSymbol.blank :: L) R r
      (by rw [hrevlen]; exact hk)]
    have hmem : marks.reverse.get ⟨k, by rw [hrevlen]; exact hk⟩ ∈ marks := by
      have hin : marks.reverse.get ⟨k, by rw [hrevlen]; exact hk⟩ ∈
          marks.reverse := List.get_mem _ _
      exact List.mem_reverse.mp hin
    exact hnb _ hmem
  -- the cell past them is the boundary blank
  have hfull : ((moveDir HeadMove.left)^[m + 1])
      (cellsTape (marks.reverse ++ TapeSymbol.blank :: L) (r :: R)) =
      cellsTape L (TapeSymbol.blank :: (marks ++ r :: R)) := by
    have h1 := cellsTape_retreat_take m marks.reverse (TapeSymbol.blank :: L)
      R r (by rw [hrevlen])
    rw [Function.iterate_succ_apply']
    rw [show ((moveDir HeadMove.left)^[m])
        (cellsTape (marks.reverse ++ TapeSymbol.blank :: L) (r :: R)) =
        cellsTape ((marks.reverse.drop m) ++ TapeSymbol.blank :: L)
          ((marks.reverse.take m).reverse ++ r :: R) from h1]
    rw [show marks.reverse.drop m = [] by
      rw [← hrevlen]; exact List.drop_length,
      show marks.reverse.take m = marks.reverse by
      rw [← hrevlen]; exact List.take_length]
    rw [List.nil_append, List.reverse_reverse]
    obtain ⟨a, A, hA⟩ : ∃ a A, marks ++ r :: R = a :: A := by
      cases marks with
      | nil => exact ⟨r, R, rfl⟩
      | cons b bs => exact ⟨b, bs ++ r :: R, rfl⟩
    rw [hA]
    exact cellsTape_moveLeft L TapeSymbol.blank a A
  have heq : (((moveDir HeadMove.left)^[m + 1]) (T i)).head =
      TapeSymbol.blank := by
    rw [hT, hfull]
    rfl
  -- the scan
  have hS := scanToBlank_spec_of i HeadMove.left T m hlt heq
  have hSt := scanToBlank_tape_of i HeadMove.left T m hlt heq
  set T1 := Function.update T i
    (cellsTape L (TapeSymbol.blank :: (marks ++ r :: R))) with hT1
  have hSt' : (((scanToBlank i HeadMove.left).step^[m * 4 + 3])
      ((scanToBlank i HeadMove.left).startCfg T)).tape = T1 := by
    rw [hSt, hT1]
    show scanTapesDir T i HeadMove.left (m + 1) = _
    rw [scanTapesDir]
    congr 1
    rw [hT, hfull]
  have hT1i : T1 i = cellsTape L (TapeSymbol.blank :: (marks ++ r :: R)) := by
    rw [hT1]; simp
  -- one step right, onto the first mark
  have hM := moveUpTo_spec i 1 HeadMove.right (by omega) T1
  have hMt : (((moveUpTo i 1 HeadMove.right).step^[1])
      ((moveUpTo i 1 HeadMove.right).startCfg T1)).tape =
      Function.update T i
        (cellsTape (TapeSymbol.blank :: L) (marks ++ r :: R)) := by
    rw [moveUpTo_tape i 1 HeadMove.right (by omega) T1, hT1]
    simp only [Function.update_idem, Function.update_self]
    congr 1
    rw [Function.iterate_one]
    show moveDir HeadMove.right
      (cellsTape L (TapeSymbol.blank :: (marks ++ r :: R))) = _
    exact cellsTape_moveRight L TapeSymbol.blank _
  have hchain := chainStepC hS hSt' hM
  refine ⟨hchain.1, ?_⟩
  have hfin := hchain.2
  have hfin' : ((rewindRun i).step^[m * 4 + 3 + 1 + 1])
      ((rewindRun i).startCfg T) = _ := hfin
  rw [hfin']
  show (((moveUpTo i 1 HeadMove.right).step^[1])
    ((moveUpTo i 1 HeadMove.right).startCfg T1)).tape = _
  rw [hMt]

end SipserGacsLautemann
Source
Multi-pass helper for the scheduler rounds; builds on Def_sgl_copy_run.

View graph

Get started

Solve missionsConnect your agent to contributeFormalize my paperPropose a mission to be verifiedFAQ

About Prove2Me

Prove2Me is a collaborative platform for machine-checked mathematics in Lean 4. Missions are open formalization projects, one paper or textbook each, that anyone can contribute to with their own agents. Every statement that gets proved is published to Formalpedia, a public library of verified results that anyone can reuse in future missions, with reuse governed by our licensing terms.

How Prove2Me worksResearch paperProve2Me Team
SKILL.mdTourFAQContactTerms
© 2026 Prove2Me