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

refactor!: Separate Signature from FuncValueType by parametrizing Type(/Row)/etc. #1138

Merged
merged 123 commits into from
Jul 17, 2024

Conversation

acl-cqc
Copy link
Contributor

@acl-cqc acl-cqc commented May 30, 2024

  • Type now means a single type, not a row variable; TypeRV can be a single type or a row variable. (Type --Into--> TypeRV).
  • These are actually parameterizations: TypeBase<NoRV> (NoRV is a type with no instances) vs TypeBase<RowVariable> (RowVariable is a struct i.e. index + bound)
  • Similarly for the other type structures - all names have changed uniformly except FunctionType:
No Row Vars With Row Vars Generic
Type TypeRV TypeBase<RV>
TypeRow TypeRowRV TypeRowBase<RV>
Signature FuncValueType FuncTypeBase<RV>
PolyFuncType PolyFuncTypeRV PolyFuncTypeBase<RV>
  • This allows us to make static guarantees (and remove dynamic checks):
    • node signatures do not have row variables as inputs or outputs
    • Neither do FuncDefn type schemes, even though they may quantify over row variables
    • "port"-indexing methods are specific to Signature (not FuncValueType) - this justifies the exceptional naming

Planning to rename PolyFuncType to (Op/Fn)TypeScheme in a followup PR (lots of fields want renaming too, whereas many FunctionType things are already called signature!)

BREAKING CHANGE: (1) In OpDefs, Type/TypeRow/FunctionType/PolyFuncType will generally need replacing by TypeRV/TypeRowRV/FuncValueType/PolyFuncTypeRV. (2) FuncValueType omits the various numbered-"port" accessors, you really shouldn't be doing that except on a Signature.... (4) SignatureError::RowVarWhereTypeExpected now contains a RowVariable struct (including bound) rather than only the index

@acl-cqc acl-cqc requested a review from doug-q May 30, 2024 08:13
@acl-cqc acl-cqc force-pushed the refactor/parametrize_type branch from 0b608a5 to 825e911 Compare May 30, 2024 09:33
@acl-cqc acl-cqc changed the title refactor!: Split (Poly/)(Function/)Type(/Row) into RowVariable/NoRV parametrizations refactor!: Separate Signature from FuncValueType by parametrizing Type(/Row)/etc. Jul 16, 2024
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.

Brilliant. I admit I have not carefully re-reviewed every line, having done so multiple times in previous iterations. I have carefully reviewed all of create::types::*. A few comments, but I recommend merging immediately.

@@ -186,15 +186,21 @@ impl<T: CustomSignatureFunc + 'static> From<T> for SignatureFunc {
}
}

impl From<PolyFuncType> for SignatureFunc {
fn from(v: PolyFuncType) -> Self {
impl From<TypeSchemeRV> for SignatureFunc {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think impl From<TypeScheme> is missing. I think you could do all four instances with impl<T: Into<TypeSchemRV>> From<T> for SignatureFunc

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Rust complains, because some future type could define both Into (currently Into<PolyFuncTypeRV>) and Into<CustomSignatureFunc> and then it would be ambiguous...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried impl <RV: MaybeRV> From<PolyFuncTypeBase<RV>> for SignatureFunc but there is no conversion from PolyFuncTypeBase<RV> (parametrized) to <RowVariable>... I could add a .into_() but would rather avoid that if I can...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can add the fourth (From<PolyFuncType>) but I couldn't see a scenario where you're making an OpDef but didn't know to create a PolyFuncTypeRV (rather than PolyFuncType) in the first place...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's easy to add though, done

}
}

pub(super) fn validate(
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this should be pub, doesn't need to be done in this PR.

@@ -185,7 +185,16 @@ pub enum TypeArg {

impl From<Type> for TypeArg {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this can be implemented as impl<RV: MaybeRV> From<TypeBase<RV>> for TypeArg.

Can be done in later

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sadly not - try_into has a different Result type according to RV, the Err could be RowVariable or Infallible (the latter from a library blanket impl). Could add a impl<RV:MaybeRV> TypeBase<RV> { fn try_into_(self) -> Result<Type, RowVariable> } I guess...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think that is slightly better, though

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added try_into_type

@acl-cqc acl-cqc enabled auto-merge July 17, 2024 09:40
@acl-cqc acl-cqc added this pull request to the merge queue Jul 17, 2024
@acl-cqc acl-cqc removed this pull request from the merge queue due to a manual request Jul 17, 2024
@acl-cqc acl-cqc added this pull request to the merge queue Jul 17, 2024
@acl-cqc
Copy link
Contributor Author

acl-cqc commented Jul 17, 2024

Ok thanks @doug-q! :).

I'm not touching serialization. I think it's all fine 🔥 we just don't extend the separation into the python world. (At least not yet, possibly never.)

Merged via the queue into main with commit b832274 Jul 17, 2024
20 checks passed
@acl-cqc acl-cqc deleted the refactor/parametrize_type branch July 17, 2024 10:08
@hugrbot hugrbot mentioned this pull request Jul 17, 2024
github-merge-queue bot pushed a commit that referenced this pull request Jul 19, 2024
## 🤖 New release
* `hugr`: 0.8.0 -> 0.9.0
* `hugr-core`: 0.5.0 -> 0.6.0
* `hugr-passes`: 0.5.0 -> 0.6.0
* `hugr-cli`: 0.1.4 -> 0.2.0

<details><summary><i><b>Changelog</b></i></summary><p>

## `hugr`
<blockquote>

## 0.9.0 (2024-07-19)

### Bug Fixes

- Add op's extension to signature check in `resolve_opaque_op`
([#1317](#1317))
- Panic on `SimpleReplace` with multiports
([#1324](#1324))

### Refactor

- [**breaking**] Separate Signature from FuncValueType by parametrizing
Type(/Row)/etc. ([#1138](#1138))

### Testing

- Verify order edges ([#1293](#1293))
- Add failing test case for
[#1315](#1315)
([#1316](#1316))
</blockquote>

## `hugr-core`
<blockquote>

## 0.6.0 (2024-07-19)

### Bug Fixes

- Add op's extension to signature check in `resolve_opaque_op`
([#1317](#1317))
- Panic on `SimpleReplace` with multiports
([#1324](#1324))

### Refactor

- [**breaking**] Separate Signature from FuncValueType by parametrizing
Type(/Row)/etc. ([#1138](#1138))

### Testing

- Verify order edges ([#1293](#1293))
- Add failing test case for
[#1315](#1315)
([#1316](#1316))
</blockquote>

## `hugr-passes`
<blockquote>

## 0.6.0 (2024-07-19)

### Refactor

- [**breaking**] Separate Signature from FuncValueType by parametrizing
Type(/Row)/etc. ([#1138](#1138))
</blockquote>

## `hugr-cli`
<blockquote>

## 0.2.0 (2024-07-19)

### Refactor

- [**breaking**] Separate Signature from FuncValueType by parametrizing
Type(/Row)/etc. ([#1138](#1138))
</blockquote>


</p></details>

---
This PR was generated with
[release-plz](https://github.com/MarcoIeni/release-plz/).

---------

Co-authored-by: Douglas Wilson <douglas.wilson@quantinuum.com>
@hugrbot hugrbot mentioned this pull request Jul 22, 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.

None yet

2 participants