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: move rewrite inside hugr, Rewrite -> Replace implementing new 'Rewrite' trait #119

Merged
merged 28 commits into from
Jun 19, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
80703e1
Remove Pattern
acl-cqc Jun 5, 2023
9ef30ad
Remove Pattern.rs, too skeletal to be useful
acl-cqc Jun 5, 2023
75b9f1f
Move rewrite to hugr/replace
acl-cqc Jun 5, 2023
5c5662f
Add RewriteOp enum, move Hugr code into RewriteOp::apply (a big match)
acl-cqc Jun 5, 2023
de70383
Make into a trait. So it has to be public...so what?
acl-cqc Jun 5, 2023
3a534e6
Parametrize by error type
acl-cqc Jun 5, 2023
55f83ba
fmt
acl-cqc Jun 5, 2023
76d13fe
Rename hugr/replace/{rewrite.rs -> replace.rs}
acl-cqc Jun 6, 2023
6e8b152
Rename src/hugr/{replace=>rewrite}(,.rs)
acl-cqc Jun 6, 2023
99b31ce
Rename Rewrite(Error) to Replace(Error)
acl-cqc Jun 6, 2023
070afdd
Rename RewriteOp to Rewrite
acl-cqc Jun 6, 2023
7348e11
Hugr::apply -> apply_rewrite
acl-cqc Jun 6, 2023
9349aa4
Merge remote-tracking branch 'origin/main' into refactor/replace_trait
acl-cqc Jun 6, 2023
ec8354e
Add may_fail_destructively check, default true, and Transactional wra…
acl-cqc Jun 6, 2023
147c937
is_err
acl-cqc Jun 6, 2023
9f1d382
unchanged_on_failure as trait associated constant
acl-cqc Jun 7, 2023
f0b5b82
Rephrase assert/debug_assert
acl-cqc Jun 7, 2023
45c5fc2
Merge remote-tracking branch 'origin/main' into refactor/replace_trait
acl-cqc Jun 7, 2023
8f8fcae
Merge remote-tracking branch 'origin/main' into refactor/replace_trait
acl-cqc Jun 9, 2023
4604c70
Move SimpleReplacement inside rewrite, move Hugr::apply_simple_replac…
acl-cqc Jun 9, 2023
a83a93e
unused variable
acl-cqc Jun 9, 2023
0291bba
Drive-by: simple_replace.rs: change ".ok();"s to unwrap
acl-cqc Jun 9, 2023
3572933
Merge remote-tracking branch 'origin/main' into refactor/replace_trait
acl-cqc Jun 9, 2023
04c8777
WIP
acl-cqc Jun 19, 2023
6070e20
Fix merge
acl-cqc Jun 19, 2023
4202f5e
Review comments
acl-cqc Jun 19, 2023
4553395
todo -> unimplemented, the plan is not necessarily to do these
acl-cqc Jun 19, 2023
0201233
fmt
acl-cqc Jun 19, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 4 additions & 24 deletions src/hugr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
mod hugrmut;
pub mod view;

pub mod replace;
pub mod serialize;
pub mod validate;

use derive_more::From;
pub use hugrmut::HugrMut;
pub use replace::{Rewrite, RewriteError, RewriteOp};
pub use validate::ValidationError;

use portgraph::dot::{hier_graph_dot_string_with, DotEdgeStyle};
Expand All @@ -16,7 +18,6 @@ use thiserror::Error;

pub use self::view::HugrView;
use crate::ops::{ModuleOp, OpType};
use crate::rewrite::{Rewrite, RewriteError};
use crate::types::EdgeKind;

use html_escape::encode_text_to_string;
Expand Down Expand Up @@ -74,29 +75,8 @@ impl Hugr {
}

/// Applies a rewrite to the graph.
pub fn apply_rewrite(mut self, rewrite: Rewrite) -> Result<(), RewriteError> {
// Get the open graph for the rewrites, and a HUGR with the additional components.
let (rewrite, mut replacement, parents) = rewrite.into_parts();

// TODO: Use `parents` to update the hierarchy, and keep the internal hierarchy from `replacement`.
let _ = parents;

let node_inserted = |old, new| {
std::mem::swap(&mut self.op_types[new], &mut replacement.op_types[old]);
// TODO: metadata (Fn parameter ?)
};
rewrite.apply_with_callbacks(
&mut self.graph,
|_| {},
|_| {},
node_inserted,
|_, _| {},
|_, _| {},
)?;

// TODO: Check types

Ok(())
pub fn apply<E>(&mut self, op: impl RewriteOp<E>) -> Result<(), E> {
op.apply(self)
}

/// Return dot string showing underlying graph and hierarchy side by side.
Expand Down
14 changes: 14 additions & 0 deletions src/hugr/replace.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//! Rewrite operations on the HUGR.

#[allow(clippy::module_inception)] // TODO: Rename?
pub mod rewrite;
use crate::Hugr;
pub use rewrite::{OpenHugr, Rewrite, RewriteError};

/// An operation that can be applied to mutate a Hugr
pub trait RewriteOp<E> {
/// Mutate the specified Hugr, or fail with an error.
/// Implementations are strongly encouraged not to mutate the Hugr
/// if they return an Err.
fn apply(self, h: &mut Hugr) -> Result<(), E>;
}
28 changes: 28 additions & 0 deletions src/rewrite/rewrite.rs → src/hugr/replace/rewrite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use portgraph::substitute::OpenGraph;
use portgraph::{NodeIndex, PortIndex};
use thiserror::Error;

use super::RewriteOp;
use crate::Hugr;

/// A subset of the nodes in a graph, and the ports that it is connected to.
Expand Down Expand Up @@ -131,6 +132,33 @@ impl Rewrite {
}
}

impl RewriteOp<RewriteError> for Rewrite {
/// Applies a ReplacementOp to the graph.
fn apply(self, h: &mut Hugr) -> Result<(), RewriteError> {
// Get the open graph for the rewrites, and a HUGR with the additional components.
let (rewrite, mut replacement, parents) = self.into_parts();

// TODO: Use `parents` to update the hierarchy, and keep the internal hierarchy from `replacement`.
let _ = parents;

let node_inserted = |old, new| {
std::mem::swap(&mut h.op_types[new], &mut replacement.op_types[old]);
// TODO: metadata (Fn parameter ?)
};
rewrite.apply_with_callbacks(
&mut h.graph,
|_| {},
|_| {},
node_inserted,
|_, _| {},
|_, _| {},
)?;

// TODO: Check types
Ok(())
}
}

/// Error generated when a rewrite fails.
#[derive(Debug, Clone, Error, PartialEq, Eq)]
pub enum RewriteError {
Expand Down
4 changes: 1 addition & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@ pub mod hugr;
pub mod macros;
pub mod ops;
pub mod resource;
pub mod rewrite;
pub mod types;
mod utils;

pub use crate::hugr::{Direction, Hugr, Node, Port, Wire};
pub use crate::hugr::{Direction, Hugr, Node, Port, Rewrite, RewriteError, Wire};
pub use crate::resource::Resource;
pub use crate::rewrite::{Rewrite, RewriteError};
7 changes: 0 additions & 7 deletions src/rewrite.rs

This file was deleted.

10 changes: 0 additions & 10 deletions src/rewrite/pattern.rs

This file was deleted.