Skip to content

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

Open
crei wants to merge 3 commits intoleanprover:mainfrom
crei:tapes_as_functions
Open

feat(Foundations/Data): Function view for Turing tapes#499
crei wants to merge 3 commits 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 thread Cslib/Foundations/Data/BiTape.lean Outdated
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?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

After adding the attributes to StackTape.ext and StackTape.ext_get, I can use ext1 p here, but how can I be sure that the ext tactic uses StackTape.ext_get instead of StackTape.ext?

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.

You're right, even if you remove both it picks up another usage of ext, so maybe this isn't helpful?

Comment thread Cslib/Foundations/Data/BiTape.lean Outdated
Comment thread Cslib/Foundations/Data/BiTape.lean Outdated
Comment thread Cslib/Foundations/Data/BiTape.lean Outdated
Comment thread Cslib/Foundations/Data/BiTape.lean Outdated
Comment thread Cslib/Foundations/Data/BiTape.lean Outdated
Comment thread Cslib/Foundations/Data/BiTape.lean Outdated
Comment thread Cslib/Foundations/Data/BiTape.lean Outdated
@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
crei and others added 2 commits April 20, 2026 11:38
Co-authored-by: Chris Henson <46805207+chenson2018@users.noreply.github.com>
@crei crei requested a review from chenson2018 April 21, 2026 08:58
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.

A couple more style comments.

Comment on lines +94 to +105
obtain ⟨head₁, left₁, right₁⟩ := t₁
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
intro p
simpa [get] using h_get_eq (p + 1)
have h_left : left₁ = left₂ := by
apply StackTape.ext_get
intro p
simpa [get] using h_get_eq (Int.negSucc p)
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.

It seems clearer to use congr here directly rather than have grind infer this:

Suggested change
obtain ⟨head₁, left₁, right₁⟩ := t₁
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
intro p
simpa [get] using h_get_eq (p + 1)
have h_left : left₁ = left₂ := by
apply StackTape.ext_get
intro p
simpa [get] using h_get_eq (Int.negSucc p)
grind
cases t₁
congr
· simpa [get] using h_get_eq 0
· apply StackTape.ext_get
intro p
simpa [get] using h_get_eq (Int.negSucc p)
· apply StackTape.ext_get
intro p
simpa [get] using h_get_eq (p + 1)

Comment on lines +159 to +160
rw [show Int.ofNat 0 - 1 = Int.negSucc 0 from rfl]
simp [StackTape.head_eq_getD]
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.

For these where you have something like rw [show ... from rfl[, you can also just do

Suggested change
rw [show Int.ofNat 0 - 1 = Int.negSucc 0 from rfl]
simp [StackTape.head_eq_getD]
simp [StackTape.head_eq_getD]
rfl

Comment on lines +154 to +155
lemma get_move_left (t : BiTape Symbol) (p : ℤ) :
(t.move_left).get p = t.get (p - 1) := by
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.

Fir on one line, and some parentheses can be removed:

Suggested change
lemma get_move_left (t : BiTape Symbol) (p : ℤ) :
(t.move_left).get p = t.get (p - 1) := by
lemma get_move_left (t : BiTape Symbol) (p : ℤ) : t.move_left.get p = t.get (p - 1) := by

Comment on lines +168 to +169
lemma get_move_right (t : BiTape Symbol) (p : ℤ) :
(t.move_right).get p = t.get (p + 1) := by
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
lemma get_move_right (t : BiTape Symbol) (p : ℤ) :
(t.move_right).get p = t.get (p + 1) := by
lemma get_move_right (t : BiTape Symbol) (p : ℤ) : t.move_right.get p = t.get (p + 1) := by

unfold move_right get
match p with
| Int.ofNat n =>
rw [show Int.ofNat n + 1 = Int.ofNat (n + 1) by lia]
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.

Is this the preferred way to do these proofs in Mathlib? I rarely work with integers, but this seems very low-level.

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