Skip to content

Commit

Permalink
refactor: remove add_op_<posn> by generalizing add_node_<posn> with "…
Browse files Browse the repository at this point in the history
…impl Into" (#642)

* Add a mildly-funky `Into` conversion from OpType to NodeType using the
`new_auto` added in #635
* Generalize add_node_(with_parent,etc.) methods to take `impl
Into<NodeType>`, and then remove corresponding `add_op_....`
* `add_op_after` had no corresponding `add_node_after`, so just turn it
into that
* leave Hugr::add_node in simple form, without the `impl Into"

BREAKING CHANGE: add_op_{with_parent,before,after} removed => use
add_node_....
  • Loading branch information
acl-cqc committed Nov 8, 2023
1 parent a13b5ef commit a6dee37
Show file tree
Hide file tree
Showing 19 changed files with 190 additions and 214 deletions.
4 changes: 2 additions & 2 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,14 @@ pub(crate) mod test {
let mut hugr = Hugr::new(NodeType::new_pure(ops::DFG {
signature: signature.clone(),
}));
hugr.add_op_with_parent(
hugr.add_node_with_parent(
hugr.root(),
ops::Input {
types: signature.input,
},
)
.unwrap();
hugr.add_op_with_parent(
hugr.add_node_with_parent(
hugr.root(),
ops::Output {
types: signature.output,
Expand Down
17 changes: 5 additions & 12 deletions src/builder/build_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub trait Container {
/// Add an [`OpType`] as the final child of the container.
fn add_child_op(&mut self, op: impl Into<OpType>) -> Result<Node, BuildError> {
let parent = self.container_node();
Ok(self.hugr_mut().add_op_with_parent(parent, op)?)
Ok(self.hugr_mut().add_node_with_parent(parent, op)?)
}
/// Add a [`NodeType`] as the final child of the container.
fn add_child_node(&mut self, node: NodeType) -> Result<Node, BuildError> {
Expand Down Expand Up @@ -416,7 +416,7 @@ pub trait Dataflow: Container {
rest: rest_types.into(),
};
// TODO: Make input extensions a parameter
let (loop_node, _) = add_op_with_wires(self, tail_loop.clone(), input_wires)?;
let (loop_node, _) = add_node_with_wires(self, tail_loop.clone(), input_wires)?;

TailLoopBuilder::create_with_io(self.hugr_mut(), loop_node, &tail_loop)
}
Expand Down Expand Up @@ -623,21 +623,14 @@ pub trait Dataflow: Container {
}
}

fn add_op_with_wires<T: Dataflow + ?Sized>(
data_builder: &mut T,
optype: impl Into<OpType>,
inputs: Vec<Wire>,
) -> Result<(Node, usize), BuildError> {
add_node_with_wires(data_builder, NodeType::new_auto(optype), inputs)
}

fn add_node_with_wires<T: Dataflow + ?Sized>(
data_builder: &mut T,
nodetype: NodeType,
nodetype: impl Into<NodeType>,
inputs: Vec<Wire>,
) -> Result<(Node, usize), BuildError> {
let op_node = data_builder.add_child_node(nodetype.clone())?;
let nodetype = nodetype.into();
let sig = nodetype.op_signature();
let op_node = data_builder.add_child_node(nodetype)?;

wire_up_inputs(inputs, op_node, data_builder)?;

Expand Down
6 changes: 3 additions & 3 deletions src/builder/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ impl<B: AsMut<Hugr> + AsRef<Hugr>> CFGBuilder<B> {
let exit_node = base
.as_mut()
// Make the extensions a parameter
.add_op_with_parent(cfg_node, exit_block_type)?;
.add_node_with_parent(cfg_node, exit_block_type)?;
Ok(Self {
base,
cfg_node,
Expand Down Expand Up @@ -144,10 +144,10 @@ impl<B: AsMut<Hugr> + AsRef<Hugr>> CFGBuilder<B> {
let block_n = if entry {
let exit = self.exit_node;
// TODO: Make extensions a parameter
self.hugr_mut().add_op_before(exit, op)
self.hugr_mut().add_node_before(exit, op)
} else {
// TODO: Make extensions a parameter
self.hugr_mut().add_op_with_parent(parent, op)
self.hugr_mut().add_node_with_parent(parent, op)
}?;

BlockBuilder::create(
Expand Down
2 changes: 1 addition & 1 deletion src/builder/conditional.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ impl<B: AsMut<Hugr> + AsRef<Hugr>> ConditionalBuilder<B> {
let case_node =
// add case before any existing subsequent cases
if let Some(&sibling_node) = self.case_nodes[case + 1..].iter().flatten().next() {
self.hugr_mut().add_op_before(sibling_node, case_op)?
self.hugr_mut().add_node_before(sibling_node, case_op)?
} else {
self.add_child_op(case_op)?
};
Expand Down
52 changes: 26 additions & 26 deletions src/extension/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -698,8 +698,8 @@ mod test {
let input = ops::Input::new(type_row![NAT, NAT]);
let output = ops::Output::new(type_row![NAT]);

let input = hugr.add_op_with_parent(hugr.root(), input)?;
let output = hugr.add_op_with_parent(hugr.root(), output)?;
let input = hugr.add_node_with_parent(hugr.root(), input)?;
let output = hugr.add_node_with_parent(hugr.root(), output)?;

assert_matches!(hugr.get_io(hugr.root()), Some(_));

Expand All @@ -715,25 +715,25 @@ mod test {
let mult_c_sig = FunctionType::new(type_row![NAT, NAT], type_row![NAT])
.with_extension_delta(&ExtensionSet::singleton(&C));

let add_a = hugr.add_op_with_parent(
let add_a = hugr.add_node_with_parent(
hugr.root(),
ops::DFG {
signature: add_a_sig,
},
)?;
let add_b = hugr.add_op_with_parent(
let add_b = hugr.add_node_with_parent(
hugr.root(),
ops::DFG {
signature: add_b_sig,
},
)?;
let add_ab = hugr.add_op_with_parent(
let add_ab = hugr.add_node_with_parent(
hugr.root(),
ops::DFG {
signature: add_ab_sig,
},
)?;
let mult_c = hugr.add_op_with_parent(
let mult_c = hugr.add_node_with_parent(
hugr.root(),
ops::DFG {
signature: mult_c_sig,
Expand Down Expand Up @@ -877,7 +877,7 @@ mod test {
let [input, output] = hugr.get_io(hugr.root()).unwrap();
let add_r_sig = FunctionType::new(type_row![NAT], type_row![NAT]).with_extension_delta(&rs);

let add_r = hugr.add_op_with_parent(
let add_r = hugr.add_node_with_parent(
hugr.root(),
ops::DFG {
signature: add_r_sig,
Expand All @@ -888,11 +888,11 @@ mod test {
let src_sig = FunctionType::new(type_row![], type_row![NAT])
.with_extension_delta(&ExtensionSet::new());

let src = hugr.add_op_with_parent(hugr.root(), ops::DFG { signature: src_sig })?;
let src = hugr.add_node_with_parent(hugr.root(), ops::DFG { signature: src_sig })?;

let mult_sig = FunctionType::new(type_row![NAT, NAT], type_row![NAT]);
// Mult has open extension requirements, which we should solve to be "R"
let mult = hugr.add_op_with_parent(
let mult = hugr.add_node_with_parent(
hugr.root(),
ops::DFG {
signature: mult_sig,
Expand Down Expand Up @@ -956,14 +956,14 @@ mod test {
) -> Result<[Node; 3], Box<dyn Error>> {
let op: OpType = op.into();

let node = hugr.add_op_with_parent(parent, op)?;
let input = hugr.add_op_with_parent(
let node = hugr.add_node_with_parent(parent, op)?;
let input = hugr.add_node_with_parent(
node,
ops::Input {
types: op_sig.input,
},
)?;
let output = hugr.add_op_with_parent(
let output = hugr.add_node_with_parent(
node,
ops::Output {
types: op_sig.output,
Expand All @@ -988,15 +988,15 @@ mod test {
Into::<OpType>::into(op).signature(),
)?;

let lift1 = hugr.add_op_with_parent(
let lift1 = hugr.add_node_with_parent(
case,
ops::LeafOp::Lift {
type_row: type_row![NAT],
new_extension: first_ext,
},
)?;

let lift2 = hugr.add_op_with_parent(
let lift2 = hugr.add_node_with_parent(
case,
ops::LeafOp::Lift {
type_row: type_row![NAT],
Expand Down Expand Up @@ -1066,13 +1066,13 @@ mod test {
}));

let root = hugr.root();
let input = hugr.add_op_with_parent(
let input = hugr.add_node_with_parent(
root,
ops::Input {
types: type_row![NAT],
},
)?;
let output = hugr.add_op_with_parent(
let output = hugr.add_node_with_parent(
root,
ops::Output {
types: type_row![NAT],
Expand All @@ -1097,7 +1097,7 @@ mod test {
.unwrap();

let lift = hugr
.add_op_with_parent(
.add_node_with_parent(
node,
ops::LeafOp::Lift {
type_row: type_row![NAT],
Expand Down Expand Up @@ -1149,7 +1149,7 @@ mod test {

let [bb, bb_in, bb_out] = create_with_io(hugr, bb_parent, dfb, dfb_sig)?;

let dfg = hugr.add_op_with_parent(bb, op)?;
let dfg = hugr.add_node_with_parent(bb, op)?;

hugr.connect(bb_in, 0, dfg, 0)?;
hugr.connect(dfg, 0, bb_out, 0)?;
Expand Down Expand Up @@ -1181,16 +1181,16 @@ mod test {
extension_delta: entry_extensions,
};

let exit = hugr.add_op_with_parent(
let exit = hugr.add_node_with_parent(
root,
ops::BasicBlock::Exit {
cfg_outputs: exit_types.into(),
},
)?;

let entry = hugr.add_op_before(exit, dfb)?;
let entry_in = hugr.add_op_with_parent(entry, ops::Input { types: inputs })?;
let entry_out = hugr.add_op_with_parent(
let entry = hugr.add_node_before(exit, dfb)?;
let entry_in = hugr.add_node_with_parent(entry, ops::Input { types: inputs })?;
let entry_out = hugr.add_node_with_parent(
entry,
ops::Output {
types: vec![entry_tuple_sum].into(),
Expand Down Expand Up @@ -1245,7 +1245,7 @@ mod test {
type_row![NAT],
)?;

let mkpred = hugr.add_op_with_parent(
let mkpred = hugr.add_node_with_parent(
entry,
make_opaque(
A,
Expand Down Expand Up @@ -1341,7 +1341,7 @@ mod test {
type_row![NAT],
)?;

let entry_mid = hugr.add_op_with_parent(
let entry_mid = hugr.add_node_with_parent(
entry,
make_opaque(UNKNOWN_EXTENSION, FunctionType::new(vec![NAT], twoway(NAT))),
)?;
Expand Down Expand Up @@ -1427,7 +1427,7 @@ mod test {
type_row![NAT],
)?;

let entry_dfg = hugr.add_op_with_parent(
let entry_dfg = hugr.add_node_with_parent(
entry,
make_opaque(
UNKNOWN_EXTENSION,
Expand Down Expand Up @@ -1508,7 +1508,7 @@ mod test {
type_row![NAT],
)?;

let entry_mid = hugr.add_op_with_parent(
let entry_mid = hugr.add_node_with_parent(
entry,
make_opaque(UNKNOWN_EXTENSION, FunctionType::new(vec![NAT], oneway(NAT))),
)?;
Expand Down
8 changes: 4 additions & 4 deletions src/extension/op_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ impl Extension {
}

/// Create an OpDef with custom binary code to compute the signature
pub fn add_op_custom_sig(
pub fn add_node_custom_sig(
&mut self,
name: SmolStr,
description: String,
Expand All @@ -291,14 +291,14 @@ impl Extension {

/// Create an OpDef with custom binary code to compute the signature, and no "misc" or "lowering
/// functions" defined.
pub fn add_op_custom_sig_simple(
pub fn add_node_custom_sig_simple(
&mut self,
name: SmolStr,
description: String,
params: Vec<TypeParam>,
signature_func: impl CustomSignatureFunc + 'static,
) -> Result<&OpDef, ExtensionBuildError> {
self.add_op_custom_sig(
self.add_node_custom_sig(
name,
description,
params,
Expand All @@ -310,7 +310,7 @@ impl Extension {

/// Create an OpDef with a signature (inputs+outputs) read from the
/// declarative YAML
pub fn add_op_decl_sig(
pub fn add_node_decl_sig(
&mut self,
name: SmolStr,
description: String,
Expand Down
2 changes: 1 addition & 1 deletion src/extension/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ lazy_static! {
.unwrap();

prelude
.add_op_custom_sig_simple(
.add_node_custom_sig_simple(
SmolStr::new_inline(NEW_ARRAY_OP_ID),
"Create a new array from elements".to_string(),
vec![TypeParam::Type(TypeBound::Any), TypeParam::max_nat()],
Expand Down
17 changes: 4 additions & 13 deletions src/hugr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,9 @@ impl NodeType {
}
}

impl OpType {
/// Convert an OpType to a NodeType by giving it some input extensions
pub fn with_extensions(self, rs: ExtensionSet) -> NodeType {
NodeType {
op: self,
input_extensions: Some(rs),
}
impl<T: Into<OpType>> From<T> for NodeType {
fn from(value: T) -> Self {
NodeType::new_auto(value.into())
}
}

Expand Down Expand Up @@ -246,11 +242,6 @@ impl Hugr {
}
}

/// Add a node to the graph, with the default conversion from OpType to NodeType
pub(crate) fn add_op(&mut self, op: impl Into<OpType>) -> Node {
self.add_node(NodeType::new_auto(op))
}

/// Add a node to the graph.
pub(crate) fn add_node(&mut self, nodetype: NodeType) -> Node {
let node = self
Expand Down Expand Up @@ -400,7 +391,7 @@ mod test {
FunctionType::new(type_row![BIT], type_row![BIT]).with_extension_delta(&r),
);
let [input, output] = hugr.get_io(hugr.root()).unwrap();
let lift = hugr.add_op_with_parent(
let lift = hugr.add_node_with_parent(
hugr.root(),
ops::LeafOp::Lift {
type_row: type_row![BIT],
Expand Down
Loading

0 comments on commit a6dee37

Please sign in to comment.