# PvsNP/Machine/Tape.lean @ 989456136115376209fd6a56be8e8c3f89c3d9d4

````
/-!
# Tapes

A tape is a bi-infinite row of cells indexed by `Int`, each blank (`none`) or holding a symbol of the
alphabet `α`, with a head position. Tapes are functions, so two tapes are equal exactly when they have
the same head and the same contents: no trailing-blank bookkeeping.

The machines of this project use `Option α` as their tape alphabet: input symbols plus the blank.
-/

namespace PvsNP

/-- A head movement. -/
inductive Dir where
  | left
  | stay
  | right
  deriving DecidableEq, Repr

/-- A tape: head position and contents. `none` is the blank. -/
structure Tape (α : Type) where
  head : Int
  cells : Int → Option α

namespace Tape

variable {α : Type}

/-- The all-blank tape, head at `0`. -/
def blank : Tape α := ⟨0, fun _ => none⟩

/-- The tape holding the word `w` in cells `0 … |w| - 1`, blank elsewhere, head at `0`. -/
def ofWord (w : List α) : Tape α :=
  ⟨0, fun i => if 0 ≤ i then w[i.toNat]? else none⟩

/-- The symbol under the head. -/
def read (t : Tape α) : Option α := t.cells t.head

/-- Overwrite the cell under the head. -/
def write (t : Tape α) (s : Option α) : Tape α :=
  ⟨t.head, fun i => if i = t.head then s else t.cells i⟩

/-- Move the head one cell (or not at all). -/
def move (t : Tape α) : Dir → Tape α
  | .left => ⟨t.head - 1, t.cells⟩
  | .stay => t
  | .right => ⟨t.head + 1, t.cells⟩

@[simp] theorem move_stay (t : Tape α) : t.move .stay = t := rfl

/-- Writing back the symbol just read changes nothing. -/
@[simp] theorem write_read (t : Tape α) : t.write t.read = t := by
  cases t with
  | mk h c =>
    simp only [write, read, Tape.mk.injEq, true_and]
    funext i
    by_cases hi : i = h
    · subst hi; simp
    · simp [hi]

@[simp] theorem ofWord_nil : (ofWord [] : Tape α) = blank := by
  simp only [ofWord, blank, List.getElem?_nil, ite_self]

theorem read_ofWord (w : List α) : (ofWord w).read = w.head? := by
  cases w <;> simp [ofWord, read]

end Tape

end PvsNP

````

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