-
Notifications
You must be signed in to change notification settings - Fork 5
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!: Comptime functions #727
base: main
Are you sure you want to change the base?
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #727 +/- ##
========================================
Coverage 93.11% 93.12%
========================================
Files 74 82 +8
Lines 8632 9393 +761
========================================
+ Hits 8038 8747 +709
- Misses 594 646 +52 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Previous use occurred in | ||
linear_copy6.py:15 |
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 line-break is a bug in diagnostics rendering. See #818
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.
Review part 1, excluding tests
I suggest you expand the compiler docs to include discussion of comptime as part of this PR, while it is still fresh
guppylang/tracing/function.py
Outdated
# Type error in the return statement. For example, this happens if users | ||
# try to return a struct with invalid field values | ||
raise GuppyError(TracingReturnTypeError(node, str(err))) from None | ||
except ValueError as err: |
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'm confused as to why these are raising standard Python errors rather than guppy internal ones
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.
Errors from guppy_object_from_py
can also surface to the user so they are not internal. We just catch them here since they ocurr while processing the function return, so we don't have a useful traceback
If you prefer, we could create a new user-facing GuppyComptimeError
instead of using Python's builtin ValueError
and TypeError
?
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.
Yes would prefer that
using `guppy_object_from_py`, updates the wires of any `GuppyObjects` contained in | ||
`v` to the new wires specified by `obj`. | ||
|
||
Also resets the used flag on any of those updated wires. |
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.
why is this?
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 corresponds to making a value available again after a borrow expires. I'll add this to the docstring
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.
is this done? I couldn't find it
guppylang/std/builtins.py
Outdated
class array(Generic[_T, _n]): | ||
class array(list[_T], Generic[_T, _n]): | ||
"""Class to import in order to use arrays.""" | ||
|
||
def __init__(self, *args: Any): | ||
pass | ||
list.__init__(self, args) |
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.
include this as a comment in the code
from guppylang.module import GuppyModule | ||
|
||
import guppylang.decorator as decorator | ||
|
||
|
||
# Regular expression to match the `~~~~~^^^~~~` highlights that are printed in | ||
# tracebacks from Python 3.11 onwards. We strip those out so we can use the same golden | ||
# files for Python 3.10 |
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 feels super flaky
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.
Do you have another preferred solution? We could store multiple .err
files for each Python version, but then each we make will need to run pytest --snapshot-update
for all versions which would be annoying
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.
Review part 2, tests
10 | xs.pop() | ||
| ^^^^^^^^^^^^ | ||
|
||
Borrowed argument `xs` cannot be returned back to the caller. Expected it to |
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.
Suggest rephrasing something like "xs is borrowed so must be returned to the caller. ..."
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.
Maybe we should also mention that this return is happening implicitly. Otherwise, useres might get confused and try to return it themselves?
| ^^^^^^^^^^^^^^ | ||
|
||
Borrowed argument `xs` cannot be returned back to the caller. Cannot infer the | ||
type of empty list |
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.
type of empty list | |
type of empty list. |
@@ -3,4 +3,4 @@ Traceback (most recent call last): | |||
module.compile() | |||
File "$FILE", line 13, in test | |||
foo(1, 2, 3, 4) | |||
guppylang.error.GuppyComptimeError: Too many arguments: Expected 2, got 4 |
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.
what happened to the expected line
using `guppy_object_from_py`, updates the wires of any `GuppyObjects` contained in | ||
`v` to the new wires specified by `obj`. | ||
|
||
Also resets the used flag on any of those updated wires. |
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.
is this done? I couldn't find it
Adds a new
guppy.comptime
decorator for functions that construct Hugr via tracing of the Python interpreter.The most important bits are in the new
guppylang.tracing
module:state.py
defines the shared state that is maintained during tracingfunction.py
contains the main entry point for tracing of a function:trace_function
. It also contains the logic for handling function calls during tracing:trace_call
object.py
defines the newGuppyObject
class. This is the abstract representation of values that are not statically known (e.g. function inputs, outputs of called functions, etc.). It also contains theGuppyDefinition
class which is the new object that is returned to users when they decorate a function.unpacking.py
defines the logic for turningGuppyObjects
representing structured types into their Python equivalent and vice versa. For example, Guppy tuples are turned into Python tuples, Guppy arrays are turned into Python lists.Some more esoteric things:
builtins_mock.py
contains some metaclass hacks to mock builtin functionsint
,float
, andlen
to acceptGuppyObject
sfrozenlist.py
is an implementation of immutable lists. They are needed to prevent users from mutating function inputs to preserve Python semantics.util.py
defines the logic for dealing with exceptions and modifying tracebacks to hide internal compiler frames from the userOther changes:
execute_llvm
Rust library to support passing function inputs. This is to make testing of arithmetic functions easier.sys.excepthook
on the caught exception in the test (seetests.error.util.run_error_test
)tests.error.util.run_error_test
to ignore those differencesFollow-ups:
py(...)
expressions tocomptime(...)
with comptime:
blocksBREAKING CHANGE: Guppy decorators now return instances of
GuppyDefinition
Closes #751