-
Notifications
You must be signed in to change notification settings - Fork 3
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 array type #258
feat: Add array type #258
Conversation
Co-authored-by: Craig Roy <croyzor@users.noreply.github.com>
@guppy.extend_type(builtins, array_type_def) | ||
class Array: | ||
@guppy.hugr_op(builtins, DummyOp("ArrayGet")) | ||
def __getitem__(self: array[T, n], idx: int) -> T: ... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The type varable T
is declared as non-linear, so it cannot be instantiated with qubit
(see error tests).
We first need some changes to the type checker in order to fully support linear arrays
13: def main(qs: array[qubit, 42]) -> int: | ||
14: return qs[0] | ||
^^ | ||
GuppyTypeError: Cannot instantiate non-linear type variable `T` in type `forall n, T: nat. (array[T, n], int) -> T` with linear type `qubit` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This error message is not super helpful, but we only need it temporarily until we have proper linear arrays
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The linear array errors look quite confusing, but I guess they are OK as temporary measure.
guppylang/prelude/builtins.py
Outdated
class array(Generic[_S, _T]): | ||
"""Class to import in order to use arrays.""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The names are switched from the Array
definition below (which uses arrar[T, n]
).
Can you document which typevar is the array type and which one the length?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This class is just for the purpose of mypy and ruff to stop them from complaining, so it really doesn't matter what type vars we use here. But you're right, I'll choose some more suggestive names 👍
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #258 +/- ##
==========================================
+ Coverage 90.67% 90.93% +0.26%
==========================================
Files 44 44
Lines 4954 4976 +22
==========================================
+ Hits 4492 4525 +33
+ Misses 462 451 -11 ☔ View full report in Codecov by Sentry. |
This file should have been added with #258
🤖 I have created a release *beep* *boop* --- ## [0.6.0](v0.5.2...v0.6.0) (2024-07-02) ### Features * Add array type ([#258](#258)) ([041c621](041c621)) * Add nat type ([#254](#254)) ([a461a9d](a461a9d)) * Add result function ([#271](#271)) ([792fb87](792fb87)), closes [#270](#270) * Allow constant nats as type args ([#255](#255)) ([d706735](d706735)) * Generate constructor methods for structs ([#262](#262)) ([f68d0af](f68d0af)), closes [#261](#261) * Return already-compiled hugrs from `GuppyModule.compile` ([#247](#247)) ([9d01eae](9d01eae)) * Turn int and float into core types ([#225](#225)) ([99217dc](99217dc)) ### Bug Fixes * Add missing test file ([#266](#266)) ([75231fe](75231fe)) * Loading custom polymorphic function defs as values ([#260](#260)) ([d15b2f5](d15b2f5)), closes [#259](#259) --- This PR was generated with [Release Please](https://github.com/googleapis/release-please). See [documentation](https://github.com/googleapis/release-please#release-please).
No description provided.