Skip to content

feat(Foundations/Data): Function view for Turing tapes#499

Open
crei wants to merge 1 commit intoleanprover:mainfrom
crei:tapes_as_functions
Open

feat(Foundations/Data): Function view for Turing tapes#499
crei wants to merge 1 commit intoleanprover:mainfrom
crei:tapes_as_functions

Conversation

@crei
Copy link
Copy Markdown

@crei crei commented Apr 18, 2026

The central addition in this PR is BiTape.get: it allows (integer) index-based access to the Turing tape cells.

In addition, adds several lemmas that make it easier to work with BiTapes by viewing them as functions ℤ → Option Symbol. A sequence of moves and writes translates into a sequence of Function.update and thus maps everything into a domain that simp and grind can already nicely work with.

@BoltonBailey
Copy link
Copy Markdown
Contributor

Seems like a good addition! My only comment is: Should we add tags like @[ext] or grind = to some of these to improve integration with tactics?

Copy link
Copy Markdown
Collaborator

@chenson2018 chenson2018 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks straightforward, thanks! Just some style questions.

Comment on lines +81 to +86
/-- Returns the tape symbol at positon `p` relative to the head, where
positive numbers are right of the head and negative are left of the head. -/
def get (t : BiTape Symbol) : ℤ → Option Symbol
| Int.ofNat 0 => t.head
| Int.ofNat (Nat.succ p') => t.right.toList[p']?.getD none
| Int.negSucc p' => t.left.toList[p']?.getD none
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usually the notation for these matches is used, right? I.e.

Suggested change
/-- Returns the tape symbol at positon `p` relative to the head, where
positive numbers are right of the head and negative are left of the head. -/
def get (t : BiTape Symbol) : ℤ → Option Symbol
| Int.ofNat 0 => t.head
| Int.ofNat (Nat.succ p') => t.right.toList[p']?.getD none
| Int.negSucc p' => t.left.toList[p']?.getD none
open scoped Int in
/-- Returns the tape symbol at positon `p` relative to the head, where
positive numbers are right of the head and negative are left of the head. -/
def get (t : BiTape Symbol) : ℤ → Option Symbol
| 0 => t.head
| (p' + 1 : Nat) => t.right.toList[p']?.getD none
| -[p'+1] => t.left.toList[p']?.getD none

obtain ⟨head₂, left₂, right₂⟩ := t₂
have h_head : head₁ = head₂ := by simpa [get] using h_get_eq 0
have h_right : right₁ = right₂ := by
apply StackTape.ext_get
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should StackTape.ext_get and this theorem have ext attributes?

have h_right : right₁ = right₂ := by
apply StackTape.ext_get
intro p
simpa [get] using h_get_eq p.succ
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, is notation preferred?

Suggested change
simpa [get] using h_get_eq p.succ
simpa [get] using h_get_eq (p + 1)

apply StackTape.ext_get
intro p
simpa [get] using h_get_eq (Int.negSucc p)
simp only [h_head, h_left, h_right]
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to squeeze terminal simp. Can also just be grind if you prefer.

simp [StackTape.head_eq_getD]
| Int.ofNat 1 => simp
| Int.ofNat (n + 2) =>
rw [show Int.ofNat (n + 2) - 1 = Int.ofNat (n + 1) from by grind]
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
rw [show Int.ofNat (n + 2) - 1 = Int.ofNat (n + 1) from by grind]
rw [show Int.ofNat (n + 2) - 1 = Int.ofNat (n + 1) by lia]

unfold move_right get
match p with
| Int.ofNat n =>
rw [show Int.ofNat n + 1 = Int.ofNat (n + 1) from by grind]
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
rw [show Int.ofNat n + 1 = Int.ofNat (n + 1) from by grind]
rw [show Int.ofNat n + 1 = Int.ofNat (n + 1) by lia]

cases d with
| none => simp
| some d =>
cases d <;> simp [move, show p + (-1 : ℤ) = p - 1 from by omega]
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a fan of nested simp and grind. Here it seems particularly not needed because you can write

Suggested change
cases d <;> simp [move, show p + (-1 : ℤ) = p - 1 from by omega]
cases d
· grind [move, get_move_left]
· grind [move, get_move_right]

maybe some of these should be simp/grind =?

induction n generalizing t p with
| zero => simp
| succ n ih =>
have : p - n - 1 = p - (n + 1) := by grind
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
have : p - n - 1 = p - (n + 1) := by grind
have : p - n - 1 = p - (n + 1) := by lia

funext p
match p with
| Int.ofNat 0 => simp
| Int.ofNat (n + 1) => simp; grind
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

simp; grind is technically allowed, but not needed here

Suggested change
| Int.ofNat (n + 1) => simp; grind
| Int.ofNat (n + 1) => grind

@chenson2018 chenson2018 self-assigned this Apr 18, 2026
@chenson2018 chenson2018 changed the title feat(Foundations/Data): Function view for Turing tapes. feat(Foundations/Data): Function view for Turing tapes Apr 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants