Skip to content

Commit a6dee37

Browse files
authored
refactor: remove add_op_<posn> by generalizing add_node_<posn> with "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_....
1 parent a13b5ef commit a6dee37

19 files changed

+190
-214
lines changed

src/builder.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -149,14 +149,14 @@ pub(crate) mod test {
149149
let mut hugr = Hugr::new(NodeType::new_pure(ops::DFG {
150150
signature: signature.clone(),
151151
}));
152-
hugr.add_op_with_parent(
152+
hugr.add_node_with_parent(
153153
hugr.root(),
154154
ops::Input {
155155
types: signature.input,
156156
},
157157
)
158158
.unwrap();
159-
hugr.add_op_with_parent(
159+
hugr.add_node_with_parent(
160160
hugr.root(),
161161
ops::Output {
162162
types: signature.output,

src/builder/build_traits.rs

+5-12
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ pub trait Container {
4747
/// Add an [`OpType`] as the final child of the container.
4848
fn add_child_op(&mut self, op: impl Into<OpType>) -> Result<Node, BuildError> {
4949
let parent = self.container_node();
50-
Ok(self.hugr_mut().add_op_with_parent(parent, op)?)
50+
Ok(self.hugr_mut().add_node_with_parent(parent, op)?)
5151
}
5252
/// Add a [`NodeType`] as the final child of the container.
5353
fn add_child_node(&mut self, node: NodeType) -> Result<Node, BuildError> {
@@ -416,7 +416,7 @@ pub trait Dataflow: Container {
416416
rest: rest_types.into(),
417417
};
418418
// TODO: Make input extensions a parameter
419-
let (loop_node, _) = add_op_with_wires(self, tail_loop.clone(), input_wires)?;
419+
let (loop_node, _) = add_node_with_wires(self, tail_loop.clone(), input_wires)?;
420420

421421
TailLoopBuilder::create_with_io(self.hugr_mut(), loop_node, &tail_loop)
422422
}
@@ -623,21 +623,14 @@ pub trait Dataflow: Container {
623623
}
624624
}
625625

626-
fn add_op_with_wires<T: Dataflow + ?Sized>(
627-
data_builder: &mut T,
628-
optype: impl Into<OpType>,
629-
inputs: Vec<Wire>,
630-
) -> Result<(Node, usize), BuildError> {
631-
add_node_with_wires(data_builder, NodeType::new_auto(optype), inputs)
632-
}
633-
634626
fn add_node_with_wires<T: Dataflow + ?Sized>(
635627
data_builder: &mut T,
636-
nodetype: NodeType,
628+
nodetype: impl Into<NodeType>,
637629
inputs: Vec<Wire>,
638630
) -> Result<(Node, usize), BuildError> {
639-
let op_node = data_builder.add_child_node(nodetype.clone())?;
631+
let nodetype = nodetype.into();
640632
let sig = nodetype.op_signature();
633+
let op_node = data_builder.add_child_node(nodetype)?;
641634

642635
wire_up_inputs(inputs, op_node, data_builder)?;
643636

src/builder/cfg.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ impl<B: AsMut<Hugr> + AsRef<Hugr>> CFGBuilder<B> {
9292
let exit_node = base
9393
.as_mut()
9494
// Make the extensions a parameter
95-
.add_op_with_parent(cfg_node, exit_block_type)?;
95+
.add_node_with_parent(cfg_node, exit_block_type)?;
9696
Ok(Self {
9797
base,
9898
cfg_node,
@@ -144,10 +144,10 @@ impl<B: AsMut<Hugr> + AsRef<Hugr>> CFGBuilder<B> {
144144
let block_n = if entry {
145145
let exit = self.exit_node;
146146
// TODO: Make extensions a parameter
147-
self.hugr_mut().add_op_before(exit, op)
147+
self.hugr_mut().add_node_before(exit, op)
148148
} else {
149149
// TODO: Make extensions a parameter
150-
self.hugr_mut().add_op_with_parent(parent, op)
150+
self.hugr_mut().add_node_with_parent(parent, op)
151151
}?;
152152

153153
BlockBuilder::create(

src/builder/conditional.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ impl<B: AsMut<Hugr> + AsRef<Hugr>> ConditionalBuilder<B> {
126126
let case_node =
127127
// add case before any existing subsequent cases
128128
if let Some(&sibling_node) = self.case_nodes[case + 1..].iter().flatten().next() {
129-
self.hugr_mut().add_op_before(sibling_node, case_op)?
129+
self.hugr_mut().add_node_before(sibling_node, case_op)?
130130
} else {
131131
self.add_child_op(case_op)?
132132
};

src/extension/infer.rs

+26-26
Original file line numberDiff line numberDiff line change
@@ -698,8 +698,8 @@ mod test {
698698
let input = ops::Input::new(type_row![NAT, NAT]);
699699
let output = ops::Output::new(type_row![NAT]);
700700

701-
let input = hugr.add_op_with_parent(hugr.root(), input)?;
702-
let output = hugr.add_op_with_parent(hugr.root(), output)?;
701+
let input = hugr.add_node_with_parent(hugr.root(), input)?;
702+
let output = hugr.add_node_with_parent(hugr.root(), output)?;
703703

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

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

718-
let add_a = hugr.add_op_with_parent(
718+
let add_a = hugr.add_node_with_parent(
719719
hugr.root(),
720720
ops::DFG {
721721
signature: add_a_sig,
722722
},
723723
)?;
724-
let add_b = hugr.add_op_with_parent(
724+
let add_b = hugr.add_node_with_parent(
725725
hugr.root(),
726726
ops::DFG {
727727
signature: add_b_sig,
728728
},
729729
)?;
730-
let add_ab = hugr.add_op_with_parent(
730+
let add_ab = hugr.add_node_with_parent(
731731
hugr.root(),
732732
ops::DFG {
733733
signature: add_ab_sig,
734734
},
735735
)?;
736-
let mult_c = hugr.add_op_with_parent(
736+
let mult_c = hugr.add_node_with_parent(
737737
hugr.root(),
738738
ops::DFG {
739739
signature: mult_c_sig,
@@ -877,7 +877,7 @@ mod test {
877877
let [input, output] = hugr.get_io(hugr.root()).unwrap();
878878
let add_r_sig = FunctionType::new(type_row![NAT], type_row![NAT]).with_extension_delta(&rs);
879879

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

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

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

959-
let node = hugr.add_op_with_parent(parent, op)?;
960-
let input = hugr.add_op_with_parent(
959+
let node = hugr.add_node_with_parent(parent, op)?;
960+
let input = hugr.add_node_with_parent(
961961
node,
962962
ops::Input {
963963
types: op_sig.input,
964964
},
965965
)?;
966-
let output = hugr.add_op_with_parent(
966+
let output = hugr.add_node_with_parent(
967967
node,
968968
ops::Output {
969969
types: op_sig.output,
@@ -988,15 +988,15 @@ mod test {
988988
Into::<OpType>::into(op).signature(),
989989
)?;
990990

991-
let lift1 = hugr.add_op_with_parent(
991+
let lift1 = hugr.add_node_with_parent(
992992
case,
993993
ops::LeafOp::Lift {
994994
type_row: type_row![NAT],
995995
new_extension: first_ext,
996996
},
997997
)?;
998998

999-
let lift2 = hugr.add_op_with_parent(
999+
let lift2 = hugr.add_node_with_parent(
10001000
case,
10011001
ops::LeafOp::Lift {
10021002
type_row: type_row![NAT],
@@ -1066,13 +1066,13 @@ mod test {
10661066
}));
10671067

10681068
let root = hugr.root();
1069-
let input = hugr.add_op_with_parent(
1069+
let input = hugr.add_node_with_parent(
10701070
root,
10711071
ops::Input {
10721072
types: type_row![NAT],
10731073
},
10741074
)?;
1075-
let output = hugr.add_op_with_parent(
1075+
let output = hugr.add_node_with_parent(
10761076
root,
10771077
ops::Output {
10781078
types: type_row![NAT],
@@ -1097,7 +1097,7 @@ mod test {
10971097
.unwrap();
10981098

10991099
let lift = hugr
1100-
.add_op_with_parent(
1100+
.add_node_with_parent(
11011101
node,
11021102
ops::LeafOp::Lift {
11031103
type_row: type_row![NAT],
@@ -1149,7 +1149,7 @@ mod test {
11491149

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

1152-
let dfg = hugr.add_op_with_parent(bb, op)?;
1152+
let dfg = hugr.add_node_with_parent(bb, op)?;
11531153

11541154
hugr.connect(bb_in, 0, dfg, 0)?;
11551155
hugr.connect(dfg, 0, bb_out, 0)?;
@@ -1181,16 +1181,16 @@ mod test {
11811181
extension_delta: entry_extensions,
11821182
};
11831183

1184-
let exit = hugr.add_op_with_parent(
1184+
let exit = hugr.add_node_with_parent(
11851185
root,
11861186
ops::BasicBlock::Exit {
11871187
cfg_outputs: exit_types.into(),
11881188
},
11891189
)?;
11901190

1191-
let entry = hugr.add_op_before(exit, dfb)?;
1192-
let entry_in = hugr.add_op_with_parent(entry, ops::Input { types: inputs })?;
1193-
let entry_out = hugr.add_op_with_parent(
1191+
let entry = hugr.add_node_before(exit, dfb)?;
1192+
let entry_in = hugr.add_node_with_parent(entry, ops::Input { types: inputs })?;
1193+
let entry_out = hugr.add_node_with_parent(
11941194
entry,
11951195
ops::Output {
11961196
types: vec![entry_tuple_sum].into(),
@@ -1245,7 +1245,7 @@ mod test {
12451245
type_row![NAT],
12461246
)?;
12471247

1248-
let mkpred = hugr.add_op_with_parent(
1248+
let mkpred = hugr.add_node_with_parent(
12491249
entry,
12501250
make_opaque(
12511251
A,
@@ -1341,7 +1341,7 @@ mod test {
13411341
type_row![NAT],
13421342
)?;
13431343

1344-
let entry_mid = hugr.add_op_with_parent(
1344+
let entry_mid = hugr.add_node_with_parent(
13451345
entry,
13461346
make_opaque(UNKNOWN_EXTENSION, FunctionType::new(vec![NAT], twoway(NAT))),
13471347
)?;
@@ -1427,7 +1427,7 @@ mod test {
14271427
type_row![NAT],
14281428
)?;
14291429

1430-
let entry_dfg = hugr.add_op_with_parent(
1430+
let entry_dfg = hugr.add_node_with_parent(
14311431
entry,
14321432
make_opaque(
14331433
UNKNOWN_EXTENSION,
@@ -1508,7 +1508,7 @@ mod test {
15081508
type_row![NAT],
15091509
)?;
15101510

1511-
let entry_mid = hugr.add_op_with_parent(
1511+
let entry_mid = hugr.add_node_with_parent(
15121512
entry,
15131513
make_opaque(UNKNOWN_EXTENSION, FunctionType::new(vec![NAT], oneway(NAT))),
15141514
)?;

src/extension/op_def.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ impl Extension {
270270
}
271271

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

292292
/// Create an OpDef with custom binary code to compute the signature, and no "misc" or "lowering
293293
/// functions" defined.
294-
pub fn add_op_custom_sig_simple(
294+
pub fn add_node_custom_sig_simple(
295295
&mut self,
296296
name: SmolStr,
297297
description: String,
298298
params: Vec<TypeParam>,
299299
signature_func: impl CustomSignatureFunc + 'static,
300300
) -> Result<&OpDef, ExtensionBuildError> {
301-
self.add_op_custom_sig(
301+
self.add_node_custom_sig(
302302
name,
303303
description,
304304
params,
@@ -310,7 +310,7 @@ impl Extension {
310310

311311
/// Create an OpDef with a signature (inputs+outputs) read from the
312312
/// declarative YAML
313-
pub fn add_op_decl_sig(
313+
pub fn add_node_decl_sig(
314314
&mut self,
315315
name: SmolStr,
316316
description: String,

src/extension/prelude.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ lazy_static! {
4141
.unwrap();
4242

4343
prelude
44-
.add_op_custom_sig_simple(
44+
.add_node_custom_sig_simple(
4545
SmolStr::new_inline(NEW_ARRAY_OP_ID),
4646
"Create a new array from elements".to_string(),
4747
vec![TypeParam::Type(TypeBound::Any), TypeParam::max_nat()],

src/hugr.rs

+4-13
Original file line numberDiff line numberDiff line change
@@ -150,13 +150,9 @@ impl NodeType {
150150
}
151151
}
152152

153-
impl OpType {
154-
/// Convert an OpType to a NodeType by giving it some input extensions
155-
pub fn with_extensions(self, rs: ExtensionSet) -> NodeType {
156-
NodeType {
157-
op: self,
158-
input_extensions: Some(rs),
159-
}
153+
impl<T: Into<OpType>> From<T> for NodeType {
154+
fn from(value: T) -> Self {
155+
NodeType::new_auto(value.into())
160156
}
161157
}
162158

@@ -246,11 +242,6 @@ impl Hugr {
246242
}
247243
}
248244

249-
/// Add a node to the graph, with the default conversion from OpType to NodeType
250-
pub(crate) fn add_op(&mut self, op: impl Into<OpType>) -> Node {
251-
self.add_node(NodeType::new_auto(op))
252-
}
253-
254245
/// Add a node to the graph.
255246
pub(crate) fn add_node(&mut self, nodetype: NodeType) -> Node {
256247
let node = self
@@ -400,7 +391,7 @@ mod test {
400391
FunctionType::new(type_row![BIT], type_row![BIT]).with_extension_delta(&r),
401392
);
402393
let [input, output] = hugr.get_io(hugr.root()).unwrap();
403-
let lift = hugr.add_op_with_parent(
394+
let lift = hugr.add_node_with_parent(
404395
hugr.root(),
405396
ops::LeafOp::Lift {
406397
type_row: type_row![BIT],

0 commit comments

Comments
 (0)