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: Add iterators and lists #67

Merged
merged 113 commits into from
Jan 16, 2024
Merged

feat: Add iterators and lists #67

merged 113 commits into from
Jan 16, 2024

Conversation

mark-koch
Copy link
Collaborator

@mark-koch mark-koch commented Dec 20, 2023

Adds iterators, lists, for loops, and list comprehensions.

This PR sets up the basics:

  • Add new AST nodes for desugared list comprehensions and iterator commands (in nodes.py)
  • Add ListType and LinstType to the Guppy type hierarchy (in gtypes.py)
  • Declare list methods in the builtins module (in prelude/builtins.py)
  • Store/compute Hugr type bounds for guppy types

The remaining functionality is added in the following child PRs:

@mark-koch mark-koch requested a review from ss2165 January 3, 2024 10:36
Comment on lines +364 to +366
return transformer.transform(self) or ListType(
self.element_type.transform(transformer)
)
Copy link
Member

Choose a reason for hiding this comment

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

OOI what's the logic here? Try to transform the list and if not just transform the element?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, if the transformer doesn't specify what to do with a type, we leave the outer type as is and recurse

return self.element_type.linear

@property
def type_args(self) -> Iterator[GuppyType]:
Copy link
Member

Choose a reason for hiding this comment

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

why return Iterator rather than just Iterable? (and then avoid the call to iter())

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good point. I think I initially wanted an iterator since I wanted to chain them at some point, but didn't turn out to be necessary.

I created issue #75 for this

if res == TypeBound.Eq:
res = b
if res == TypeBound.Copyable and b == TypeBound.Any:
res = TypeBound.Any
Copy link
Member

Choose a reason for hiding this comment

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

could just

Suggested change
res = TypeBound.Any
return TypeBound.Any

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

In fact, the following should be enough?

res = TypeBound.Eq
for b in bs:
    if b == TypeBound.Any:
        return TypeBound.Any
    if res == TypeBound.Eq:
        res = b
return res

@@ -365,6 +369,155 @@ def __trunc__(self: float) -> float:
...


@guppy.extend_type(builtins, ListType)
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 guppy.type and guppy.extend_type ?

Copy link
Collaborator Author

@mark-koch mark-koch Jan 3, 2024

Choose a reason for hiding this comment

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

guppy.type declares a new type, wheras guppy.extend_type takes an exisisting type and allows users to define new methods on them (similar to extension methods in e.g. C#)

guppy/prelude/builtins.py Outdated Show resolved Hide resolved
guppy/prelude/builtins.py Outdated Show resolved Hide resolved
guppy/prelude/builtins.py Outdated Show resolved Hide resolved
@mark-koch mark-koch requested a review from ss2165 January 3, 2024 16:18
Base automatically changed from feat/poly to main January 9, 2024 13:47
@ss2165 ss2165 added this to the Release v0.1 milestone Jan 10, 2024
@mark-koch mark-koch marked this pull request as ready for review January 16, 2024 14:27
@mark-koch mark-koch merged commit 69fac9c into main Jan 16, 2024
1 check passed
@mark-koch mark-koch deleted the feat/lists branch January 16, 2024 14:28
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