Skip to content
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

feat: List.forall_get_iff_forall_mem #682

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Qiu233
Copy link

@Qiu233 Qiu233 commented Mar 4, 2024

theorem forall_mem_iff_forall_index [DecidableEq α] {p : α → Prop} {l : List α} :
  (∀ x ∈ l, p x) ↔ (∀ i : Fin (l.length), p (l.get i))

is trivial but sometimes useful.

@Qiu233
Copy link
Author

Qiu233 commented Mar 4, 2024

awaiting-review

@github-actions github-actions bot added the awaiting-review This PR is ready for review; the author thinks it is ready to be merged. label Mar 4, 2024
@digama0
Copy link
Member

digama0 commented Mar 4, 2024

I usually obtain the headline theorem using simp [List.mem_iff_get]. The other stuff is more API surface that should probably be considered in its own PR.

@Qiu233 Qiu233 force-pushed the forall_mem_iff_forall_index branch from 5d6ae00 to 7db45bd Compare March 4, 2024 23:18
@Qiu233
Copy link
Author

Qiu233 commented Mar 4, 2024

I usually obtain the headline theorem using simp [List.mem_iff_get]. The other stuff is more API surface that should probably be considered in its own PR.

Sorry I didn't find this theorem. Now other stuff has been removed.

@Qiu233 Qiu233 force-pushed the forall_mem_iff_forall_index branch from 7db45bd to 1cc3c1f Compare March 9, 2024 06:09
@Qiu233 Qiu233 force-pushed the forall_mem_iff_forall_index branch from 1cc3c1f to a343175 Compare March 18, 2024 12:32
@Qiu233 Qiu233 force-pushed the forall_mem_iff_forall_index branch 2 times, most recently from e01530a to 1c3f2b1 Compare March 27, 2024 13:53
Comment on lines 406 to 409
theorem forall_mem_iff_forall_index {p : α → Prop} {l : List α} :
(∀ x ∈ l, p x) ↔ (∀ i : Fin (l.length), p (l.get i)) :=
⟨fun h i => h _ (get_mem l i.1 i.2),
fun h _ hx => by have ⟨_, hi⟩ := mem_iff_get.mp hx; simp only [← hi, h]⟩

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
theorem forall_mem_iff_forall_index {p : α → Prop} {l : List α} :
(∀ x ∈ l, p x) ↔ (∀ i : Fin (l.length), p (l.get i)) :=
⟨fun h i => h _ (get_mem l i.1 i.2),
fun h _ hx => by have ⟨_, hi⟩ := mem_iff_get.mp hx; simp only [← hi, h]⟩
theorem forall_mem_iff_forall_index {p : α → Prop} {l : List α} :
(∀ x ∈ l, p x) ↔ (∀ i : Fin (l.length), p (l.get i)) := by
simp [List.mem_iff_get]

Copy link
Collaborator

Choose a reason for hiding this comment

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

What is the use case of this theorem?

Copy link
Author

Choose a reason for hiding this comment

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

What is the use case of this theorem?

Sometimes some propositions of List are expressed in indexed form, especially when translating algorithm proof from CS books.

I struggled with it for hours and it's until this issue then I know the existence of List.mem_iff_get which derives this theorem. Without it there may be someone else facing the same trouble again.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Sorry about the slow reply here. Let's reverse the iff, rename to forall_get_iff_forall_mem, and maybe even mark this as @[simp].

Copy link
Author

Choose a reason for hiding this comment

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

Done.

Copy link
Member

Choose a reason for hiding this comment

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

I think it shouldn't be @[simp]; this will not fire reliably anyway (only the RTL direction looks like it would match), and whether it is better to use indexing or membership in a given context is very dependent on other factors.

@semorrison semorrison added awaiting-author Waiting for PR author to address issues and removed awaiting-review This PR is ready for review; the author thinks it is ready to be merged. labels Apr 30, 2024
@Qiu233 Qiu233 force-pushed the forall_mem_iff_forall_index branch from 74b07fe to 84fb7eb Compare April 30, 2024 10:21
@Qiu233 Qiu233 force-pushed the forall_mem_iff_forall_index branch from 84fb7eb to ddbfca4 Compare April 30, 2024 10:22
@semorrison semorrison changed the title feat: List.forall_mem_iff_forall_index feat: List.forall_get_iff_forall_mem May 6, 2024
@leanprover-community-mathlib4-bot leanprover-community-mathlib4-bot added the merge-conflict This PR has merge conflicts with the `main` branch which must be resolved by the author. label May 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting-author Waiting for PR author to address issues merge-conflict This PR has merge conflicts with the `main` branch which must be resolved by the author.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants