Skip to content

Commit

Permalink
FunctionType -> Signature
Browse files Browse the repository at this point in the history
  • Loading branch information
acl-cqc committed Jul 16, 2024
1 parent 4a40b9b commit 53e1c4d
Show file tree
Hide file tree
Showing 53 changed files with 389 additions and 423 deletions.
6 changes: 3 additions & 3 deletions hugr-cli/tests/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use hugr_core::{
builder::{Container, Dataflow, DataflowHugr},
extension::prelude::{BOOL_T, QB_T},
type_row,
types::FunctionType,
types::Signature,
Hugr,
};
use predicates::{prelude::*, str::contains};
Expand All @@ -25,7 +25,7 @@ fn cmd() -> Command {

#[fixture]
fn test_hugr() -> Hugr {
let df = DFGBuilder::new(FunctionType::new_endo(type_row![BOOL_T])).unwrap();
let df = DFGBuilder::new(Signature::new_endo(type_row![BOOL_T])).unwrap();
let [i] = df.input_wires_arr();
df.finish_prelude_hugr_with_outputs([i]).unwrap()
}
Expand Down Expand Up @@ -83,7 +83,7 @@ fn test_mermaid(test_hugr_file: NamedTempFile, mut cmd: Command) {

#[rstest]
fn test_bad_hugr(mut cmd: Command) {
let df = DFGBuilder::new(FunctionType::new_endo(type_row![QB_T])).unwrap();
let df = DFGBuilder::new(Signature::new_endo(type_row![QB_T])).unwrap();
let bad_hugr = df.hugr().clone();

let bad_hugr_string = serde_json::to_string(&bad_hugr).unwrap();
Expand Down
29 changes: 14 additions & 15 deletions hugr-core/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
//! # use hugr::builder::{BuildError, BuildHandle, Container, DFGBuilder, Dataflow, DataflowHugr, ModuleBuilder, DataflowSubContainer, HugrBuilder};
//! use hugr::extension::prelude::BOOL_T;
//! use hugr::std_extensions::logic::{NotOp, LOGIC_REG};
//! use hugr::types::FunctionType;
//! use hugr::types::Signature;
//!
//! # fn doctest() -> Result<(), BuildError> {
//! let hugr = {
Expand All @@ -42,7 +42,7 @@
//! let _dfg_handle = {
//! let mut dfg = module_builder.define_function(
//! "main",
//! FunctionType::new(vec![BOOL_T], vec![BOOL_T]),
//! Signature::new(vec![BOOL_T], vec![BOOL_T]),
//! )?;
//!
//! // Get the wires from the function inputs.
Expand All @@ -59,7 +59,7 @@
//! let _circuit_handle = {
//! let mut dfg = module_builder.define_function(
//! "circuit",
//! FunctionType::new_endo(vec![BOOL_T, BOOL_T]),
//! Signature::new_endo(vec![BOOL_T, BOOL_T]),
//! )?;
//! let mut circuit = dfg.as_circuit(dfg.input_wires());
//!
Expand Down Expand Up @@ -92,7 +92,7 @@ use crate::hugr::ValidationError;
use crate::ops::handle::{BasicBlockID, CfgID, ConditionalID, DfgID, FuncID, TailLoopID};
use crate::ops::{NamedOp, OpType};
use crate::types::Type;
use crate::types::{ConstTypeError, FunctionType, TypeRow};
use crate::types::{ConstTypeError, Signature, TypeRow};
use crate::{Node, Port, Wire};

pub mod handle;
Expand Down Expand Up @@ -121,16 +121,16 @@ pub use conditional::{CaseBuilder, ConditionalBuilder};
mod circuit;
pub use circuit::{CircuitBuildError, CircuitBuilder};

/// Return a FunctionType with the same input and output types (specified)
/// Return a Signature with the same input and output types (specified)
/// whose extension delta, when used in a non-FuncDefn container, will be inferred.
pub fn ft1(types: impl Into<TypeRow>) -> FunctionType {
FunctionType::new_endo(types).with_extension_delta(TO_BE_INFERRED)
pub fn ft1(types: impl Into<TypeRow>) -> Signature {
Signature::new_endo(types).with_extension_delta(TO_BE_INFERRED)
}

/// Return a FunctionType with the specified input and output types
/// Return a Signature with the specified input and output types
/// whose extension delta, when used in a non-FuncDefn container, will be inferred.
pub fn ft2(inputs: impl Into<TypeRow>, outputs: impl Into<TypeRow>) -> FunctionType {
FunctionType::new(inputs, outputs).with_extension_delta(TO_BE_INFERRED)
pub fn ft2(inputs: impl Into<TypeRow>, outputs: impl Into<TypeRow>) -> Signature {
Signature::new(inputs, outputs).with_extension_delta(TO_BE_INFERRED)
}

#[derive(Debug, Clone, PartialEq, Error)]
Expand Down Expand Up @@ -235,7 +235,7 @@ pub(crate) mod test {
use crate::hugr::{views::HugrView, HugrMut};
use crate::ops;
use crate::std_extensions::arithmetic::float_ops::FLOAT_OPS_REGISTRY;
use crate::types::{FunctionType, PolyFuncType, Type};
use crate::types::{PolyFuncType, Signature, Type};
use crate::{type_row, Hugr};

use super::handle::BuildHandle;
Expand Down Expand Up @@ -271,16 +271,15 @@ pub(crate) mod test {

#[fixture]
pub(crate) fn simple_dfg_hugr() -> Hugr {
let dfg_builder =
DFGBuilder::new(FunctionType::new(type_row![BIT], type_row![BIT])).unwrap();
let dfg_builder = DFGBuilder::new(Signature::new(type_row![BIT], type_row![BIT])).unwrap();
let [i1] = dfg_builder.input_wires_arr();
dfg_builder.finish_prelude_hugr_with_outputs([i1]).unwrap()
}

#[fixture]
pub(crate) fn simple_cfg_hugr() -> Hugr {
let mut cfg_builder =
CFGBuilder::new(FunctionType::new(type_row![NAT], type_row![NAT])).unwrap();
CFGBuilder::new(Signature::new(type_row![NAT], type_row![NAT])).unwrap();
super::cfg::test::build_basic_cfg(&mut cfg_builder).unwrap();
cfg_builder.finish_prelude_hugr().unwrap()
}
Expand All @@ -289,7 +288,7 @@ pub(crate) mod test {
/// for tests which want to avoid having open extension variables after
/// inference. Using DFGBuilder will default to a root node with an open
/// extension variable
pub(crate) fn closed_dfg_root_hugr(signature: FunctionType) -> Hugr {
pub(crate) fn closed_dfg_root_hugr(signature: Signature) -> Hugr {
let mut hugr = Hugr::new(ops::DFG {
signature: signature.clone(),
});
Expand Down
8 changes: 4 additions & 4 deletions hugr-core/src/builder/build_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::{
};

use crate::extension::{ExtensionRegistry, ExtensionSet, PRELUDE_REGISTRY, TO_BE_INFERRED};
use crate::types::{FunctionType, PolyFuncType, Type, TypeArg, TypeRow};
use crate::types::{PolyFuncType, Signature, Type, TypeArg, TypeRow};

use itertools::Itertools;

Expand Down Expand Up @@ -274,7 +274,7 @@ pub trait Dataflow: Container {
// TODO: Should this be one function, or should there be a temporary "op" one like with the others?
fn dfg_builder(
&mut self,
signature: FunctionType,
signature: Signature,
input_wires: impl IntoIterator<Item = Wire>,
) -> Result<DFGBuilder<&mut Hugr>, BuildError> {
let op = ops::DFG {
Expand All @@ -295,7 +295,7 @@ pub trait Dataflow: Container {
) -> Result<DFGBuilder<&mut Hugr>, BuildError> {
let (types, input_wires): (Vec<Type>, Vec<Wire>) = inputs.into_iter().unzip();
self.dfg_builder(
FunctionType::new_endo(types).with_extension_delta(TO_BE_INFERRED),
Signature::new_endo(types).with_extension_delta(TO_BE_INFERRED),
input_wires,
)
}
Expand Down Expand Up @@ -323,7 +323,7 @@ pub trait Dataflow: Container {
let (cfg_node, _) = add_node_with_wires(
self,
ops::CFG {
signature: FunctionType::new(inputs.clone(), output_types.clone())
signature: Signature::new(inputs.clone(), output_types.clone())
.with_extension_delta(extension_delta),
},
input_wires,
Expand Down
32 changes: 16 additions & 16 deletions hugr-core/src/builder/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use super::{
use crate::ops::{self, handle::NodeHandle, DataflowBlock, DataflowParent, ExitBlock, OpType};
use crate::{
extension::{ExtensionRegistry, ExtensionSet},
types::FunctionType,
types::Signature,
};
use crate::{hugr::views::HugrView, types::TypeRow};

Expand Down Expand Up @@ -46,14 +46,14 @@ use crate::{hugr::HugrMut, type_row, Hugr};
/// builder::{BuildError, CFGBuilder, Container, Dataflow, HugrBuilder},
/// extension::{prelude, ExtensionSet},
/// ops, type_row,
/// types::{FunctionType, SumType, Type},
/// types::{Signature, SumType, Type},
/// Hugr,
/// };
///
/// const NAT: Type = prelude::USIZE_T;
///
/// fn make_cfg() -> Result<Hugr, BuildError> {
/// let mut cfg_builder = CFGBuilder::new(FunctionType::new(type_row![NAT], type_row![NAT]))?;
/// let mut cfg_builder = CFGBuilder::new(Signature::new(type_row![NAT], type_row![NAT]))?;
///
/// // Outputs from basic blocks must be packed in a sum which corresponds to
/// // which successor to pick. We'll either choose the first branch and pass
Expand Down Expand Up @@ -82,7 +82,7 @@ use crate::{hugr::HugrMut, type_row, Hugr};
/// // `NAT` arguments: one from the `sum_variants` type, and another from the
/// // entry node's `other_outputs`.
/// let mut successor_builder = cfg_builder.simple_block_builder(
/// FunctionType::new(type_row![NAT, NAT], type_row![NAT]),
/// Signature::new(type_row![NAT, NAT], type_row![NAT]),
/// 1, // only one successor to this block
/// )?;
/// let successor_a = {
Expand All @@ -97,7 +97,7 @@ use crate::{hugr::HugrMut, type_row, Hugr};
///
/// // The only argument to this block is the entry node's `other_outputs`.
/// let mut successor_builder = cfg_builder
/// .simple_block_builder(FunctionType::new(type_row![NAT], type_row![NAT]), 1)?;
/// .simple_block_builder(Signature::new(type_row![NAT], type_row![NAT]), 1)?;
/// let successor_b = {
/// let sum_unary = successor_builder.add_load_value(ops::Value::unary_unit_sum());
/// let [in_wire] = successor_builder.input_wires_arr();
Expand Down Expand Up @@ -150,7 +150,7 @@ impl<H: AsMut<Hugr> + AsRef<Hugr>> SubContainer for CFGBuilder<H> {

impl CFGBuilder<Hugr> {
/// New CFG rooted HUGR builder
pub fn new(signature: FunctionType) -> Result<Self, BuildError> {
pub fn new(signature: Signature) -> Result<Self, BuildError> {
let cfg_op = ops::CFG {
signature: signature.clone(),
};
Expand Down Expand Up @@ -248,7 +248,7 @@ impl<B: AsMut<Hugr> + AsRef<Hugr>> CFGBuilder<B> {
/// This function will return an error if there is an error adding the node.
pub fn simple_block_builder(
&mut self,
signature: FunctionType,
signature: Signature,
n_cases: usize,
) -> Result<BlockBuilder<&mut Hugr>, BuildError> {
self.block_builder(
Expand Down Expand Up @@ -402,7 +402,7 @@ pub(crate) mod test {
let build_result = {
let mut module_builder = ModuleBuilder::new();
let mut func_builder = module_builder
.define_function("main", FunctionType::new(vec![NAT], type_row![NAT]))?;
.define_function("main", Signature::new(vec![NAT], type_row![NAT]))?;
let _f_id = {
let [int] = func_builder.input_wires_arr();

Expand All @@ -428,7 +428,7 @@ pub(crate) mod test {
}
#[test]
fn basic_cfg_hugr() -> Result<(), BuildError> {
let mut cfg_builder = CFGBuilder::new(FunctionType::new(type_row![NAT], type_row![NAT]))?;
let mut cfg_builder = CFGBuilder::new(Signature::new(type_row![NAT], type_row![NAT]))?;
build_basic_cfg(&mut cfg_builder)?;
assert_matches!(cfg_builder.finish_prelude_hugr(), Ok(_));

Expand All @@ -447,8 +447,8 @@ pub(crate) mod test {
let sum = entry_b.make_sum(1, sum2_variants, [inw])?;
entry_b.finish_with_outputs(sum, [])?
};
let mut middle_b = cfg_builder
.simple_block_builder(FunctionType::new(type_row![NAT], type_row![NAT]), 1)?;
let mut middle_b =
cfg_builder.simple_block_builder(Signature::new(type_row![NAT], type_row![NAT]), 1)?;
let middle = {
let c = middle_b.add_load_const(ops::Value::unary_unit_sum());
let [inw] = middle_b.input_wires_arr();
Expand All @@ -462,7 +462,7 @@ pub(crate) mod test {
}
#[test]
fn test_dom_edge() -> Result<(), BuildError> {
let mut cfg_builder = CFGBuilder::new(FunctionType::new(type_row![NAT], type_row![NAT]))?;
let mut cfg_builder = CFGBuilder::new(Signature::new(type_row![NAT], type_row![NAT]))?;
let sum_tuple_const = cfg_builder.add_constant(ops::Value::unary_unit_sum());
let sum_variants = vec![type_row![]];

Expand All @@ -475,7 +475,7 @@ pub(crate) mod test {
entry_b.finish_with_outputs(sum, [])?
};
let mut middle_b =
cfg_builder.simple_block_builder(FunctionType::new(type_row![], type_row![NAT]), 1)?;
cfg_builder.simple_block_builder(Signature::new(type_row![], type_row![NAT]), 1)?;
let middle = {
let c = middle_b.load_const(&sum_tuple_const);
middle_b.finish_with_outputs(c, [inw])?
Expand All @@ -490,11 +490,11 @@ pub(crate) mod test {

#[test]
fn test_non_dom_edge() -> Result<(), BuildError> {
let mut cfg_builder = CFGBuilder::new(FunctionType::new(type_row![NAT], type_row![NAT]))?;
let mut cfg_builder = CFGBuilder::new(Signature::new(type_row![NAT], type_row![NAT]))?;
let sum_tuple_const = cfg_builder.add_constant(ops::Value::unary_unit_sum());
let sum_variants = vec![type_row![]];
let mut middle_b = cfg_builder
.simple_block_builder(FunctionType::new(type_row![NAT], type_row![NAT]), 1)?;
let mut middle_b =
cfg_builder.simple_block_builder(Signature::new(type_row![NAT], type_row![NAT]), 1)?;
let [inw] = middle_b.input_wires_arr();
let middle = {
let c = middle_b.load_const(&sum_tuple_const);
Expand Down
12 changes: 6 additions & 6 deletions hugr-core/src/builder/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,13 +254,13 @@ mod test {
extension::prelude::BOOL_T,
ops::{custom::OpaqueOp, CustomOp},
type_row,
types::FunctionType,
types::Signature,
};

#[test]
fn simple_linear() {
let build_res = build_main(
FunctionType::new(type_row![QB, QB], type_row![QB, QB])
Signature::new(type_row![QB, QB], type_row![QB, QB])
.with_extension_delta(float_types::EXTENSION_ID)
.into(),
|mut f_build| {
Expand Down Expand Up @@ -299,10 +299,10 @@ mod test {
"MyOp",
"unknown op".to_string(),
vec![],
FunctionType::new(vec![QB, NAT], vec![QB]),
Signature::new(vec![QB, NAT], vec![QB]),
));
let build_res = build_main(
FunctionType::new(type_row![QB, QB, NAT], type_row![QB, QB, BOOL_T]).into(),
Signature::new(type_row![QB, QB, NAT], type_row![QB, QB, BOOL_T]).into(),
|mut f_build| {
let [q0, q1, angle]: [Wire; 3] = f_build.input_wires_arr();

Expand All @@ -327,7 +327,7 @@ mod test {
#[test]
fn ancillae() {
let build_res = build_main(
FunctionType::new(type_row![QB], type_row![QB]).into(),
Signature::new(type_row![QB], type_row![QB]).into(),
|mut f_build| {
let mut circ = f_build.as_circuit(f_build.input_wires());
assert_eq!(circ.n_wires(), 1);
Expand Down Expand Up @@ -363,7 +363,7 @@ mod test {
#[test]
fn circuit_builder_errors() {
let _build_res = build_main(
FunctionType::new_endo(type_row![QB, QB]).into(),
Signature::new_endo(type_row![QB, QB]).into(),
|mut f_build| {
let mut circ = f_build.as_circuit(f_build.input_wires());
let [q0, q1] = circ.tracked_units_arr();
Expand Down
10 changes: 5 additions & 5 deletions hugr-core/src/builder/conditional.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::extension::ExtensionRegistry;
use crate::hugr::views::HugrView;
use crate::ops::dataflow::DataflowOpTrait;
use crate::types::{FunctionType, TypeRow};
use crate::types::{Signature, TypeRow};

use crate::ops;
use crate::ops::handle::CaseID;
Expand Down Expand Up @@ -118,7 +118,7 @@ impl<B: AsMut<Hugr> + AsRef<Hugr>> ConditionalBuilder<B> {

let outputs = cond.outputs;
let case_op = ops::Case {
signature: FunctionType::new(inputs.clone(), outputs.clone())
signature: Signature::new(inputs.clone(), outputs.clone())
.with_extension_delta(extension_delta.clone()),
};
let case_node =
Expand All @@ -134,7 +134,7 @@ impl<B: AsMut<Hugr> + AsRef<Hugr>> ConditionalBuilder<B> {
let dfg_builder = DFGBuilder::create_with_io(
self.hugr_mut(),
case_node,
FunctionType::new(inputs, outputs).with_extension_delta(extension_delta),
Signature::new(inputs, outputs).with_extension_delta(extension_delta),
)?;

Ok(CaseBuilder::from_dfg_builder(dfg_builder))
Expand Down Expand Up @@ -186,7 +186,7 @@ impl ConditionalBuilder<Hugr> {

impl CaseBuilder<Hugr> {
/// Initialize a Case rooted HUGR
pub fn new(signature: FunctionType) -> Result<Self, BuildError> {
pub fn new(signature: Signature) -> Result<Self, BuildError> {
let op = ops::Case {
signature: signature.clone(),
};
Expand Down Expand Up @@ -233,7 +233,7 @@ mod test {
let build_result: Result<Hugr, BuildError> = {
let mut module_builder = ModuleBuilder::new();
let mut fbuild = module_builder
.define_function("main", FunctionType::new(type_row![NAT], type_row![NAT]))?;
.define_function("main", Signature::new(type_row![NAT], type_row![NAT]))?;
let tru_const = fbuild.add_constant(Value::true_val());
let _fdef = {
let const_wire = fbuild.load_const(&tru_const);
Expand Down
Loading

0 comments on commit 53e1c4d

Please sign in to comment.