Skip to content

Commit ea5213d

Browse files
authored
refactor!: Remove NodeType and input_extensions (#1183)
Following #1142 the `input_extensions` are unused, so remove the storage for them BREAKING CHANGE: * `add_child_op`(`_with_parent`), etc., gone; use `add_child_node`(`_with_parent`) with an (impl Into-)OpType. * `get_nodetype` gone - use `get_optype`. * `NodeType` gone - use `OpType` directly. * Various (Into<)Option<ExtensionSet> params removed from builder methods especially {cfg_,dfg_}builder. * `input_extensions` removed from serialization schema.
1 parent 65718f7 commit ea5213d

31 files changed

+196
-1932
lines changed

hugr-core/src/builder.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ pub enum BuilderWiringError {
220220
pub(crate) mod test {
221221
use rstest::fixture;
222222

223-
use crate::hugr::{views::HugrView, HugrMut, NodeType};
223+
use crate::hugr::{views::HugrView, HugrMut};
224224
use crate::ops;
225225
use crate::std_extensions::arithmetic::float_ops::FLOAT_OPS_REGISTRY;
226226
use crate::types::{FunctionType, PolyFuncType, Type};
@@ -278,9 +278,9 @@ pub(crate) mod test {
278278
/// inference. Using DFGBuilder will default to a root node with an open
279279
/// extension variable
280280
pub(crate) fn closed_dfg_root_hugr(signature: FunctionType) -> Hugr {
281-
let mut hugr = Hugr::new(NodeType::new_pure(ops::DFG {
281+
let mut hugr = Hugr::new(ops::DFG {
282282
signature: signature.clone(),
283-
}));
283+
});
284284
hugr.add_node_with_parent(
285285
hugr.root(),
286286
ops::Input {

hugr-core/src/builder/build_traits.rs

+22-54
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use super::{
1414
use super::{BuilderWiringError, FunctionBuilder};
1515

1616
use crate::{
17-
hugr::NodeType,
1817
ops::handle::{ConstID, DataflowOpID, FuncID, NodeHandle},
1918
types::EdgeKind,
2019
};
@@ -45,12 +44,7 @@ pub trait Container {
4544
/// Immutable reference to HUGR being built
4645
fn hugr(&self) -> &Hugr;
4746
/// Add an [`OpType`] as the final child of the container.
48-
fn add_child_op(&mut self, op: impl Into<OpType>) -> Node {
49-
let parent = self.container_node();
50-
self.hugr_mut().add_node_with_parent(parent, op)
51-
}
52-
/// Add a [`NodeType`] as the final child of the container.
53-
fn add_child_node(&mut self, node: NodeType) -> Node {
47+
fn add_child_node(&mut self, node: impl Into<OpType>) -> Node {
5448
let parent = self.container_node();
5549
self.hugr_mut().add_node_with_parent(parent, node)
5650
}
@@ -71,8 +65,7 @@ pub trait Container {
7165
/// This function will return an error if there is an error in adding the
7266
/// [`OpType::Const`] node.
7367
fn add_constant(&mut self, constant: impl Into<ops::Const>) -> ConstID {
74-
self.add_child_node(NodeType::new_pure(constant.into()))
75-
.into()
68+
self.add_child_node(constant.into()).into()
7669
}
7770

7871
/// Add a [`ops::FuncDefn`] node and returns a builder to define the function
@@ -88,13 +81,12 @@ pub trait Container {
8881
signature: PolyFuncType,
8982
) -> Result<FunctionBuilder<&mut Hugr>, BuildError> {
9083
let body = signature.body().clone();
91-
let f_node = self.add_child_node(NodeType::new_pure(ops::FuncDefn {
84+
let f_node = self.add_child_node(ops::FuncDefn {
9285
name: name.into(),
9386
signature,
94-
}));
87+
});
9588

96-
let db =
97-
DFGBuilder::create_with_io(self.hugr_mut(), f_node, body, Some(ExtensionSet::new()))?;
89+
let db = DFGBuilder::create_with_io(self.hugr_mut(), f_node, body)?;
9890
Ok(FunctionBuilder::from_dfg_builder(db))
9991
}
10092

@@ -182,29 +174,15 @@ pub trait Dataflow: Container {
182174
fn input_wires(&self) -> Outputs {
183175
self.input().outputs()
184176
}
185-
/// Add a dataflow op to the sibling graph, wiring up the `input_wires` to the
177+
/// Add a dataflow [`OpType`] to the sibling graph, wiring up the `input_wires` to the
186178
/// incoming ports of the resulting node.
187179
///
188180
/// # Errors
189181
///
190182
/// Returns a [`BuildError::OperationWiring`] error if the `input_wires` cannot be connected.
191183
fn add_dataflow_op(
192184
&mut self,
193-
op: impl Into<OpType>,
194-
input_wires: impl IntoIterator<Item = Wire>,
195-
) -> Result<BuildHandle<DataflowOpID>, BuildError> {
196-
self.add_dataflow_node(NodeType::new_auto(op), input_wires)
197-
}
198-
199-
/// Add a dataflow [`NodeType`] to the sibling graph, wiring up the `input_wires` to the
200-
/// incoming ports of the resulting node.
201-
///
202-
/// # Errors
203-
///
204-
/// Returns a [`BuildError::OperationWiring`] error if the `input_wires` cannot be connected.
205-
fn add_dataflow_node(
206-
&mut self,
207-
nodetype: NodeType,
185+
nodetype: impl Into<OpType>,
208186
input_wires: impl IntoIterator<Item = Wire>,
209187
) -> Result<BuildHandle<DataflowOpID>, BuildError> {
210188
let outs = add_node_with_wires(self, nodetype, input_wires)?;
@@ -297,16 +275,14 @@ pub trait Dataflow: Container {
297275
fn dfg_builder(
298276
&mut self,
299277
signature: FunctionType,
300-
input_extensions: Option<ExtensionSet>,
301278
input_wires: impl IntoIterator<Item = Wire>,
302279
) -> Result<DFGBuilder<&mut Hugr>, BuildError> {
303280
let op = ops::DFG {
304281
signature: signature.clone(),
305282
};
306-
let nodetype = NodeType::new(op, input_extensions.clone());
307-
let (dfg_n, _) = add_node_with_wires(self, nodetype, input_wires)?;
283+
let (dfg_n, _) = add_node_with_wires(self, op, input_wires)?;
308284

309-
DFGBuilder::create_with_io(self.hugr_mut(), dfg_n, signature, input_extensions)
285+
DFGBuilder::create_with_io(self.hugr_mut(), dfg_n, signature)
310286
}
311287

312288
/// Return a builder for a [`crate::ops::CFG`] node,
@@ -322,7 +298,6 @@ pub trait Dataflow: Container {
322298
fn cfg_builder(
323299
&mut self,
324300
inputs: impl IntoIterator<Item = (Type, Wire)>,
325-
input_extensions: impl Into<Option<ExtensionSet>>,
326301
output_types: TypeRow,
327302
extension_delta: ExtensionSet,
328303
) -> Result<CFGBuilder<&mut Hugr>, BuildError> {
@@ -332,13 +307,10 @@ pub trait Dataflow: Container {
332307

333308
let (cfg_node, _) = add_node_with_wires(
334309
self,
335-
NodeType::new(
336-
ops::CFG {
337-
signature: FunctionType::new(inputs.clone(), output_types.clone())
338-
.with_extension_delta(extension_delta),
339-
},
340-
input_extensions.into(),
341-
),
310+
ops::CFG {
311+
signature: FunctionType::new(inputs.clone(), output_types.clone())
312+
.with_extension_delta(extension_delta),
313+
},
342314
input_wires,
343315
)?;
344316
CFGBuilder::create(self.hugr_mut(), cfg_node, inputs, output_types)
@@ -348,9 +320,8 @@ pub trait Dataflow: Container {
348320
/// Adds a [`OpType::LoadConstant`] node.
349321
fn load_const(&mut self, cid: &ConstID) -> Wire {
350322
let const_node = cid.node();
351-
let nodetype = self.hugr().get_nodetype(const_node);
323+
let nodetype = self.hugr().get_optype(const_node);
352324
let op: ops::Const = nodetype
353-
.op()
354325
.clone()
355326
.try_into()
356327
.expect("ConstID does not refer to Const op.");
@@ -394,7 +365,7 @@ pub trait Dataflow: Container {
394365
exts: &ExtensionRegistry,
395366
) -> Result<Wire, BuildError> {
396367
let func_node = fid.node();
397-
let func_op = self.hugr().get_nodetype(func_node).op();
368+
let func_op = self.hugr().get_optype(func_node);
398369
let func_sig = match func_op {
399370
OpType::FuncDefn(ops::FuncDefn { signature, .. })
400371
| OpType::FuncDecl(ops::FuncDecl { signature, .. }) => signature.clone(),
@@ -643,26 +614,23 @@ pub trait Dataflow: Container {
643614
/// invalid edge.
644615
fn add_node_with_wires<T: Dataflow + ?Sized>(
645616
data_builder: &mut T,
646-
nodetype: impl Into<NodeType>,
617+
nodetype: impl Into<OpType>,
647618
inputs: impl IntoIterator<Item = Wire>,
648619
) -> Result<(Node, usize), BuildError> {
649-
let nodetype: NodeType = nodetype.into();
620+
let op = nodetype.into();
650621
// Check there are no row variables, as that would prevent us
651622
// from indexing into the node's ports in order to wire up
652-
nodetype
653-
.op_signature()
623+
op.dataflow_signature()
654624
.as_ref()
655625
.and_then(FunctionType::find_rowvar)
656626
.map_or(Ok(()), |(idx, _)| {
657627
Err(SignatureError::RowVarWhereTypeExpected { idx })
658628
})?;
659-
let num_outputs = nodetype.op().value_output_count();
660-
let op_node = data_builder.add_child_node(nodetype.clone());
629+
let num_outputs = op.value_output_count();
630+
let op_node = data_builder.add_child_node(op.clone());
661631

662-
wire_up_inputs(inputs, op_node, data_builder).map_err(|error| BuildError::OperationWiring {
663-
op: nodetype.into_op(),
664-
error,
665-
})?;
632+
wire_up_inputs(inputs, op_node, data_builder)
633+
.map_err(|error| BuildError::OperationWiring { op, error })?;
666634

667635
Ok((op_node, num_outputs))
668636
}

hugr-core/src/builder/cfg.rs

+4-14
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@ use crate::{
1313
use crate::{hugr::views::HugrView, types::TypeRow};
1414

1515
use crate::Node;
16-
use crate::{
17-
hugr::{HugrMut, NodeType},
18-
type_row, Hugr,
19-
};
16+
use crate::{hugr::HugrMut, type_row, Hugr};
2017

2118
/// Builder for a [`crate::ops::CFG`] child control
2219
/// flow graph.
@@ -158,7 +155,7 @@ impl CFGBuilder<Hugr> {
158155
signature: signature.clone(),
159156
};
160157

161-
let base = Hugr::new(NodeType::new_open(cfg_op));
158+
let base = Hugr::new(cfg_op);
162159
let cfg_node = base.root();
163160
CFGBuilder::create(base, cfg_node, signature.input, signature.output)
164161
}
@@ -336,12 +333,7 @@ impl<B: AsMut<Hugr> + AsRef<Hugr>> BlockBuilder<B> {
336333
fn create(base: B, block_n: Node) -> Result<Self, BuildError> {
337334
let block_op = base.get_optype(block_n).as_dataflow_block().unwrap();
338335
let signature = block_op.inner_signature();
339-
let inp_ex = base
340-
.as_ref()
341-
.get_nodetype(block_n)
342-
.input_extensions()
343-
.cloned();
344-
let db = DFGBuilder::create_with_io(base, block_n, signature, inp_ex)?;
336+
let db = DFGBuilder::create_with_io(base, block_n, signature)?;
345337
Ok(BlockBuilder::from_dfg_builder(db))
346338
}
347339

@@ -363,7 +355,6 @@ impl BlockBuilder<Hugr> {
363355
/// Initialize a [`DataflowBlock`] rooted HUGR builder
364356
pub fn new(
365357
inputs: impl Into<TypeRow>,
366-
input_extensions: impl Into<Option<ExtensionSet>>,
367358
sum_rows: impl IntoIterator<Item = TypeRow>,
368359
other_outputs: impl Into<TypeRow>,
369360
extension_delta: ExtensionSet,
@@ -378,7 +369,7 @@ impl BlockBuilder<Hugr> {
378369
extension_delta,
379370
};
380371

381-
let base = Hugr::new(NodeType::new(op, input_extensions));
372+
let base = Hugr::new(op);
382373
let root = base.root();
383374
Self::create(base, root)
384375
}
@@ -418,7 +409,6 @@ pub(crate) mod test {
418409
let cfg_id = {
419410
let mut cfg_builder = func_builder.cfg_builder(
420411
vec![(NAT, int)],
421-
None,
422412
type_row![NAT],
423413
ExtensionSet::new(),
424414
)?;

hugr-core/src/builder/conditional.rs

+5-11
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,7 @@ use super::{
1616
};
1717

1818
use crate::Node;
19-
use crate::{
20-
extension::ExtensionSet,
21-
hugr::{HugrMut, NodeType},
22-
Hugr,
23-
};
19+
use crate::{extension::ExtensionSet, hugr::HugrMut, Hugr};
2420

2521
use std::collections::HashSet;
2622

@@ -130,7 +126,7 @@ impl<B: AsMut<Hugr> + AsRef<Hugr>> ConditionalBuilder<B> {
130126
if let Some(&sibling_node) = self.case_nodes[case + 1..].iter().flatten().next() {
131127
self.hugr_mut().add_node_before(sibling_node, case_op)
132128
} else {
133-
self.add_child_op(case_op)
129+
self.add_child_node(case_op)
134130
};
135131

136132
self.case_nodes[case] = Some(case_node);
@@ -139,7 +135,6 @@ impl<B: AsMut<Hugr> + AsRef<Hugr>> ConditionalBuilder<B> {
139135
self.hugr_mut(),
140136
case_node,
141137
FunctionType::new(inputs, outputs).with_extension_delta(extension_delta),
142-
None,
143138
)?;
144139

145140
Ok(CaseBuilder::from_dfg_builder(dfg_builder))
@@ -177,8 +172,7 @@ impl ConditionalBuilder<Hugr> {
177172
outputs,
178173
extension_delta,
179174
};
180-
// TODO: Allow input extensions to be specified
181-
let base = Hugr::new(NodeType::new_open(op));
175+
let base = Hugr::new(op);
182176
let conditional_node = base.root();
183177

184178
Ok(ConditionalBuilder {
@@ -196,9 +190,9 @@ impl CaseBuilder<Hugr> {
196190
let op = ops::Case {
197191
signature: signature.clone(),
198192
};
199-
let base = Hugr::new(NodeType::new_open(op));
193+
let base = Hugr::new(op);
200194
let root = base.root();
201-
let dfg_builder = DFGBuilder::create_with_io(base, root, signature, None)?;
195+
let dfg_builder = DFGBuilder::create_with_io(base, root, signature)?;
202196

203197
Ok(CaseBuilder::from_dfg_builder(dfg_builder))
204198
}

0 commit comments

Comments
 (0)