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(data/matrix/notation): add !![1, 2; 3, 4] notation #14991

Closed
wants to merge 28 commits into from

Conversation

eric-wieser
Copy link
Member

@eric-wieser eric-wieser commented Jun 27, 2022

This adds !![1, 2; 3, 4] as a matlab-like shorthand for matrix.of ![![1, 2], ![3, 4]]. This has special support for empty arrays, where !![,,,] is a matrix with 0 rows and 3 columns, and ![;;;] is a matrix with 3 rows and zero columns.


Open in Gitpod

Split from #14665.

@eric-wieser eric-wieser added the awaiting-review The author would like community review of the PR label Jun 27, 2022
Comment on lines +155 to +158
/-- Convert a vector of pexprs to the pexpr constructing that vector.-/
meta def _root_.pi_fin.to_pexpr : Π {n}, (fin n → pexpr) → pexpr
| 0 v := ``(![])
| (n + 1) v := ``(vec_cons %%(v 0) %%(_root_.pi_fin.to_pexpr $ vec_tail v))
Copy link
Member Author

Choose a reason for hiding this comment

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

This might be appropriate as a has_to_pexpr instance, so that %%v has this meaning automatically. As of writing we only have the obvious pexpr, expr, and reflected instances of this typeclass; but in principle we could add instances for all "containers" that sends a container of pexprs to the pexpr that constructs that container

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not comfortable enough with has_to_pexpr to say the instance would be unproblematic (recalling all the issues with universe polymorphism). So I'm happy to keep it as-is.

Copy link
Collaborator

@Vierkantor Vierkantor 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+

src/data/matrix/notation.lean Outdated Show resolved Hide resolved
Comment on lines 65 to 75
/-- A version of `sep_by` that allows trailing delimiters, but requires at least one item -/
private meta def sep_by' {α : Type} : parser unit → parser α → parser (list α)
| s p := do
fst ← p,
some () ← optional s | pure [fst],
some rest ← optional (sep_by' s p) | pure [fst],
pure (fst :: rest)

/-- A version of `many` that requires at least one -/
private meta def at_least_one {α : Type} (p : parser α) : parser (list α) :=
list.cons <$> p <*> (many p)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd like to see these in a higher-up file, maybe tactic/core.lean.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done!

Comment on lines +155 to +158
/-- Convert a vector of pexprs to the pexpr constructing that vector.-/
meta def _root_.pi_fin.to_pexpr : Π {n}, (fin n → pexpr) → pexpr
| 0 v := ``(![])
| (n + 1) v := ``(vec_cons %%(v 0) %%(_root_.pi_fin.to_pexpr $ vec_tail v))
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not comfortable enough with has_to_pexpr to say the instance would be unproblematic (recalling all the issues with universe polymorphism). So I'm happy to keep it as-is.

@bors
Copy link

bors bot commented Jul 12, 2022

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

@leanprover-community-bot-assistant leanprover-community-bot-assistant added delegated The PR author may merge after reviewing final suggestions. and removed awaiting-review The author would like community review of the PR labels Jul 12, 2022
@eric-wieser
Copy link
Member Author

bors merge

@github-actions github-actions bot added the ready-to-merge All that is left is for bors to build and merge this PR. (Remember you need to say `bors r+`.) label Jul 13, 2022
bors bot pushed a commit that referenced this pull request Jul 13, 2022
This adds `!![1, 2; 3, 4]` as a matlab-like shorthand for `matrix.of ![![1, 2], ![3, 4]]`. This has special support for empty arrays, where `!![,,,]` is a matrix with 0 rows and 3 columns, and `![;;;]` is a matrix with 3 rows and zero columns.
@bors
Copy link

bors bot commented Jul 13, 2022

Pull request successfully merged into master.

Build succeeded:

@bors bors bot changed the title feat(data/matrix/notation): add !![1, 2; 3, 4] notation [Merged by Bors] - feat(data/matrix/notation): add !![1, 2; 3, 4] notation Jul 13, 2022
@bors bors bot closed this Jul 13, 2022
@bors bors bot deleted the eric-wieser/matrix.notation-no_of branch July 13, 2022 12:18
joelriou pushed a commit that referenced this pull request Jul 23, 2022
This adds `!![1, 2; 3, 4]` as a matlab-like shorthand for `matrix.of ![![1, 2], ![3, 4]]`. This has special support for empty arrays, where `!![,,,]` is a matrix with 0 rows and 3 columns, and `![;;;]` is a matrix with 3 rows and zero columns.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
delegated The PR author may merge after reviewing final suggestions. ready-to-merge All that is left is for bors to build and merge this PR. (Remember you need to say `bors r+`.)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants