Skip to content

Conversation

ctchou
Copy link

@ctchou ctchou commented Oct 8, 2025

This is the initial version of ωSequence with reference to:

https://leanprover.zulipchat.com/#narrow/channel/513188-CSLib/topic/Automata/near/543709917

This PR supercedes #80 and #86.

I would appreciate feedbacks on which theorems should be labeled with @[simp] and @[grind], especially those involving extract, as I am not familiar with how that should be done.

@ctchou ctchou requested a review from fmontesi as a code owner October 8, 2025 22:36
@ctchou
Copy link
Author

ctchou commented Oct 9, 2025

Removed a bunch of API functions and associated theorems which probably won't be needed.

@ctchou ctchou force-pushed the omega-sequence-candidate branch from 87b4bf0 to d45f253 Compare October 11, 2025 02:50
Copy link
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.

I have mostly just reviewed the new extract code, as I don't want to spend too much time re-reviewing the old Stream'.

I didn't suggest explicit choices for what should be annotated as grind and simp, but you can see some recurring theorems I use in the suggestions. If they seem okay, consider adding them so they don't have to be specified every time.

Comment on lines +31 to +34
coe s := s.get
coe_injective' := by
rintro ⟨get1⟩ ⟨get2⟩
grind
Copy link
Collaborator

Choose a reason for hiding this comment

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

I find this more readable:

Suggested change
coe s := s.get
coe_injective' := by
rintro ⟨get1⟩ ⟨get2⟩
grind
coe := ωSequence.get
coe_injective' := by grind [ωSequence, Function.Injective]


/-- Iterates of a function as an ω-sequence. -/
def iterate (f : α → α) (a : α) : ωSequence α := iterate' f a
where iterate' (f : α → α) (a : α) : ℕ → α
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe it would be best to move the where to a separate Function.iterate? Then I think the theorem below would just be equality with that by rfl. It seems awkward to have a theorem about equality to a match.

(fun s₁ s₂ ⟨h₁, h₂, h₃⟩ => by rw [← h₂, ← h₃] ; grind)
(And.intro hh (And.intro ht₁ ht₂))

theorem coinduction {s₁ s₂ : ωSequence α} :
Copy link
Collaborator

Choose a reason for hiding this comment

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

If ωSequence is not meant to be emulating a coinductive type, do we need to port this from Stream'? Or is it useful in some other way?


theorem extract_eq_take {xs : ωSequence α} {n : ℕ} :
xs.extract 0 n = xs.take n := by
simp only [extract_eq_drop_take, Nat.sub_zero, drop_zero]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Mathlib style is to not "squeeze terminal simps" i.e at the end of a proof not use only. Can you please remove all of these and minimize the the theorems specified in the [...]? This is considered to make these proofs a bit less fragile.

Copy link
Collaborator

Choose a reason for hiding this comment

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

(I ended up leaving exact suggestions for all of these, hopefully that makes it easier. As a reminder, you can commit suggestions from the UI here)

Comment on lines +461 to +462
rw [← h_eq] at h_fun ; simp only [heq_eq_eq, funext_iff, Fin.forall_iff] at h_fun
simp only [← h_eq, true_and] ; intro k h_k ; simp only [h_fun k h_k]
Copy link
Collaborator

Choose a reason for hiding this comment

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

The HEq here is a pain, this is the most I could simplify:

Suggested change
rw [← h_eq] at h_fun ; simp only [heq_eq_eq, funext_iff, Fin.forall_iff] at h_fun
simp only [← h_eq, true_and] ; intro k h_k ; simp only [h_fun k h_k]
rw [← h_eq] at h_fun
simp_all [funext_iff, Fin.forall_iff]

Comment on lines +517 to +520
ext k x ; rcases (show k < j - i ∨ ¬ k < j - i by omega) with h_k | h_k
<;> simp [extract_eq_ofFn, h_k]
· simp [(show i + k < n - m by omega), (show k < m + j - (m + i) by omega), Nat.add_assoc]
· simp only [(show ¬k < m + j - (m + i) by omega), IsEmpty.forall_iff]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
ext k x ; rcases (show k < j - i ∨ ¬ k < j - i by omega) with h_k | h_k
<;> simp [extract_eq_ofFn, h_k]
· simp [(show i + k < n - m by omega), (show k < m + j - (m + i) by omega), Nat.add_assoc]
· simp only [(show ¬k < m + j - (m + i) by omega), IsEmpty.forall_iff]
ext k
cases Decidable.em (k < j - i) <;> grind [extract_eq_ofFn]


theorem extract_extract2 {xs : ωSequence α} {n i j : ℕ} (h : j ≤ n) :
(xs.extract 0 n).extract i j = xs.extract i j := by
simp only [extract_extract2' (show j ≤ n - 0 by omega), Nat.zero_add]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
simp only [extract_extract2' (show j ≤ n - 0 by omega), Nat.zero_add]
grind [extract_extract2']


theorem extract_extract1 {xs : ωSequence α} {n i : ℕ} :
(xs.extract 0 n).extract i = xs.extract i n := by
simp only [length_extract, extract_extract2 (show n ≤ n by omega), Nat.sub_zero]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
simp only [length_extract, extract_extract2 (show n ≤ n by omega), Nat.sub_zero]
grind [extract_extract2, length_extract]

Comment on lines +452 to +454
ext k x ; rcases (show k < n - m ∨ ¬ k < n - m by omega) with h_k | h_k
<;> simp (disch := omega) [extract_eq_drop_take, getElem?_take, get_drop]
<;> aesop
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
ext k x ; rcases (show k < n - m ∨ ¬ k < n - m by omega) with h_k | h_k
<;> simp (disch := omega) [extract_eq_drop_take, getElem?_take, get_drop]
<;> aesop
ext k
cases Decidable.em (k < n - m) <;>
grind [extract_eq_drop_take, getElem?_take, get_drop, length_take]

rw [← append_extract_extract h_mn (show n ≤ n + 1 by omega)] ; congr
simp only [extract_eq_drop_take, Nat.add_sub_cancel_left, take_one, get_drop, Nat.add_zero]

theorem extract_extract2' {xs : ωSequence α} {m n i j : ℕ} (h : j ≤ n - m) :
Copy link
Collaborator

Choose a reason for hiding this comment

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

The names of these last theorems could be better, at minimum replacing the prime and maybe using subscripts for the numbers of there isn't a more descriptive name.

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.

2 participants