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: Helper functions for requesting inference, use with builder in tests #1219

Merged
merged 4 commits into from
Jun 28, 2024

Conversation

acl-cqc
Copy link
Contributor

@acl-cqc acl-cqc commented Jun 24, 2024

  • Add builder::ft2 that takes 2 Into<Typerow>s, and builds a FunctionType with extension delta TO_BE_INFERRED
  • And builder::ft1 that takes a single Into<TypeRow> and makes an endomorphic FunctionType similarly
  • Use these to update a bunch of tests made worse by earlier PRs (i.e. these now use inference rather than manually specifying deltas)
  • Correct some doc comments....
  • ....and add a dfg_builder_endo method that was hinted at by one of the incorrect doc comments, and that infers the delta.

@acl-cqc
Copy link
Contributor Author

acl-cqc commented Jun 24, 2024

On #1195, @ss2165 wrote:

Thoughts on builder interface:

  • Builders that infer extensions should be default, therefore they should have the short names (probably just the set of names we currently use with explicit deltas).
  • Explicit delta versions, if necessary, can have some standard naming convention (e.g. dfg_builder_delta)
  • Could a potential simplification be just to require FunctionType inputs wherever possible (rather than in_types + out_types) and then make the default extension set in FunctionType TO_BE_INFERRED. Then with_extension_delta on FunctionType would overwrite the default set? Are there uses of FunctionType where having the default extension set be empty is important?

@acl-cqc
Copy link
Contributor Author

acl-cqc commented Jun 24, 2024

On #1195, @ss2165 wrote:

Thoughts on builder interface:

  • Could a potential simplification be just to require FunctionType inputs wherever possible (rather than in_types + out_types) and then make the default extension set in FunctionType TO_BE_INFERRED. Then with_extension_delta on FunctionType would overwrite the default set? Are there uses of FunctionType where having the default extension set be empty is important?

On the latter - yes: every time a FunctionType is used inside a Type::new_function, say, or for a FuncDefn (which we don't infer, but I suppose could - just requires a more complex algorithm that builds a call graph and looks for cycles), or a FuncDecl (which we definitely can't infer). Also not for OpDef signatures...

Moreover whilst I agree that the builder should default to inference I'm less sure about Hugr's built directly without the builder. One thing we could do though would be

  • Rename FunctionType::new (i.e. empty-delta) to ::new_pure
  • Add FunctionType::new taking 2*TypeRow + ExtensionSet
  • Same for new_endo / new_pure_endo
  • Add FunctionType::new_inferred (or maybe a shorter name) taking 2*TypeRow (and setting TO_BE_INFERRED)
  • And ::new_inferred_endo

However whilst fine for directly-constructed Hugrs (inference is available at reduced cost but still slightly more than being explicit, which feels right) this still leaves the builder wanting.

I also thought about making the builder methods take not a FunctionType but an impl Into<FTSpec> (for some new type FTSpec) where we impl From<FunctionType> for FTSpec (providing all the existing interfaces from before this PR), but then we can also have impl From<(TypeRow, TypeRow)> for FTSpec (with extensions TO_BE_INFERRED) and impl From<TypeRow> for FTSpec (endomorphic TO_BE_INFERRED). However the problem with this is that we rely heavily (e.g. in FunctionType::new) on impl Into<TypeRow> with all sorts of handy conversions there (e.g. from a single Type or Vec<Type>) which we'd not be able to use. (I think the Rust foreign-trait impl rule prevents us from defining transitive conversions - we can't have impl <T: Into<TypeRow>> From<T> for FTSpec as we might like.)

Hence I think specific builder functions are the right way to go...

Base automatically changed from acl-cqc/infer_deltas to main June 24, 2024 16:14
@acl-cqc acl-cqc force-pushed the acl/builder_infer branch from d4a3af2 to 7b70f00 Compare June 24, 2024 16:44
@acl-cqc acl-cqc requested a review from ss2165 June 24, 2024 16:57
@acl-cqc
Copy link
Contributor Author

acl-cqc commented Jun 24, 2024

@ss2165 sending this your way because you looked at the original (#1195) from which this was split. Feel free to reassign....

@acl-cqc acl-cqc marked this pull request as ready for review June 24, 2024 16:57
@acl-cqc acl-cqc requested a review from a team as a code owner June 24, 2024 16:57
Copy link

codecov bot commented Jun 24, 2024

Codecov Report

Attention: Patch coverage is 78.37838% with 8 lines in your changes missing coverage. Please review.

Project coverage is 86.98%. Comparing base (c05edd3) to head (d4d1c4e).

Files Patch % Lines
hugr-core/src/builder/dataflow.rs 0.00% 0 Missing and 3 partials ⚠️
hugr-core/src/hugr/rewrite/inline_dfg.rs 0.00% 0 Missing and 3 partials ⚠️
hugr-core/src/hugr/validate/test.rs 75.00% 0 Missing and 1 partial ⚠️
hugr-core/src/ops/constant.rs 75.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1219      +/-   ##
==========================================
- Coverage   86.99%   86.98%   -0.02%     
==========================================
  Files         100      100              
  Lines       18975    18948      -27     
  Branches    16992    16965      -27     
==========================================
- Hits        16508    16481      -27     
  Misses       1689     1689              
  Partials      778      778              
Flag Coverage Δ
rust 86.46% <78.37%> (-0.03%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

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

@acl-cqc
Copy link
Contributor Author

acl-cqc commented Jun 25, 2024

Hmmm, I'm starting to think that having a new type BuilderFunctionTypeSpec (call it FTSpec or ContainerSpec or something maybe) with factory methods new_endo(impl Into<TypeRow>), new(impl Into<TypeRow>, impl Into<TypeRow>), new_fixed_exts(impl Into<TypeRow>, impl Into<TypeRow>, impl Into<ExtensionSet>), maybe alsonew_endo_fixed_exts might be the best way to go. (new_fixed_exts could also take a FunctionType but taking the three params will require less typing at use sites).

Roughly, we're gonna want to change the builder interface for DFGs, CFGs, and simple-basic-blocks, also non-simple basic blocks + Conditionals + TailLoops (these have an extra row of common inputs), that's gonna be a lot of new methods if we add _endo and _ft variants of each one...

@acl-cqc acl-cqc force-pushed the acl/builder_infer branch from 0e6fd1e to d4d1c4e Compare June 25, 2024 17:14
@acl-cqc acl-cqc changed the title feat: Builder uses inference for deltas of nested DFGs feat: Ease requesting inference e.g. for builder Jun 25, 2024
@acl-cqc
Copy link
Contributor Author

acl-cqc commented Jun 25, 2024

Ok, redone with a couple of helper methods. I admit/agree that ft1 and ft2 are extremely terse names...but that is part of the point.... ;-). (foo,) where foo is a row/type/etc. would be even shorter, but I've not gone there...(I could definitely be persuaded!)

@acl-cqc acl-cqc changed the title feat: Ease requesting inference e.g. for builder feat: Helper functions for requesting inference, use with builder in tests Jun 25, 2024
Copy link
Member

@ss2165 ss2165 left a comment

Choose a reason for hiding this comment

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

🎉

// A box which adds extensions A and B, via child Lift nodes
let mut add_ab = parent.dfg_builder(add_ab_sig, [w])?;
let mut add_ab = parent.dfg_builder(ft1(BIT), [w])?;
Copy link
Member

Choose a reason for hiding this comment

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

nice

@acl-cqc acl-cqc added this pull request to the merge queue Jun 28, 2024
Merged via the queue into main with commit 320a9a7 Jun 28, 2024
25 of 26 checks passed
@acl-cqc acl-cqc deleted the acl/builder_infer branch June 28, 2024 08:27
@hugrbot hugrbot mentioned this pull request Jun 28, 2024
@hugrbot hugrbot mentioned this pull request Jun 28, 2024
github-merge-queue bot pushed a commit that referenced this pull request Jul 1, 2024
## 🤖 New release
* `hugr`: 0.5.1 -> 0.6.0
* `hugr-core`: 0.2.0 -> 0.3.0
* `hugr-passes`: 0.2.0 -> 0.3.0
* `hugr-cli`: 0.1.1 -> 0.1.2

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

## `hugr`
<blockquote>

## 0.6.0 (2024-06-28)

### Bug Fixes

- SimpleReplacement panic on multiports
([#1191](#1191))
- Add some validation for const nodes
([#1222](#1222))
- Cfg not validating entry/exit types
([#1229](#1229))
- `extract_hugr` not removing root node ports
([#1239](#1239))

### Documentation

- Fix documentation of `ValidationError::ConstTypeError`
([#1227](#1227))

### Features

- CircuitBuilder::add_constant
([#1168](#1168))
- [**breaking**] Make the rewrite errors more useful
([#1174](#1174))
- [**breaking**] Validate Extensions using hierarchy, ignore
input_extensions, RIP inference
([#1142](#1142))
- [**breaking**] Infer extension deltas for Case, Cfg, Conditional,
DataflowBlock, Dfg, TailLoop
([#1195](#1195))
- Helper functions for requesting inference, use with builder in tests
([#1219](#1219))

### Refactor

- [**breaking**] FunctionBuilder takes impl Into<PolyFuncType>
([#1220](#1220))
- [**breaking**] Remove NodeType and input_extensions
([#1183](#1183))
</blockquote>

## `hugr-core`
<blockquote>

## 0.3.0 (2024-06-28)

### Bug Fixes

- SimpleReplacement panic on multiports
([#1191](#1191))
- Add some validation for const nodes
([#1222](#1222))
- Cfg not validating entry/exit types
([#1229](#1229))
- `extract_hugr` not removing root node ports
([#1239](#1239))

### Documentation

- Fix documentation of `ValidationError::ConstTypeError`
([#1227](#1227))

### Features

- CircuitBuilder::add_constant
([#1168](#1168))
- [**breaking**] Make the rewrite errors more useful
([#1174](#1174))
- [**breaking**] Validate Extensions using hierarchy, ignore
input_extensions, RIP inference
([#1142](#1142))
- [**breaking**] Infer extension deltas for Case, Cfg, Conditional,
DataflowBlock, Dfg, TailLoop
([#1195](#1195))
- Helper functions for requesting inference, use with builder in tests
([#1219](#1219))

### Refactor

- [**breaking**] Remove NodeType and input_extensions
([#1183](#1183))
- [**breaking**] FunctionBuilder takes impl Into<PolyFuncType>
([#1220](#1220))
</blockquote>

## `hugr-passes`
<blockquote>

## 0.3.0 (2024-06-28)

### Features

- [**breaking**] Validate Extensions using hierarchy, ignore
input_extensions, RIP inference
([#1142](#1142))
- Helper functions for requesting inference, use with builder in tests
([#1219](#1219))

</blockquote>

## `hugr-cli`
<blockquote>

## 0.1.1 (2024-06-07)

### Features

- Reexport `clap::Parser` and `clap_verbosity_flag::Level` from hugr_cli
([#1146](#1146))

### Refactor

- Move binary to hugr-cli
([#1134](#1134))
</blockquote>


</p></details>

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

---------

Co-authored-by: Douglas Wilson <[email protected]>
@hugrbot hugrbot mentioned this pull request Jul 1, 2024
github-merge-queue bot pushed a commit that referenced this pull request Aug 2, 2024
closes #1318 and also deals with BlockBuilder not mentioned there.
I think this is now all of the nested-structures covered:
| | XBuilder::new | fn x |
|----|----|----|
|Conditional|**here**|in #1226|
|\->Case|takes `Signature`|inherits exts|
|TailLoop|**here**|**here**|
|DFG|takes `Signature`|takes `Signature` (`dfg_builder_endo` in #1219)|
|CFG|takes `Signature`|**here**|
|\->Block|**here**|in #1226|

(FuncDefn takes `Signature` and is *not supported by inference yet*
anyway)

BREAKING CHANGE: `cfg_builder`, `tail_loop_builder`,
`ConditionalBuilder::new`, `BlockBuilder::new` and
`TailLoopBuilder::new` no longer take an ExtensionSet parameter; either
remove the argument (to use extension inference) or use the `_exts`
variant
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.

2 participants