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!: Merge Value into Const #881

Merged
merged 10 commits into from
Mar 18, 2024
Merged

feat!: Merge Value into Const #881

merged 10 commits into from
Mar 18, 2024

Conversation

aborgna-q
Copy link
Collaborator

Const was a struct with a Value + a defined type. In practice, the only Value that couldn't report its full type was Sum.

This PR merges the two structs, adding an explicit sum_type to the sum variant.

Also:

  • Removes Type::check_type, as this is now unnecessary.
  • Splits and clarifies the type check and constant errors;
    SumTypeError is not a ConstTypeError is not a CustomCheckFailure.
    • Adds contexts to those errors, so we get better messages.
  • CustomConst is now accessible from ::ops::custom instead of ::values.

This change breaks the serialisation format.

Closes #874.

BREAKING CHANGE: Removed Value, merged into Const

@aborgna-q aborgna-q changed the title feat!: Mergo Value into Const feat!: Merge Value into Const Mar 14, 2024
}

/// Constant Sum over units, used as branching values.
pub fn unit_sum(tag: usize, size: u8) -> Result<Self, ConstTypeError> {
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 only returns an Err if tag >= size.
I'm undecided on whether panicking instead, as every caller just unwraps it immediately.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I prefer using Result over panicking here, because other similar functions (e.g. function above) do. It's a public function so we can't say anything about every caller.

@aborgna-q aborgna-q force-pushed the refactor/drop-value branch from c9976c7 to 76f18e5 Compare March 14, 2024 17:26
@aborgna-q
Copy link
Collaborator Author

Added an ad-hoc get_const_function_type in lieu of #880, while we decide on that.

@aborgna-q aborgna-q changed the base branch from feat/get-function-type to main March 14, 2024 17:27
@aborgna-q aborgna-q marked this pull request as ready for review March 14, 2024 17:27
@doug-q
Copy link
Collaborator

doug-q commented Mar 15, 2024

I think the serialisation schema should be changing because the serialisation of const nodes has changed?

We have already bumped the serialisation version to V1 since the last release, so it is fine to change the V1 schema. If we hadn't bumped since the last release this would necessitate bumping.

@aborgna-q
Copy link
Collaborator Author

aborgna-q commented Mar 15, 2024

Yes, here's how the new encoding looks for an (extension) usize:

{
  "v": "Extension",
  "c": {
    "c": "ConstUsize",
    "value": 257
  }
}

And for a [float, usize] + [] const:

{
  "v": "Sum",
  "tag": 0,
  "values": [
    {
      "v": "Extension",
      "c": {
        "c": "ConstUsize",
        "value": 257
      }
    },
    {
      "v": "Extension",
      "c": {
        "c": "CustomSerialized",
        "typ": {
          "t": "Opaque",
          "extension": "arithmetic.float.types",
          "id": "float64",
          "args": [],
          "bound": "C"
        },
        "value": 5.1,
        "extensions": [
          "arithmetic.float.types"
        ]
      }
    }
  ],
  "typ": {
    "s": "General",
    "rows": [
      [
        {
          "t": "I"
        },
        {
          "t": "Opaque",
          "extension": "arithmetic.float.types",
          "id": "float64",
          "args": [],
          "bound": "C"
        }
      ],
      []
    ]
  }
}

Copy link

codecov bot commented Mar 15, 2024

Codecov Report

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

Project coverage is 85.62%. Comparing base (9ef7808) to head (930c5e5).

Files Patch % Lines
quantinuum-hugr/src/ops/constant.rs 83.96% 18 Missing and 3 partials ⚠️
quantinuum-hugr/src/ops/constant/custom.rs 65.95% 15 Missing and 1 partial ⚠️
quantinuum-hugr/src/algorithm/const_fold.rs 66.66% 6 Missing ⚠️
...td_extensions/arithmetic/conversions/const_fold.rs 40.00% 2 Missing and 1 partial ⚠️
quantinuum-hugr/src/hugr/rewrite/inline_dfg.rs 0.00% 0 Missing and 1 partial ⚠️
quantinuum-hugr/src/types.rs 80.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #881      +/-   ##
==========================================
+ Coverage   85.49%   85.62%   +0.12%     
==========================================
  Files          78       78              
  Lines       14455    14397      -58     
  Branches    14455    14397      -58     
==========================================
- Hits        12359    12327      -32     
+ Misses       1459     1436      -23     
+ Partials      637      634       -3     

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

@aborgna-q
Copy link
Collaborator Author

I updated the serialization schema manually (merging the json generated by CQCL/guppylang#172 with the local changes).
Hopefully we can improve this process in the future (see CQCL/guppylang#173).

@aborgna-q aborgna-q requested a review from doug-q March 18, 2024 10:37
Copy link
Collaborator

@doug-q doug-q left a comment

Choose a reason for hiding this comment

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

Awesome, just a few nits

quantinuum-hugr/src/ops/constant.rs Outdated Show resolved Hide resolved
}

/// Constant Sum over units, used as branching values.
pub fn unit_sum(tag: usize, size: u8) -> Result<Self, ConstTypeError> {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I prefer using Result over panicking here, because other similar functions (e.g. function above) do. It's a public function so we can't say anything about every caller.

quantinuum-hugr/src/types/check.rs Outdated Show resolved Hide resolved
@aborgna-q aborgna-q added this pull request to the merge queue Mar 18, 2024
Merged via the queue into main with commit 76d3ea3 Mar 18, 2024
9 of 10 checks passed
@aborgna-q aborgna-q deleted the refactor/drop-value branch March 18, 2024 14:13
@github-actions github-actions bot mentioned this pull request Mar 18, 2024
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.

Get rid of Value
2 participants