-
Notifications
You must be signed in to change notification settings - Fork 135
feat(Foundations/Data): Function view for Turing tapes #499
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -78,6 +78,33 @@ def mk₁ (l : List Symbol) : BiTape Symbol := | |||||||||||||||||||||||||||||||||||||||||||
| | [] => ∅ | ||||||||||||||||||||||||||||||||||||||||||||
| | h :: t => { head := some h, left := ∅, right := StackTape.map_some t } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| 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. -/ | ||||||||||||||||||||||||||||||||||||||||||||
| @[scoped grind] | ||||||||||||||||||||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| /-- Two tapes are equal if and only if their `get` functions are equal. This allows to view | ||||||||||||||||||||||||||||||||||||||||||||
| tapes as functions `ℤ → Option Symbol`. -/ | ||||||||||||||||||||||||||||||||||||||||||||
| @[ext] | ||||||||||||||||||||||||||||||||||||||||||||
| lemma ext_get (t₁ t₂ : BiTape Symbol) (h_get_eq : ∀ p, t₁.get p = t₂.get p) : t₁ = t₂ := by | ||||||||||||||||||||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+94
to
+105
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems clearer to use
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| section Move | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| /-- | ||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -114,13 +141,81 @@ lemma move_left_move_right (t : BiTape Symbol) : t.move_left.move_right = t := b | |||||||||||||||||||||||||||||||||||||||||||
| lemma move_right_move_left (t : BiTape Symbol) : t.move_right.move_left = t := by | ||||||||||||||||||||||||||||||||||||||||||||
| simp [move_left, move_right] | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| /-- Translate an optional direction into a head movement offset, where the positive | ||||||||||||||||||||||||||||||||||||||||||||
| direction is to the right. -/ | ||||||||||||||||||||||||||||||||||||||||||||
| @[scoped grind] | ||||||||||||||||||||||||||||||||||||||||||||
| def optionDirToInt (d : Option Dir) : ℤ := | ||||||||||||||||||||||||||||||||||||||||||||
| match d with | ||||||||||||||||||||||||||||||||||||||||||||
| | none => 0 | ||||||||||||||||||||||||||||||||||||||||||||
| | some .left => -1 | ||||||||||||||||||||||||||||||||||||||||||||
| | some .right => 1 | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| @[simp, scoped grind =] | ||||||||||||||||||||||||||||||||||||||||||||
| lemma get_move_left (t : BiTape Symbol) (p : ℤ) : | ||||||||||||||||||||||||||||||||||||||||||||
| (t.move_left).get p = t.get (p - 1) := by | ||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+154
to
+155
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fir on one line, and some parentheses can be removed:
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||
| unfold move_left get | ||||||||||||||||||||||||||||||||||||||||||||
| match p with | ||||||||||||||||||||||||||||||||||||||||||||
| | Int.ofNat 0 => | ||||||||||||||||||||||||||||||||||||||||||||
| rw [show Int.ofNat 0 - 1 = Int.negSucc 0 from rfl] | ||||||||||||||||||||||||||||||||||||||||||||
| simp [StackTape.head_eq_getD] | ||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+159
to
+160
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For these where you have something like
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||
| | Int.ofNat 1 => simp | ||||||||||||||||||||||||||||||||||||||||||||
| | Int.ofNat (n + 2) => | ||||||||||||||||||||||||||||||||||||||||||||
| rw [show Int.ofNat (n + 2) - 1 = Int.ofNat (n + 1) by lia] | ||||||||||||||||||||||||||||||||||||||||||||
| simp | ||||||||||||||||||||||||||||||||||||||||||||
| | Int.negSucc n => simp | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| @[simp, scoped grind =] | ||||||||||||||||||||||||||||||||||||||||||||
| lemma get_move_right (t : BiTape Symbol) (p : ℤ) : | ||||||||||||||||||||||||||||||||||||||||||||
| (t.move_right).get p = t.get (p + 1) := by | ||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+168
to
+169
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||
| unfold move_right get | ||||||||||||||||||||||||||||||||||||||||||||
| match p with | ||||||||||||||||||||||||||||||||||||||||||||
| | Int.ofNat n => | ||||||||||||||||||||||||||||||||||||||||||||
| rw [show Int.ofNat n + 1 = Int.ofNat (n + 1) by lia] | ||||||||||||||||||||||||||||||||||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||||||||||||||||||||||||||||||||||||||||||||
| cases n <;> simp [StackTape.head_eq_getD] | ||||||||||||||||||||||||||||||||||||||||||||
| | Int.negSucc 0 => simp | ||||||||||||||||||||||||||||||||||||||||||||
| | Int.negSucc (n + 1) => | ||||||||||||||||||||||||||||||||||||||||||||
| rw [show Int.negSucc (n + 1) + 1 = Int.negSucc n from rfl] | ||||||||||||||||||||||||||||||||||||||||||||
| simp | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| @[simp, scoped grind =] | ||||||||||||||||||||||||||||||||||||||||||||
| lemma get_optionMove (t : BiTape Symbol) (d : Option Dir) (p : ℤ) : | ||||||||||||||||||||||||||||||||||||||||||||
| (t.optionMove d).get p = t.get (p + optionDirToInt d) := by | ||||||||||||||||||||||||||||||||||||||||||||
| unfold optionMove optionDirToInt | ||||||||||||||||||||||||||||||||||||||||||||
| grind [move] | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| @[simp, scoped grind =] | ||||||||||||||||||||||||||||||||||||||||||||
| lemma get_move_right_iterate (t : BiTape Symbol) (n : ℕ) (p : ℤ) : | ||||||||||||||||||||||||||||||||||||||||||||
| (move_right^[n] t).get p = t.get (p + n):= by | ||||||||||||||||||||||||||||||||||||||||||||
| induction n generalizing t p with | ||||||||||||||||||||||||||||||||||||||||||||
| | zero => simp | ||||||||||||||||||||||||||||||||||||||||||||
| | succ n ih => simp [Function.iterate_succ_apply, ih, Int.add_assoc] | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| @[simp, scoped grind =] | ||||||||||||||||||||||||||||||||||||||||||||
| lemma get_move_left_iterate (t : BiTape Symbol) (n : ℕ) (p : ℤ) : | ||||||||||||||||||||||||||||||||||||||||||||
| (move_left^[n] t).get p = t.get (p - n):= by | ||||||||||||||||||||||||||||||||||||||||||||
| induction n generalizing t p with | ||||||||||||||||||||||||||||||||||||||||||||
| | zero => simp | ||||||||||||||||||||||||||||||||||||||||||||
| | succ n ih => | ||||||||||||||||||||||||||||||||||||||||||||
| have : p - n - 1 = p - (n + 1) := by lia | ||||||||||||||||||||||||||||||||||||||||||||
| simp [Function.iterate_succ_apply, ih, this] | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| end Move | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| /-- | ||||||||||||||||||||||||||||||||||||||||||||
| Write a value under the head of the `BiTape`. | ||||||||||||||||||||||||||||||||||||||||||||
| -/ | ||||||||||||||||||||||||||||||||||||||||||||
| def write (t : BiTape Symbol) (a : Option Symbol) : BiTape Symbol := { t with head := a } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| @[simp, scoped grind =] | ||||||||||||||||||||||||||||||||||||||||||||
| lemma get_write (t : BiTape Symbol) (a : Option Symbol) : | ||||||||||||||||||||||||||||||||||||||||||||
| (t.write a).get = Function.update t.get 0 a := by | ||||||||||||||||||||||||||||||||||||||||||||
| unfold write get Function.update | ||||||||||||||||||||||||||||||||||||||||||||
| funext p | ||||||||||||||||||||||||||||||||||||||||||||
| match p with | ||||||||||||||||||||||||||||||||||||||||||||
| | Int.ofNat 0 => simp | ||||||||||||||||||||||||||||||||||||||||||||
| | Int.ofNat (n + 1) => grind | ||||||||||||||||||||||||||||||||||||||||||||
| | Int.negSucc n => simp | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| /-- | ||||||||||||||||||||||||||||||||||||||||||||
| The space used by a `BiTape` is the number of symbols | ||||||||||||||||||||||||||||||||||||||||||||
| between and including the head, and leftmost and rightmost non-blank symbols on the `BiTape`. | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should
StackTape.ext_getand this theorem haveextattributes?There was a problem hiding this comment.
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.extandStackTape.ext_get, I can useext1 phere, but how can I be sure that theexttactic usesStackTape.ext_getinstead ofStackTape.ext?There was a problem hiding this comment.
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?