-
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: Allow lists in py expressions #113
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f0435e7
feat: Allow lists in py expressions
mark-koch 3208bab
Merge branch 'main' into feat/py-lists
mark-koch 5fa46f9
Fix test decorator
mark-koch 829bf20
Improve error message
mark-koch 45bb77a
Merge branch 'main' into feat/py-lists
mark-koch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import ast | ||
from typing import Literal | ||
from typing import Any, Literal | ||
|
||
from pydantic import BaseModel | ||
|
||
|
@@ -52,6 +52,13 @@ class ConstF64(BaseModel): | |
value: float | ||
|
||
|
||
class ListValue(BaseModel): | ||
"""Hugr representation of floats in the arithmetic extension.""" | ||
|
||
c: Literal["ListValue"] = "ListValue" | ||
value: list[Any] | ||
|
||
|
||
def bool_value(b: bool) -> val.Value: | ||
"""Returns the Hugr representation of a boolean value.""" | ||
return val.Sum(tag=int(b), value=val.Tuple(vs=[])) | ||
|
@@ -67,6 +74,11 @@ def float_value(f: float) -> val.Value: | |
return val.ExtensionVal(c=(ConstF64(value=f),)) | ||
|
||
|
||
def list_value(v: list[val.Value]) -> val.Value: | ||
"""Returns the Hugr representation of a list value.""" | ||
return val.ExtensionVal(c=(ListValue(value=v),)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
|
||
def logic_op(op_name: str, args: list[tys.TypeArg] | None = None) -> ops.OpType: | ||
"""Utility method to create Hugr logic ops.""" | ||
return ops.CustomOp(extension="logic", op_name=op_name, args=args or []) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Guppy compilation failed. Error in file $FILE:6 | ||
|
||
4: @compile_guppy | ||
5: def foo() -> int: | ||
6: return py([1, 1.0]) | ||
^^^^^^^^^^^^ | ||
GuppyError: Python list contains elements with different types |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from tests.util import compile_guppy | ||
|
||
|
||
@compile_guppy | ||
def foo() -> int: | ||
return py([1, 1.0]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Guppy compilation failed. Error in file $FILE:6 | ||
|
||
4: @compile_guppy | ||
5: def foo() -> None: | ||
6: xs = py([]) | ||
^^^^^^ | ||
GuppyTypeError: Cannot infer type variable in expression of type `list[?T]` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from tests.util import compile_guppy | ||
|
||
|
||
@compile_guppy | ||
def foo() -> None: | ||
xs = py([]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I don't understand why we're converting
python_val
to a type. It's an arbitrary python value right? Just something inside apy(...)
?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.
Okay, I'm looking up how python
match
works...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.
Here, we're checking that the Python expression inside
py(...)
evaluates to something that is valid in Guppy and compute the corresponding Guppy type