-
Notifications
You must be signed in to change notification settings - Fork 7
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: move rewrite
inside hugr
, Rewrite
-> Replace
implementing new 'Rewrite' trait
#119
Merged
Merged
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
80703e1
Remove Pattern
acl-cqc 9ef30ad
Remove Pattern.rs, too skeletal to be useful
acl-cqc 75b9f1f
Move rewrite to hugr/replace
acl-cqc 5c5662f
Add RewriteOp enum, move Hugr code into RewriteOp::apply (a big match)
acl-cqc de70383
Make into a trait. So it has to be public...so what?
acl-cqc 3a534e6
Parametrize by error type
acl-cqc 55f83ba
fmt
acl-cqc 76d13fe
Rename hugr/replace/{rewrite.rs -> replace.rs}
acl-cqc 6e8b152
Rename src/hugr/{replace=>rewrite}(,.rs)
acl-cqc 99b31ce
Rename Rewrite(Error) to Replace(Error)
acl-cqc 070afdd
Rename RewriteOp to Rewrite
acl-cqc 7348e11
Hugr::apply -> apply_rewrite
acl-cqc 9349aa4
Merge remote-tracking branch 'origin/main' into refactor/replace_trait
acl-cqc ec8354e
Add may_fail_destructively check, default true, and Transactional wra…
acl-cqc 147c937
is_err
acl-cqc 9f1d382
unchanged_on_failure as trait associated constant
acl-cqc f0b5b82
Rephrase assert/debug_assert
acl-cqc 45c5fc2
Merge remote-tracking branch 'origin/main' into refactor/replace_trait
acl-cqc 8f8fcae
Merge remote-tracking branch 'origin/main' into refactor/replace_trait
acl-cqc 4604c70
Move SimpleReplacement inside rewrite, move Hugr::apply_simple_replac…
acl-cqc a83a93e
unused variable
acl-cqc 0291bba
Drive-by: simple_replace.rs: change ".ok();"s to unwrap
acl-cqc 3572933
Merge remote-tracking branch 'origin/main' into refactor/replace_trait
acl-cqc 04c8777
WIP
acl-cqc 6070e20
Fix merge
acl-cqc 4202f5e
Review comments
acl-cqc 4553395
todo -> unimplemented, the plan is not necessarily to do these
acl-cqc 0201233
fmt
acl-cqc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
//! Rewrite operations on the HUGR - replacement, outlining, etc. | ||
|
||
pub mod replace; | ||
pub mod simple_replace; | ||
use std::mem; | ||
|
||
use crate::Hugr; | ||
pub use replace::{OpenHugr, Replace, ReplaceError}; | ||
pub use simple_replace::{SimpleReplacement, SimpleReplacementError}; | ||
|
||
/// An operation that can be applied to mutate a Hugr | ||
pub trait Rewrite { | ||
/// The type of Error with which this Rewrite may fail | ||
type Error: std::error::Error; | ||
|
||
/// If `true`, [self.apply]'s of this rewrite guarantee that they do not mutate the Hugr when they return an Err. | ||
/// If `false`, there is no guarantee; the Hugr should be assumed invalid when Err is returned. | ||
const UNCHANGED_ON_FAILURE: bool; | ||
|
||
/// Checks whether the rewrite would succeed on the specified Hugr. | ||
/// If this call succeeds, [self.apply] should also succeed on the same `h` | ||
/// If this calls fails, [self.apply] would fail with the same error. | ||
fn verify(&self, h: &Hugr) -> Result<(), Self::Error>; | ||
|
||
/// Mutate the specified Hugr, or fail with an error. | ||
/// If [self.unchanged_on_failure] is true, then `h` must be unchanged if Err is returned. | ||
/// See also [self.verify] | ||
/// # Panics | ||
/// May panic if-and-only-if `h` would have failed [Hugr::validate]; that is, | ||
/// implementations may begin with `assert!(h.validate())`, with `debug_assert!(h.validate())` | ||
/// being preferred. | ||
fn apply(self, h: &mut Hugr) -> Result<(), Self::Error>; | ||
} | ||
|
||
/// Wraps any rewrite into a transaction (i.e. that has no effect upon failure) | ||
pub struct Transactional<R> { | ||
underlying: R, | ||
} | ||
|
||
// Note we might like to constrain R to Rewrite<unchanged_on_failure=false> but this | ||
// is not yet supported, https://github.com/rust-lang/rust/issues/92827 | ||
impl<R: Rewrite> Rewrite for Transactional<R> { | ||
type Error = R::Error; | ||
const UNCHANGED_ON_FAILURE: bool = true; | ||
|
||
fn verify(&self, h: &Hugr) -> Result<(), Self::Error> { | ||
self.underlying.verify(h) | ||
} | ||
|
||
fn apply(self, h: &mut Hugr) -> Result<(), Self::Error> { | ||
if R::UNCHANGED_ON_FAILURE { | ||
return self.underlying.apply(h); | ||
} | ||
let backup = h.clone(); | ||
let r = self.underlying.apply(h); | ||
if r.is_err() { | ||
// drop the old h, it was undefined | ||
let _ = mem::replace(h, backup); | ||
} | ||
r | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
An alternative is to return
Result<(), Option<E>>
where returning Err(None) means, no guarantee can be given aboutapply
. If a rewrite really has to get partway through in order to complete its own validity checks then said alternative would support that. But I think we should leave asResult<(), E>
until/unless we find a Rewrite where that's difficult.(Compound Rewrites is one such case. For a sequence of rewrites,
verify
might have to clone() the Hugr, and then step through the sequence,apply
ing each rewrite beforeverify
ing the next.)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'd say custom enums with
{Recoverable, NonRecoverable}
variants rather thanOption<E>
.But yeah, let's keep it simple for now.