From 2b81d6f2e58e6f01788ce5422917478ab1fe71eb Mon Sep 17 00:00:00 2001 From: Alan Lawrence Date: Tue, 28 Nov 2023 08:56:17 +0000 Subject: [PATCH] refactor: remove Signature struct (#714) Main uses were removed in #700 and #692 - so just update tests that were using Signature to store two things together. closes #691 --- src/builder/dataflow.rs | 8 ++-- src/hugr/validate/test.rs | 39 ++++++------------- src/macros.rs | 2 +- src/types.rs | 2 +- src/types/signature.rs | 82 +-------------------------------------- 5 files changed, 17 insertions(+), 116 deletions(-) diff --git a/src/builder/dataflow.rs b/src/builder/dataflow.rs index 1db768b52..4761696c7 100644 --- a/src/builder/dataflow.rs +++ b/src/builder/dataflow.rs @@ -437,9 +437,8 @@ pub(crate) mod test { FunctionType::new(type_row![BIT], type_row![BIT]).with_extension_delta(&abc_extensions); let mut parent = DFGBuilder::new(parent_sig)?; - let add_c_sig = FunctionType::new(type_row![BIT], type_row![BIT]) - .with_extension_delta(&c_extensions) - .with_input_extensions(ab_extensions.clone()); + let add_c_sig = + FunctionType::new(type_row![BIT], type_row![BIT]).with_extension_delta(&c_extensions); let [w] = parent.input_wires_arr(); @@ -476,8 +475,7 @@ pub(crate) mod test { // Add another node (a sibling to add_ab) which adds extension C // via a child lift node - let mut add_c = - parent.dfg_builder(add_c_sig.signature, Some(add_c_sig.input_extensions), [w])?; + let mut add_c = parent.dfg_builder(add_c_sig, Some(ab_extensions.clone()), [w])?; let [w] = add_c.input_wires_arr(); let lift_c = add_c.add_dataflow_node( NodeType::new( diff --git a/src/hugr/validate/test.rs b/src/hugr/validate/test.rs index 261b9e56f..424f5f049 100644 --- a/src/hugr/validate/test.rs +++ b/src/hugr/validate/test.rs @@ -453,13 +453,10 @@ fn missing_lift_node() -> Result<(), BuildError> { )?; let [main_input] = main.input_wires_arr(); - let inner_sig = FunctionType::new(type_row![NAT], type_row![NAT]) - // Inner DFG has extension requirements that the wire wont satisfy - .with_input_extensions(ExtensionSet::from_iter([XA, XB])); - let f_builder = main.dfg_builder( - inner_sig.signature, - Some(inner_sig.input_extensions), + FunctionType::new(type_row![NAT], type_row![NAT]), + // Inner DFG has extension requirements that the wire wont satisfy + Some(ExtensionSet::from_iter([XA, XB])), [main_input], )?; let f_inputs = f_builder.input_wires(); @@ -491,14 +488,9 @@ fn too_many_extension() -> Result<(), BuildError> { let [main_input] = main.input_wires_arr(); let inner_sig = FunctionType::new(type_row![NAT], type_row![NAT]) - .with_extension_delta(&ExtensionSet::singleton(&XA)) - .with_input_extensions(ExtensionSet::new()); + .with_extension_delta(&ExtensionSet::singleton(&XA)); - let f_builder = main.dfg_builder( - inner_sig.signature, - Some(inner_sig.input_extensions), - [main_input], - )?; + let f_builder = main.dfg_builder(inner_sig, Some(ExtensionSet::new()), [main_input])?; let f_inputs = f_builder.input_wires(); let f_handle = f_builder.finish_with_outputs(f_inputs)?; let [f_output] = f_handle.outputs_arr(); @@ -529,19 +521,10 @@ fn extensions_mismatch() -> Result<(), BuildError> { let mut main = module_builder.define_function("main", main_sig)?; - let inner_left_sig = FunctionType::new(type_row![], type_row![NAT]) - .with_input_extensions(ExtensionSet::singleton(&XA)); - - let inner_right_sig = FunctionType::new(type_row![], type_row![NAT]) - .with_input_extensions(ExtensionSet::singleton(&XB)); - - let inner_mult_sig = - FunctionType::new(type_row![NAT, NAT], type_row![NAT]).with_input_extensions(all_rs); - let [left_wire] = main .dfg_builder( - inner_left_sig.signature, - Some(inner_left_sig.input_extensions), + FunctionType::new(type_row![], type_row![NAT]), + Some(ExtensionSet::singleton(&XA)), [], )? .finish_with_outputs([])? @@ -549,16 +532,16 @@ fn extensions_mismatch() -> Result<(), BuildError> { let [right_wire] = main .dfg_builder( - inner_right_sig.signature, - Some(inner_right_sig.input_extensions), + FunctionType::new(type_row![], type_row![NAT]), + Some(ExtensionSet::singleton(&XB)), [], )? .finish_with_outputs([])? .outputs_arr(); let builder = main.dfg_builder( - inner_mult_sig.signature, - Some(inner_mult_sig.input_extensions), + FunctionType::new(type_row![NAT, NAT], type_row![NAT]), + Some(all_rs), [left_wire, right_wire], )?; let [_left, _right] = builder.input_wires_arr(); diff --git a/src/macros.rs b/src/macros.rs index a6c4c1e6c..fdeacfe2d 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -47,7 +47,7 @@ pub(crate) use impl_box_clone; /// const U: Type = Type::UNIT; /// let static_row: TypeRow = type_row![U, U]; /// let dynamic_row: TypeRow = vec![U, U, U].into(); -/// let sig = FunctionType::new(static_row, dynamic_row).pure(); +/// let sig = FunctionType::new(static_row, dynamic_row); /// /// let repeated_row: TypeRow = type_row![U; 3]; /// assert_eq!(repeated_row, *sig.output()); diff --git a/src/types.rs b/src/types.rs index 39974d019..ff8164221 100644 --- a/src/types.rs +++ b/src/types.rs @@ -11,7 +11,7 @@ pub mod type_row; pub use check::{ConstTypeError, CustomCheckFailure}; pub use custom::CustomType; pub use poly_func::PolyFuncType; -pub use signature::{FunctionType, Signature}; +pub use signature::FunctionType; pub use type_param::TypeArg; pub use type_row::TypeRow; diff --git a/src/types/signature.rs b/src/types/signature.rs index 152bdd2f3..ebcd44a7c 100644 --- a/src/types/signature.rs +++ b/src/types/signature.rs @@ -2,7 +2,6 @@ use itertools::Either; -use delegate::delegate; use std::fmt::{self, Display, Write}; use super::type_param::TypeParam; @@ -23,15 +22,6 @@ pub struct FunctionType { pub extension_reqs: ExtensionSet, } -#[derive(Clone, Default, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)] -/// A combination of a FunctionType and a set of input extensions, used for declaring functions -pub struct Signature { - /// The underlying signature - pub signature: FunctionType, - /// The extensions which are associated with all the inputs and carried through - pub input_extensions: ExtensionSet, -} - impl FunctionType { /// Builder method, add extension_reqs to an FunctionType pub fn with_extension_delta(mut self, rs: &ExtensionSet) -> Self { @@ -39,19 +29,6 @@ impl FunctionType { self } - /// Instantiate an FunctionType, converting it to a concrete one - pub fn with_input_extensions(self, es: ExtensionSet) -> Signature { - Signature { - signature: self, - input_extensions: es, - } - } - - /// Instantiate a signature with the empty set of extensions - pub fn pure(self) -> Signature { - self.with_input_extensions(ExtensionSet::new()) - } - pub(crate) fn validate( &self, extension_registry: &ExtensionRegistry, @@ -73,21 +50,6 @@ impl FunctionType { } } -impl From for FunctionType { - fn from(sig: Signature) -> Self { - sig.signature - } -} - -impl Signature { - /// Calculate the extension requirements of the output wires - pub fn output_extensions(&self) -> ExtensionSet { - self.input_extensions - .clone() - .union(&self.signature.extension_reqs) - } -} - impl FunctionType { /// The number of wires in the signature. #[inline(always)] @@ -239,17 +201,6 @@ impl FunctionType { } } -impl Signature { - delegate! { - to self.signature { - /// Inputs of the function type - pub fn input(&self) -> &TypeRow; - /// Outputs of the function type - pub fn output(&self) -> &TypeRow; - } - } -} - impl Display for FunctionType { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { if !self.input.is_empty() { @@ -263,20 +214,9 @@ impl Display for FunctionType { } } -impl Display for Signature { - delegate! { - to self.signature { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result; - } - } -} - #[cfg(test)] mod test { - use crate::{ - extension::{prelude::USIZE_T, ExtensionId}, - type_row, - }; + use crate::{extension::prelude::USIZE_T, type_row}; use super::*; #[test] @@ -300,24 +240,4 @@ mod test { assert_eq!(f_type.input_types(), &[Type::UNIT]); assert_eq!(f_type.output_types(), &[USIZE_T]); } - - #[test] - fn test_signature() { - let f_type = FunctionType::new(type_row![Type::UNIT], type_row![USIZE_T]); - - let sig: Signature = f_type.pure(); - - assert_eq!(sig.input(), &type_row![Type::UNIT]); - assert_eq!(sig.output(), &type_row![USIZE_T]); - } - - #[test] - fn test_display() { - let f_type = FunctionType::new(type_row![Type::UNIT], type_row![USIZE_T]); - assert_eq!(f_type.to_string(), "[Tuple([])] -> [[]][usize([])]"); - let sig: Signature = f_type.with_input_extensions(ExtensionSet::singleton( - &ExtensionId::new("Example").unwrap(), - )); - assert_eq!(sig.to_string(), "[Tuple([])] -> [[]][usize([])]"); - } }