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

[Merged by Bors] - feat: Add to List.finRange and Fin.castLE APIs #8306

Closed
wants to merge 13 commits into from

Conversation

alexkeizer
Copy link
Collaborator

@alexkeizer alexkeizer commented Nov 9, 2023

Add a lemma that unfolds finRange n.succ in terms of List.concat, and add a few simp-lemmas for Fin.castLE


The PRed lemmas seem unrelated. This PR exists because they were both used in a different proof, which was decided against, but these lemmas were deemed useful enough to keep the PR

Open in Gitpod

… n → α` is equivalent to

    folding over `List.finRange` and invoking `g` for each index
alexkeizer and others added 3 commits November 10, 2023 12:04
Co-authored-by: Eric Wieser <wieser.eric@gmail.com>
@alexkeizer alexkeizer marked this pull request as ready for review November 10, 2023 11:38
alexkeizer and others added 3 commits November 10, 2023 12:11
Co-authored-by: Eric Wieser <wieser.eric@gmail.com>
Comment on lines 75 to 85
/-- Folding over a list obtained from a function `g : Fin n → α` is equivalent to
folding over the list of indices `0, 1, ..., n-1` and invoking `g` for each index -/
theorem foldl_ofFn {α β n} (f : α → β → α) (init : α) (g : Fin n → β) :
(List.ofFn g).foldl f init = (List.finRange n).foldl (fun a i => f a (g i)) init := by
rw [ofFn_eq_map, foldl_map]

/-- Folding over a list obtained from a function `g : Fin n → α` is equivalent to
folding over the list of indices `0, 1, ..., n-1` and invoking `g` for each index -/
theorem foldr_ofFn {α β n} (f : β → α → α) (init : α) (g : Fin n → β) :
(List.ofFn g).foldr f init = (List.finRange n).foldr (fun i a => f (g i) a) init := by
rw [ofFn_eq_map, foldr_map]
Copy link
Member

Choose a reason for hiding this comment

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

With these new proofs in mind, it's not really clear to me that we want these lemmas at all; I think it makes more sense for the user to go via ofFn_eq_map, because then the whole API for map is at their fingertips. The trail you're blazing ends in us repeating the entire map API for ofFn!

Maybe the documentation for ofFn should more prominently display ofFn_eq_map.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fair point.

Maybe the documentation for ofFn should more prominently display ofFn_eq_map.

This is slightly complicated, since List.ofFn is defined in Std, while ofFn_eq_map is from Mathlib.

In any case, now that I know about ofFn_eq_map, I can just use these simpler proofs in #5920

Copy link
Member

Choose a reason for hiding this comment

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

ofFn_eq_map could probably be upstreamed to Std (unless finRange is only in mathlib...)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

finRange is indeed also only in mathlib. The definition of finRange depends only on stuff in std, so we could upstream ofFn_eq_map and finRange together, if desired

@alexkeizer alexkeizer closed this Nov 10, 2023
@alexkeizer alexkeizer reopened this Nov 10, 2023
@alexkeizer alexkeizer changed the title feat: Add to List.finRange API feat: Add to List.finRange and Fin.castLE APIs Nov 10, 2023
@alexkeizer
Copy link
Collaborator Author

I've now removed the fold lemmas, but kept the others

Copy link
Member

@eric-wieser eric-wieser left a comment

Choose a reason for hiding this comment

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

bors d+

Thanks!

Mathlib/Data/Fin/Basic.lean Outdated Show resolved Hide resolved
Mathlib/Data/Fin/Basic.lean Outdated Show resolved Hide resolved
Mathlib/Data/Fin/Basic.lean Outdated Show resolved Hide resolved
Mathlib/Data/Fin/Basic.lean Outdated Show resolved Hide resolved
@mathlib-bors
Copy link

mathlib-bors bot commented Nov 10, 2023

✌️ alexkeizer can now approve this pull request. To approve and merge a pull request, simply reply with bors r+. More detailed instructions are available here.

Co-authored-by: Eric Wieser <wieser.eric@gmail.com>
@alexkeizer
Copy link
Collaborator Author

bors r+

mathlib-bors bot pushed a commit that referenced this pull request Nov 11, 2023
Add a lemma that unfolds `finRange n.succ` in terms of `List.concat`, and add a few simp-lemmas for `Fin.castLE`
@mathlib-bors
Copy link

mathlib-bors bot commented Nov 11, 2023

Pull request successfully merged into master.

Build succeeded:

@mathlib-bors mathlib-bors bot changed the title feat: Add to List.finRange and Fin.castLE APIs [Merged by Bors] - feat: Add to List.finRange and Fin.castLE APIs Nov 11, 2023
@mathlib-bors mathlib-bors bot closed this Nov 11, 2023
@mathlib-bors mathlib-bors bot deleted the list-foldl-offn branch November 11, 2023 12:25
alexkeizer added a commit that referenced this pull request Nov 17, 2023
Add a lemma that unfolds `finRange n.succ` in terms of `List.concat`, and add a few simp-lemmas for `Fin.castLE`
grunweg pushed a commit that referenced this pull request Dec 15, 2023
Add a lemma that unfolds `finRange n.succ` in terms of `List.concat`, and add a few simp-lemmas for `Fin.castLE`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants