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: resolve extension ops (mutating Hugr) in (infer_and_->)update_validate #603

Merged
merged 3 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion src/builder/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ impl HugrBuilder for CFGBuilder<Hugr> {
mut self,
extension_registry: &ExtensionRegistry,
) -> Result<Hugr, crate::hugr::ValidationError> {
self.base.infer_and_validate(extension_registry)?;
self.base.update_validate(extension_registry)?;
Ok(self.base)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/builder/conditional.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ impl HugrBuilder for ConditionalBuilder<Hugr> {
mut self,
extension_registry: &ExtensionRegistry,
) -> Result<Hugr, crate::hugr::ValidationError> {
self.base.infer_and_validate(extension_registry)?;
self.base.update_validate(extension_registry)?;
Ok(self.base)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/builder/dataflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ impl HugrBuilder for DFGBuilder<Hugr> {
mut self,
extension_registry: &ExtensionRegistry,
) -> Result<Hugr, ValidationError> {
self.base.infer_and_validate(extension_registry)?;
self.base.update_validate(extension_registry)?;
Ok(self.base)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/builder/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl HugrBuilder for ModuleBuilder<Hugr> {
mut self,
extension_registry: &ExtensionRegistry,
) -> Result<Hugr, ValidationError> {
self.0.infer_and_validate(extension_registry)?;
self.0.update_validate(extension_registry)?;
Ok(self.0)
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/extension/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -867,7 +867,7 @@ mod test {
// nodes and their parents and `report_mismatch` isn't yet smart enough
// to handle that.
assert_matches!(
hugr.infer_and_validate(&PRELUDE_REGISTRY),
hugr.update_validate(&PRELUDE_REGISTRY),
Err(ValidationError::CantInfer(_))
);
Ok(())
Expand Down Expand Up @@ -1155,7 +1155,7 @@ mod test {
for (src, tgt) in nodes.into_iter().tuple_windows() {
hugr.connect(src, 0, tgt, 0)?;
}
hugr.infer_and_validate(&PRELUDE_REGISTRY)?;
hugr.update_validate(&PRELUDE_REGISTRY)?;
Ok(())
}

Expand Down Expand Up @@ -1421,7 +1421,7 @@ mod test {
hugr.connect(bb1, 0, bb2, 0)?;
hugr.connect(bb2, 0, exit, 0)?;

hugr.infer_and_validate(&PRELUDE_REGISTRY)?;
hugr.update_validate(&PRELUDE_REGISTRY)?;

Ok(())
}
Expand Down Expand Up @@ -1520,7 +1520,7 @@ mod test {

for (bb0, bb1, bb2) in variants.into_iter() {
let mut hugr = make_looping_cfg(bb0, bb1, bb2)?;
hugr.infer_and_validate(&PRELUDE_REGISTRY)?;
hugr.update_validate(&PRELUDE_REGISTRY)?;
}
Ok(())
}
Expand Down Expand Up @@ -1573,7 +1573,7 @@ mod test {
hugr.connect(bb, 0, bb, 0)?;
hugr.connect(bb, 0, exit, 0)?;

hugr.infer_and_validate(&PRELUDE_REGISTRY)?;
hugr.update_validate(&PRELUDE_REGISTRY)?;

Ok(())
}
Expand Down
6 changes: 4 additions & 2 deletions src/hugr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub use self::views::{HugrView, RootTagged};
use crate::extension::{
infer_extensions, ExtensionRegistry, ExtensionSet, ExtensionSolution, InferExtensionError,
};
use crate::ops::custom::resolve_extension_ops;
use crate::ops::{OpTag, OpTrait, OpType, DEFAULT_OPTYPE};
use crate::types::{FunctionType, Signature};

Expand Down Expand Up @@ -230,11 +231,12 @@ pub type Direction = portgraph::Direction;

/// Public API for HUGRs.
impl Hugr {
/// Run resource inference and pass the closure into validation
pub fn infer_and_validate(
/// Resolve extension ops, infer extensions used, and pass the closure into validation
pub fn update_validate(
&mut self,
extension_registry: &ExtensionRegistry,
) -> Result<(), ValidationError> {
resolve_extension_ops(self, extension_registry)?;
let closure = self.infer_extensions()?;
self.validate_with_extension_closure(closure, extension_registry)?;
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion src/hugr/rewrite/insert_identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ mod tests {

assert_eq!(noop, LeafOp::Noop { ty: QB_T });

h.infer_and_validate(&PRELUDE_REGISTRY).unwrap();
h.update_validate(&PRELUDE_REGISTRY).unwrap();
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion src/hugr/rewrite/outline_cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ mod test {
#[test]
fn test_outline_cfg() {
let (mut h, head, tail) = build_conditional_in_loop_cfg(false).unwrap();
h.infer_and_validate(&PRELUDE_REGISTRY).unwrap();
h.update_validate(&PRELUDE_REGISTRY).unwrap();
do_outline_cfg_test(&mut h, head, tail, 1);
h.validate(&PRELUDE_REGISTRY).unwrap();
}
Expand Down
4 changes: 2 additions & 2 deletions src/hugr/rewrite/simple_replace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ pub(in crate::hugr::rewrite) mod test {
// ├───┤├───┤┌─┴─┐
// ┤ H ├┤ H ├┤ X ├
// └───┘└───┘└───┘
assert_eq!(h.infer_and_validate(&PRELUDE_REGISTRY), Ok(()));
assert_eq!(h.update_validate(&PRELUDE_REGISTRY), Ok(()));
}

#[rstest]
Expand Down Expand Up @@ -488,7 +488,7 @@ pub(in crate::hugr::rewrite) mod test {
// ├───┤├───┤┌───┐
// ┤ H ├┤ H ├┤ H ├
// └───┘└───┘└───┘
assert_eq!(h.infer_and_validate(&PRELUDE_REGISTRY), Ok(()));
assert_eq!(h.update_validate(&PRELUDE_REGISTRY), Ok(()));
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion src/hugr/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ pub mod test {
hugr.connect(new_in, 0, out, 0).unwrap();
hugr.move_before_sibling(new_in, old_in).unwrap();
hugr.remove_node(old_in).unwrap();
hugr.infer_and_validate(&PRELUDE_REGISTRY).unwrap();
hugr.update_validate(&PRELUDE_REGISTRY).unwrap();

let ser = serde_json::to_vec(&hugr).unwrap();
let new_hugr: Hugr = serde_json::from_slice(&ser).unwrap();
Expand Down
10 changes: 5 additions & 5 deletions src/hugr/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1113,20 +1113,20 @@ mod test {
h.connect(sub_dfg, 0, output, 0)?;

assert_matches!(
h.infer_and_validate(&EMPTY_REG),
h.update_validate(&EMPTY_REG),
Err(ValidationError::UnconnectedPort { .. })
);

h.connect(input, 1, sub_op, 1)?;
assert_matches!(
h.infer_and_validate(&EMPTY_REG),
h.update_validate(&EMPTY_REG),
Err(ValidationError::InterGraphEdgeError(
InterGraphEdgeError::MissingOrderEdge { .. }
))
);
//Order edge. This will need metadata indicating its purpose.
h.add_other_edge(input, sub_dfg)?;
h.infer_and_validate(&EMPTY_REG).unwrap();
h.update_validate(&EMPTY_REG).unwrap();
Ok(())
}

Expand All @@ -1143,7 +1143,7 @@ mod test {
h.connect(input, 0, and, 0)?;
h.connect(and, 0, output, 0)?;
assert_eq!(
h.infer_and_validate(&EMPTY_REG),
h.update_validate(&EMPTY_REG),
Err(ValidationError::UnconnectedPort {
node: and,
port: Port::new_incoming(1),
Expand All @@ -1161,7 +1161,7 @@ mod test {
h.connect(cst, 0, lcst, 0)?;
h.connect(lcst, 0, and, 1)?;
// There is no edge from Input to LoadConstant, but that's OK:
h.infer_and_validate(&EMPTY_REG).unwrap();
h.update_validate(&EMPTY_REG).unwrap();
Ok(())
}

Expand Down
4 changes: 1 addition & 3 deletions src/hugr/views/sibling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,9 +437,7 @@ mod test {

#[rstest]
fn flat_mut(mut simple_dfg_hugr: Hugr) {
simple_dfg_hugr
.infer_and_validate(&PRELUDE_REGISTRY)
.unwrap();
simple_dfg_hugr.update_validate(&PRELUDE_REGISTRY).unwrap();
let root = simple_dfg_hugr.root();
let signature = simple_dfg_hugr.get_function_type().unwrap().clone();

Expand Down