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 SizedIter wrapper type #611

Merged
merged 4 commits into from
Nov 4, 2024
Merged

feat: Add SizedIter wrapper type #611

merged 4 commits into from
Nov 4, 2024

Conversation

mark-koch
Copy link
Collaborator

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

Adds a new wrapper type SizedIter to the standard library that annotates an iterator with a static size hint. This will be used for example in array comprehensions to infer the size of the resulting array. The type gets erased when lowering to Hugr.

Updates the range function to emit such a size hint when the range stop is given by a static number. For example the expression range(10) is typed as SizedIter[Range, 10] whereas range(n + 1) is typed as Range. Currently, this is implemented via a CustomCallChecker, but in the future we could use function overloading and Literal types to implement this in Guppy source:

@guppy.overloaded
def range[n: nat](stop: Literal[n]) -> SizedIter[Range, n]:
   return SizedIter(Range(0, stop))

@guppy.overloaded
def range(stop: int) -> Range:
   return Range(0, stop)

Closes #610.

@mark-koch mark-koch requested a review from a team as a code owner November 4, 2024 12:36
@mark-koch mark-koch requested a review from qartik November 4, 2024 12:36
Comment on lines -264 to +273
@abstractmethod
def check(self, args: list[ast.expr], ty: Type) -> tuple[ast.expr, Subst]:
"""Checks the return value against a given type.

Returns a (possibly) transformed and annotated AST node for the call.
"""
from guppylang.checker.expr_checker import check_type_against

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

Choose a reason for hiding this comment

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

Drive-by: Add a default implementation for CustomCallChecker.check that falls back to the synthesizing case if we can't make use of the incoming type information

@codecov-commenter
Copy link

codecov-commenter commented Nov 4, 2024

Codecov Report

Attention: Patch coverage is 88.05970% with 8 lines in your changes missing coverage. Please review.

Project coverage is 91.64%. Comparing base (f5670f6) to head (a38ec89).

Files with missing lines Patch % Lines
guppylang/tys/builtin.py 61.11% 7 Missing ⚠️
guppylang/prelude/builtins.py 93.75% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main     #611   +/-   ##
=======================================
  Coverage   91.64%   91.64%           
=======================================
  Files          60       60           
  Lines        6389     6443   +54     
=======================================
+ Hits         5855     5905   +50     
- Misses        534      538    +4     

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

Copy link
Member

@qartik qartik left a comment

Choose a reason for hiding this comment

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

Overall LGTM, it might be nice to add some tests as well.

@mark-koch
Copy link
Collaborator Author

range is already tested in tests/integration/test_range.py, but I added new tests for non-static range lengths and one that checks that the inferred size annotation is correct

@mark-koch mark-koch requested a review from qartik November 4, 2024 14:27
@mark-koch mark-koch added this pull request to the merge queue Nov 4, 2024
Merged via the queue into main with commit 2e9da6b Nov 4, 2024
3 checks passed
@mark-koch mark-koch deleted the feat/sized-iters branch November 4, 2024 14:59
github-merge-queue bot pushed a commit that referenced this pull request Nov 12, 2024
🤖 I have created a release *beep* *boop*
---


## [0.13.0](v0.12.2...v0.13.0)
(2024-11-12)


### ⚠ BREAKING CHANGES

* `prelude` module renamed to `std`

### Features

* add `qubit` discard/measure methods
([#580](#580))
([242fa44](242fa44))
* Add `SizedIter` wrapper type
([#611](#611))
([2e9da6b](2e9da6b))
* conventional results post processing
([#593](#593))
([db96224](db96224))
* Improve compiler diagnostics
([#547](#547))
([90d465d](90d465d)),
closes [#551](#551)
[#553](#553)
[#586](#586)
[#588](#588)
[#587](#587)
[#590](#590)
[#600](#600)
[#601](#601)
[#606](#606)
* restrict result tag sizes to 256 bytes
([#596](#596))
([4e8e00f](4e8e00f)),
closes [#595](#595)


### Bug Fixes

* Mock guppy decorator during sphinx builds
([#622](#622))
([1cccc04](1cccc04))


### Documentation

* Add DEVELOPMENT.md
([#584](#584))
([1d29d39](1d29d39))
* Fix docs build ([#639](#639))
([bd6011c](bd6011c))


### Miscellaneous Chores

* Manually set last release commit
([#643](#643))
([b2d569b](b2d569b))


### Code Refactoring

* rename prelude to std
([#642](#642))
([1a68e8e](1a68e8e))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

---------

Co-authored-by: Agustín Borgna <[email protected]>
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.

Statically sized iterators
3 participants