Skip to content

Commit

Permalink
feat!: Move Lift, MakeTuple, UnpackTuple and Lift to prelude (#…
Browse files Browse the repository at this point in the history
…1475)

Much of the noise is adding PRELUDE to extension inference tests. Add a
convenience `with_prelude()` method to Signature.

`Tag` left as a core operation as it is paramaterized by a variable
number of type rows.

Also drive-by get rid of custom signature use for print.

Closes #816
Closes #1373

BREAKING CHANGE: Move `Lift`, `MakeTuple`, `UnpackTuple` and `Lift` from
core operations to prelude. Rename `ops::leaf` module to `ops::sum`.
  • Loading branch information
ss2165 committed Aug 28, 2024
1 parent 49a5bad commit b387505
Show file tree
Hide file tree
Showing 35 changed files with 1,168 additions and 1,194 deletions.
5 changes: 3 additions & 2 deletions hugr-core/src/builder/build_traits.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::extension::prelude::MakeTuple;
use crate::hugr::hugrmut::InsertionResult;
use crate::hugr::views::HugrView;
use crate::hugr::{NodeMetadata, ValidationError};
use crate::ops::{self, MakeTuple, OpTag, OpTrait, OpType, Tag};
use crate::ops::{self, OpTag, OpTrait, OpType, Tag};
use crate::utils::collect_array;
use crate::{IncomingPort, Node, OutgoingPort};

Expand Down Expand Up @@ -579,7 +580,7 @@ pub trait Dataflow: Container {
.map(|&wire| self.get_wire_type(wire))
.collect();
let types = types?.into();
let make_op = self.add_dataflow_op(MakeTuple { tys: types }, values)?;
let make_op = self.add_dataflow_op(MakeTuple(types), values)?;
Ok(make_op.out_wire(0))
}

Expand Down
49 changes: 18 additions & 31 deletions hugr-core/src/builder/dataflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,11 @@ pub(crate) mod test {
use crate::builder::{
endo_sig, inout_sig, BuilderWiringError, DataflowSubContainer, ModuleBuilder,
};
use crate::extension::prelude::{Lift, Noop};
use crate::extension::prelude::{BOOL_T, USIZE_T};
use crate::extension::{ExtensionId, SignatureError, EMPTY_REG, PRELUDE_REGISTRY};
use crate::hugr::validate::InterGraphEdgeError;
use crate::ops::{handle::NodeHandle, Lift, Noop, OpTag};
use crate::ops::{handle::NodeHandle, OpTag};
use crate::ops::{OpTrait, Value};

use crate::std_extensions::logic::test::and_op;
Expand Down Expand Up @@ -319,21 +320,25 @@ pub(crate) mod test {
#[test]
fn simple_inter_graph_edge() {
let builder = || -> Result<Hugr, BuildError> {
let mut f_build =
FunctionBuilder::new("main", Signature::new(type_row![BIT], type_row![BIT]))?;
let mut f_build = FunctionBuilder::new(
"main",
Signature::new(type_row![BIT], type_row![BIT]).with_prelude(),
)?;

let [i1] = f_build.input_wires_arr();
let noop = f_build.add_dataflow_op(Noop { ty: BIT }, [i1])?;
let noop = f_build.add_dataflow_op(Noop(BIT), [i1])?;
let i1 = noop.out_wire(0);

let mut nested =
f_build.dfg_builder(Signature::new(type_row![], type_row![BIT]), [])?;
let mut nested = f_build.dfg_builder(
Signature::new(type_row![], type_row![BIT]).with_prelude(),
[],
)?;

let id = nested.add_dataflow_op(Noop { ty: BIT }, [i1])?;
let id = nested.add_dataflow_op(Noop(BIT), [i1])?;

let nested = nested.finish_with_outputs([id.out_wire(0)])?;

f_build.finish_hugr_with_outputs([nested.out_wire(0)], &EMPTY_REG)
f_build.finish_prelude_hugr_with_outputs([nested.out_wire(0)])
};

assert_matches!(builder(), Ok(_));
Expand All @@ -345,12 +350,12 @@ pub(crate) mod test {
FunctionBuilder::new("main", Signature::new(type_row![QB], type_row![QB]))?;

let [i1] = f_build.input_wires_arr();
let noop = f_build.add_dataflow_op(Noop { ty: QB }, [i1])?;
let noop = f_build.add_dataflow_op(Noop(QB), [i1])?;
let i1 = noop.out_wire(0);

let mut nested = f_build.dfg_builder(Signature::new(type_row![], type_row![QB]), [])?;

let id_res = nested.add_dataflow_op(Noop { ty: QB }, [i1]);
let id_res = nested.add_dataflow_op(Noop(QB), [i1]);

// The error would anyway be caught in validation when we finish the Hugr,
// but the builder catches it earlier
Expand Down Expand Up @@ -417,22 +422,10 @@ pub(crate) mod test {
let mut add_ab = parent.dfg_builder(endo_sig(BIT), [w])?;
let [w] = add_ab.input_wires_arr();

let lift_a = add_ab.add_dataflow_op(
Lift {
type_row: type_row![BIT],
new_extension: xa.clone(),
},
[w],
)?;
let lift_a = add_ab.add_dataflow_op(Lift::new(type_row![BIT], xa.clone()), [w])?;
let [w] = lift_a.outputs_arr();

let lift_b = add_ab.add_dataflow_op(
Lift {
type_row: type_row![BIT],
new_extension: xb,
},
[w],
)?;
let lift_b = add_ab.add_dataflow_op(Lift::new(type_row![BIT], xb), [w])?;
let [w] = lift_b.outputs_arr();

let add_ab = add_ab.finish_with_outputs([w])?;
Expand All @@ -442,13 +435,7 @@ pub(crate) mod test {
// via a child lift node
let mut add_c = parent.dfg_builder(endo_sig(BIT), [w])?;
let [w] = add_c.input_wires_arr();
let lift_c = add_c.add_dataflow_op(
Lift {
type_row: type_row![BIT],
new_extension: xc,
},
[w],
)?;
let lift_c = add_c.add_dataflow_op(Lift::new(type_row![BIT], xc), [w])?;
let wires: Vec<Wire> = lift_c.outputs().collect();

let add_c = add_c.finish_with_outputs(wires)?;
Expand Down
17 changes: 4 additions & 13 deletions hugr-core/src/builder/tail_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ mod test {
test::{BIT, NAT},
DataflowSubContainer, HugrBuilder, ModuleBuilder, SubContainer,
},
extension::prelude::{ConstUsize, PRELUDE_ID, USIZE_T},
extension::prelude::{ConstUsize, Lift, PRELUDE_ID, USIZE_T},
hugr::ValidationError,
ops::Value,
type_row,
Expand Down Expand Up @@ -142,17 +142,11 @@ mod test {
let mut module_builder = ModuleBuilder::new();
let mut fbuild = module_builder.define_function(
"main",
Signature::new(type_row![BIT], type_row![NAT]).with_extension_delta(PRELUDE_ID),
Signature::new(type_row![BIT], type_row![NAT]).with_prelude(),
)?;
let _fdef = {
let [b1] = fbuild
.add_dataflow_op(
ops::Lift {
type_row: type_row![BIT],
new_extension: PRELUDE_ID,
},
fbuild.input_wires(),
)?
.add_dataflow_op(Lift::new(type_row![BIT], PRELUDE_ID), fbuild.input_wires())?
.outputs_arr();
let loop_id = {
let mut loop_b =
Expand All @@ -161,10 +155,7 @@ mod test {
let const_val = Value::true_val();
let const_wire = loop_b.add_load_const(Value::true_val());
let lift_node = loop_b.add_dataflow_op(
ops::Lift {
type_row: vec![const_val.get_type().clone()].into(),
new_extension: PRELUDE_ID,
},
Lift::new(vec![const_val.get_type().clone()].into(), PRELUDE_ID),
[const_wire],
)?;
let [const_wire] = lift_node.outputs_arr();
Expand Down
2 changes: 1 addition & 1 deletion hugr-core/src/extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub use type_def::{TypeDef, TypeDefBound};
mod const_fold;
pub mod prelude;
pub mod simple_op;
pub use const_fold::{ConstFold, ConstFoldResult, Folder};
pub use const_fold::{fold_out_row, ConstFold, ConstFoldResult, Folder};
pub use prelude::{PRELUDE, PRELUDE_REGISTRY};

#[cfg(feature = "declarative")]
Expand Down
10 changes: 10 additions & 0 deletions hugr-core/src/extension/const_fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ use crate::ops;
/// successful and no values are output.
pub type ConstFoldResult = Option<Vec<(OutgoingPort, ops::Value)>>;

/// Tag some output constants with [`OutgoingPort`] inferred from the ordering.
pub fn fold_out_row(consts: impl IntoIterator<Item = Value>) -> ConstFoldResult {
let vec = consts
.into_iter()
.enumerate()
.map(|(i, c)| (i.into(), c))
.collect();
Some(vec)
}

/// Trait implemented by extension operations that can perform constant folding.
pub trait ConstFold: Send + Sync {
/// Given type arguments `type_args` and
Expand Down
1 change: 0 additions & 1 deletion hugr-core/src/extension/op_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,6 @@ impl SignatureFunc {
// TODO raise warning: https://github.com/CQCL/hugr/issues/1432
SignatureFunc::MissingValidateFunc(ts) => (ts, args),
};

let mut res = pf.instantiate(args, exts)?;
res.extension_reqs.insert(&def.extension);

Expand Down
Loading

0 comments on commit b387505

Please sign in to comment.