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: Type check array subscripts #420

Merged
merged 2 commits into from
Aug 30, 2024
Merged

feat: Type check array subscripts #420

merged 2 commits into from
Aug 30, 2024

Conversation

mark-koch
Copy link
Collaborator

@mark-koch mark-koch commented Aug 28, 2024

Closes #416. See #415 for context.

  • Adds a new SubscriptAccess place that will be used to track array subscripts during linearity checking
  • This place is emitted when checking a subscript AST node
  • Ensures that subscripts in inout positions also implement a __setitem__ method

@mark-koch mark-koch requested a review from a team as a code owner August 28, 2024 11:08
@mark-koch mark-koch requested review from qartik and removed request for a team August 28, 2024 11:08
@codecov-commenter
Copy link

codecov-commenter commented Aug 28, 2024

Codecov Report

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

Project coverage is 92.46%. Comparing base (59e82c8) to head (be4e452).

Files with missing lines Patch % Lines
guppylang/checker/core.py 84.00% 4 Missing ⚠️
guppylang/checker/expr_checker.py 93.10% 2 Missing ⚠️
Additional details and impacted files
@@               Coverage Diff               @@
##           feat/arrays     #420      +/-   ##
===============================================
- Coverage        92.48%   92.46%   -0.02%     
===============================================
  Files               48       48              
  Lines             5178     5232      +54     
===============================================
+ Hits              4789     4838      +49     
- Misses             389      394       +5     

☔ 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.

LGTM, I think an error message can be improved, however.

23: def test(c: MyImmutableContainer) -> MyImmutableContainer:
24: foo(c[0])
^^^^
GuppyTypeError: Expression of type `MyImmutableContainer` is not assignable since it does not implement the `__setitem__` method
Copy link
Member

Choose a reason for hiding this comment

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

The error message seems confusing here as the effect of c[0] is not an assignment. To me it reads more like __getitem__. Isn't the error really because foo consumes an inout and doesn't return it back?

Assuming that is allowed if __setitem__ is defined for the container, perhaps the error messaging needs to be improved here. Something about foo requiring an assignment that's not implemented by the container.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The problem is that once the inout value has been given back by foo, we cannot assign it back to c[0] since c doesn't implement __setitem__.

But you're right, the current message is misleading. I'll update it 👍

Copy link
Member

Choose a reason for hiding this comment

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

Ah, that explains the reassignment, certainly better error message will help here.

@mark-koch mark-koch merged commit 61997cb into feat/arrays Aug 30, 2024
2 checks passed
@mark-koch mark-koch deleted the array/checking branch August 30, 2024 14:48
github-merge-queue bot pushed a commit that referenced this pull request Sep 4, 2024
This is the feature branch for array indexing. Tracked by #253.

The goal is to allow array indexing in `@inout` positions that ignores
linearity constraints in the indices:

```python
qs: array[qubit, 42]
cx(qs[0], qs[1])  # Ok
cx(qs[0], qs[0])  # Compiles, but will panic at runtime
q = qs[0]  # Error: Indexing only allowed in inout position
```

This is achieved by lowering `array[qubit]` to `array[qubit | None]` and
making `array.__getitem__` and `array.__setitem__` swap in `None`
whenever a qubit is taken out or put back in. The functions
`array.__getitem__` and `array.__setitem__` take the array as `@inout`,
so it suffices to apply the following desugaring:

```python
cx(qs[expr1], qs[expr2])

# Desugars to
idx1 = expr1
idx2 = expr2
tmp1 = qs.__getitem__(idx1)
tmp2 = qs.__getitem__(idx2)
cx(tmp1, tmp2)
qs.__setitem__(tmp1, idx1)
qs.__setitem__(tmp2, idx2)
```

This also means that arrays containing structs might behave slightly
unexpectedly:

```python
@guppy.struct
class QubitPair:
    q1: qubit
    q2: qubit

ps: array[QubitPair, 42]
cx(ps[0].q1, ps[0].q2)  # Panics at runtime :(

# Since it desugars to
idx1 = 0
idx2 = 0
tmp1 = ps.__getitem__(idx1)
tmp2 = ps.__getitem__(idx2)  # Panic: Struct at index 0 has already been replaced with None
cx(tmp1.q1, tmp2.q2)
...
```

To solve this, we need to change the Hugr lowering of arrays to
structually replace any occurrence of `qubit` with `qubit | None`, i.e.
instead of doing `array[QubitPair | None]`, we would need to do
`array[tuple[qubit | None, qubit | None]]`. I'll leave this to a future
PR in case we are interested in that.

The following feature PRs target this branch:
* #420 
* #421
* #422
* #447
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.

3 participants