{"ok":true,"data":{"commit":"f0679799a068ef3eadad81d312dbccca601dc438","path":"PvsNP/Machine/Tape.lean","oid":"1565fb94394b9e0f47b9149f2b47df54fc620e76","size":1990,"range_start":0,"range_end":1990,"truncated":false,"binary":false,"encoding":"utf8","content":"/-!\n# Tapes\n\nA tape is a bi-infinite row of cells indexed by `Int`, each blank (`none`) or holding a symbol of the\nalphabet `α`, with a head position. Tapes are functions, so two tapes are equal exactly when they have\nthe same head and the same contents: no trailing-blank bookkeeping.\n\nThe machines of this project use `Option α` as their tape alphabet: input symbols plus the blank.\n-/\n\nnamespace PvsNP\n\n/-- A head movement. -/\ninductive Dir where\n  | left\n  | stay\n  | right\n  deriving DecidableEq, Repr\n\n/-- A tape: head position and contents. `none` is the blank. -/\nstructure Tape (α : Type) where\n  head : Int\n  cells : Int → Option α\n\nnamespace Tape\n\nvariable {α : Type}\n\n/-- The all-blank tape, head at `0`. -/\ndef blank : Tape α := ⟨0, fun _ => none⟩\n\n/-- The tape holding the word `w` in cells `0 … |w| - 1`, blank elsewhere, head at `0`. -/\ndef ofWord (w : List α) : Tape α :=\n  ⟨0, fun i => if 0 ≤ i then w[i.toNat]? else none⟩\n\n/-- The symbol under the head. -/\ndef read (t : Tape α) : Option α := t.cells t.head\n\n/-- Overwrite the cell under the head. -/\ndef write (t : Tape α) (s : Option α) : Tape α :=\n  ⟨t.head, fun i => if i = t.head then s else t.cells i⟩\n\n/-- Move the head one cell (or not at all). -/\ndef move (t : Tape α) : Dir → Tape α\n  | .left => ⟨t.head - 1, t.cells⟩\n  | .stay => t\n  | .right => ⟨t.head + 1, t.cells⟩\n\n@[simp] theorem move_stay (t : Tape α) : t.move .stay = t := rfl\n\n/-- Writing back the symbol just read changes nothing. -/\n@[simp] theorem write_read (t : Tape α) : t.write t.read = t := by\n  cases t with\n  | mk h c =>\n    simp only [write, read, Tape.mk.injEq, true_and]\n    funext i\n    by_cases hi : i = h\n    · subst hi; simp\n    · simp [hi]\n\n@[simp] theorem ofWord_nil : (ofWord [] : Tape α) = blank := by\n  simp only [ofWord, blank, List.getElem?_nil, ite_self]\n\ntheorem read_ofWord (w : List α) : (ofWord w).read = w.head? := by\n  cases w <;> simp [ofWord, read]\n\nend Tape\n\nend PvsNP\n"},"meta":{"request_id":"18959848-401","took_us":130,"limits":{"limit":600,"remaining":144,"window_ms":60000,"reset_ms":45550,"cost":1,"streams":{"limit":16,"open":0}}}}