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: Array comprehension #613

Merged
merged 16 commits into from
Dec 2, 2024
Merged

feat: Array comprehension #613

merged 16 commits into from
Dec 2, 2024

Conversation

mark-koch
Copy link
Collaborator

@mark-koch mark-koch commented Nov 4, 2024

Adds array comprehensions of the form array(i for i in range(10)).

This is the base PR that adds new AST nodes for array comprehensions and general generator expressions. Checking and lowering logic follows in subsequent PRs targetting this branch:

Closes #612

@mark-koch mark-koch requested a review from a team as a code owner November 4, 2024 13:00
@mark-koch mark-koch requested review from ss2165 and removed request for a team November 4, 2024 13:00
@codecov-commenter
Copy link

codecov-commenter commented Nov 4, 2024

Codecov Report

Attention: Patch coverage is 97.04433% with 6 lines in your changes missing coverage. Please review.

Project coverage is 92.75%. Comparing base (07b9871) to head (cba15f0).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
guppylang/std/_internal/checker.py 95.00% 3 Missing ⚠️
guppylang/cfg/bb.py 88.88% 1 Missing ⚠️
guppylang/checker/expr_checker.py 88.88% 1 Missing ⚠️
guppylang/compiler/core.py 92.30% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #613      +/-   ##
==========================================
+ Coverage   92.48%   92.75%   +0.26%     
==========================================
  Files          66       66              
  Lines        7549     7658     +109     
==========================================
+ Hits         6982     7103     +121     
+ Misses        567      555      -12     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Base automatically changed from feat/sized-iters to main November 4, 2024 14:59
Copy link
Member

@ss2165 ss2165 left a comment

Choose a reason for hiding this comment

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

👍


expr, res_ty = self.synthesize(args)
subst, _ = check_type_against(res_ty, ty, self.node)
return expr, subst
Copy link
Member

Choose a reason for hiding this comment

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

not clear to me why this change is related here

Copy link
Collaborator Author

@mark-koch mark-koch Nov 5, 2024

Choose a reason for hiding this comment

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

This was added in #610. Github showed the diff because of a merge conflict after the base branch changed, sorry 😅

@@ -559,6 +561,27 @@ def __len__(self: array[L, n]) -> int: ...
def __new__(): ...


@guppy.extend_type(sized_iter_type_def)
class SizedIter:
"""A wrapper around an iterator type `T` promising that the iterator will yield
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
"""A wrapper around an iterator type `T` promising that the iterator will yield
"""A wrapper around an iterator type `L` promising that the iterator will yield

?

Comment on lines 576 to 582
@guppy.custom(NoopCompiler())
def unwrap_iter(self: "SizedIter[L, n]" @ owned) -> L:
"""Extracts the actual iterator."""

@guppy.custom(NoopCompiler())
def __iter__(self: "SizedIter[L, n]" @ owned) -> L:
"""Extracts the actual iterator."""
Copy link
Member

Choose a reason for hiding this comment

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

what's the difference between these? (is there one?)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It's the same, I thought we might want to have a non-dunder method to get the wrapped iterator?

mark-koch and others added 8 commits November 5, 2024 09:38
Refactor the list comprehension logic so we can also use it for array
comprehensions
* Refactor compilation of list comprehensions so we can use the same
logic for array comprehensions. The only difference is that lists call
`push` to add the next element to the accumulator whereas arrays use
`set` and update a counter. This is now implemented via a `build_update`
hook provided to `_compile_generators`
* Store checked `Globals` inside the compilation context so we can
invoke Guppy methods by name while building Hugr
if node.elt_ty.linear
else node.elt_ty.to_hugr()
)
# See https://github.com/CQCL/guppylang/issues/629
Copy link
Member

Choose a reason for hiding this comment

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

not sure about the relevance of this link?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is to explain why we use Option for both linear and classical arrays

@mark-koch mark-koch added this pull request to the merge queue Dec 2, 2024
Merged via the queue into main with commit fdc0526 Dec 2, 2024
3 checks passed
@mark-koch mark-koch deleted the feat/array-compr branch December 2, 2024 13:39
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.

Array comprehension
3 participants