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 a unified definition system #179

Merged
merged 18 commits into from
Apr 16, 2024
Merged

feat: Add a unified definition system #179

merged 18 commits into from
Apr 16, 2024

Conversation

mark-koch
Copy link
Collaborator

@mark-koch mark-koch commented Apr 8, 2024

  • Adds a new Definition ABC for all user-defined things on module-level. Currently, these are functions, types, and type vars.
  • All definition related files are moved to the submodule guppylang.definition
  • Definitions are identified by a globally unique id instead of their name. The Globals container is restructured accordingly.
  • The int(x), bool(x), qubit(), etc. functions are replaced with calls to the __new__ constructor of the type.
  • The module compilation code is greatly simplified by:
    • Adding ABCs for the compilation actions: ParsableDef -> CheckableDef -> CompilableDef -> CompiledDef
    • Adding type aliases for compilation stages: RawDef < ParsedDef < CheckedDef < CompiledDef

Closes #175 and also closes #170 (as a drive-by)

@mark-koch mark-koch added the enhancement New feature or request label Apr 8, 2024
@@ -0,0 +1,116 @@
import ast
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is the main file laying out the compilation fow for definitions

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This file was mostly copied from the old guppylang/declared.py

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This file was mostly copied from guppylang/checker/func_checker.py and guppylang/compiler/func_compiler.py

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This file was mostly copied from guppylang/checker/core.py and guppylang/compiler/core.py

@codecov-commenter
Copy link

codecov-commenter commented Apr 8, 2024

Codecov Report

Attention: Patch coverage is 91.31238% with 47 lines in your changes are missing coverage. Please review.

Project coverage is 90.67%. Comparing base (7fecc45) to head (ae1f242).

Files Patch % Lines
guppylang/definition/custom.py 75.67% 9 Missing ⚠️
guppylang/checker/core.py 82.60% 8 Missing ⚠️
guppylang/definition/function.py 93.90% 5 Missing ⚠️
guppylang/tys/parsing.py 80.00% 5 Missing ⚠️
guppylang/prelude/_internal.py 73.33% 4 Missing ⚠️
guppylang/definition/common.py 92.68% 3 Missing ⚠️
guppylang/definition/ty.py 90.90% 3 Missing ⚠️
guppylang/definition/value.py 91.17% 3 Missing ⚠️
guppylang/checker/expr_checker.py 91.30% 2 Missing ⚠️
guppylang/nodes.py 66.66% 2 Missing ⚠️
... and 3 more
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #179      +/-   ##
==========================================
+ Coverage   90.55%   90.67%   +0.11%     
==========================================
  Files          41       46       +5     
  Lines        4595     4717     +122     
==========================================
+ Hits         4161     4277     +116     
- Misses        434      440       +6     

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

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Here, we replace the old bool(x), int(x) etc functions with a __new__(x) constructor

guppylang/checker/core.py Outdated Show resolved Hide resolved
return with_loc(node, GlobalName(id=x, def_id=defn.id)), defn.ty
# For types, we return their `__new__` constructor
case TypeDef() as defn if constr := self.ctx.globals.get_instance_func(
defn, "__new__"
Copy link
Collaborator

Choose a reason for hiding this comment

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

So for Qubit, this is doing a QAlloc whenever we find a name? I'm not 100% sure when I should expect to find Names that stand for types here. The obvious example is x = Qubit() - I hope everything else is covered by stmt_checker.py (like AnnAssign and type signatures on functions)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

There is also int(x) which takes everthing that implements __int__, same for float(x) and bool(x).

Before, these were defined as functions, but now the names is already taken by the type.

Python handles this in the same way: int is a class/type and "calling" a class executes the constructor

Copy link
Collaborator

Choose a reason for hiding this comment

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

Sure, but I'm only worried about the Qubit case because adding unnecessary QAllocs (when checking type signatrues for example) will mess up any qubit resource estimates!

However, I'm fairly satisfied we're doing the right thing here

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ah I see. Yes, Name nodes in types are handled by different code in tys.parsing

guppylang/compiler/expr_compiler.py Show resolved Hide resolved
guppylang/module.py Outdated Show resolved Hide resolved
guppylang/definition/custom.py Show resolved Hide resolved
guppylang/definition/function.py Outdated Show resolved Hide resolved
@mark-koch mark-koch merged commit ae71932 into main Apr 16, 2024
3 checks passed
@mark-koch mark-koch deleted the feat/defs branch April 16, 2024 10:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unify definitions Leave a pointer for @guppy-defined functions
3 participants