From 080eda0a91ddc670b4655076556f8be98cc006f5 Mon Sep 17 00:00:00 2001 From: Seyon Sivarajah Date: Mon, 6 Nov 2023 16:19:23 +0000 Subject: [PATCH] refactor!: remove `SignatureDescription` (#644) BREAKING CHANGES: `SignatureDescription` no longer exists. --- src/extension/op_def.rs | 24 ---------- src/ops.rs | 8 +--- src/ops/custom.rs | 9 +--- src/ops/leaf.rs | 11 +---- src/types.rs | 2 +- src/types/signature.rs | 98 +---------------------------------------- 6 files changed, 5 insertions(+), 147 deletions(-) diff --git a/src/extension/op_def.rs b/src/extension/op_def.rs index 817adcec1..8fe92e4fc 100644 --- a/src/extension/op_def.rs +++ b/src/extension/op_def.rs @@ -8,8 +8,6 @@ use super::{ TypeParametrised, }; -use crate::types::SignatureDescription; - use crate::types::FunctionType; use crate::types::type_param::TypeArg; @@ -34,18 +32,6 @@ pub trait CustomSignatureFunc: Send + Sync { misc: &HashMap, extension_registry: &ExtensionRegistry, ) -> Result; - - /// Describe the signature of a node, given the operation name, - /// values for the type parameters, - /// and 'misc' data from the extension definition YAML. - fn describe_signature( - &self, - _name: &SmolStr, - _arg_values: &[TypeArg], - _misc: &HashMap, - ) -> SignatureDescription { - SignatureDescription::default() - } } // Note this is very much a utility, rather than definitive; @@ -208,16 +194,6 @@ impl OpDef { Ok(res) } - /// Optional description of the ports in the signature. - pub fn signature_desc(&self, args: &[TypeArg]) -> SignatureDescription { - match &self.signature_func { - SignatureFunc::FromDecl { .. } => { - todo!() - } - SignatureFunc::CustomFunc(bf) => bf.describe_signature(&self.name, args, &self.misc), - } - } - pub(crate) fn should_serialize_signature(&self) -> bool { match self.signature_func { SignatureFunc::CustomFunc(_) => true, diff --git a/src/ops.rs b/src/ops.rs index 4926e60b6..f6ef25004 100644 --- a/src/ops.rs +++ b/src/ops.rs @@ -9,7 +9,7 @@ pub mod leaf; pub mod module; pub mod tag; pub mod validate; -use crate::types::{EdgeKind, FunctionType, SignatureDescription, Type}; +use crate::types::{EdgeKind, FunctionType, Type}; use crate::PortIndex; use crate::{Direction, Port}; @@ -189,12 +189,6 @@ pub trait OpTrait { fn signature(&self) -> FunctionType { Default::default() } - /// Optional description of the ports in the signature. - /// - /// Only dataflow operations have a non-empty signature. - fn signature_desc(&self) -> SignatureDescription { - Default::default() - } /// Get the static input type of this operation if it has one (only Some for /// [`LoadConstant`] and [`Call`]) diff --git a/src/ops/custom.rs b/src/ops/custom.rs index 77ef60399..b1c5a39b3 100644 --- a/src/ops/custom.rs +++ b/src/ops/custom.rs @@ -7,7 +7,7 @@ use thiserror::Error; use crate::extension::{ExtensionId, ExtensionRegistry, OpDef, SignatureError}; use crate::hugr::hugrmut::sealed::HugrMutInternals; use crate::hugr::{HugrView, NodeType}; -use crate::types::{type_param::TypeArg, FunctionType, SignatureDescription}; +use crate::types::{type_param::TypeArg, FunctionType}; use crate::{Hugr, Node}; use super::tag::OpTag; @@ -76,13 +76,6 @@ impl OpTrait for ExternalOp { } } - fn signature_desc(&self) -> SignatureDescription { - match self { - Self::Opaque(_) => SignatureDescription::default(), - Self::Extension(ExtensionOp { def, args, .. }) => def.signature_desc(args), - } - } - fn tag(&self) -> OpTag { OpTag::Leaf } diff --git a/src/ops/leaf.rs b/src/ops/leaf.rs index f09cd2328..768d35fcb 100644 --- a/src/ops/leaf.rs +++ b/src/ops/leaf.rs @@ -7,7 +7,7 @@ use super::{OpName, OpTag, OpTrait, StaticTag}; use crate::{ extension::{ExtensionId, ExtensionSet}, - types::{EdgeKind, FunctionType, SignatureDescription, Type, TypeRow}, + types::{EdgeKind, FunctionType, Type, TypeRow}, }; /// Dataflow operations with no children. @@ -118,15 +118,6 @@ impl OpTrait for LeafOp { } } - /// Optional description of the ports in the signature. - fn signature_desc(&self) -> SignatureDescription { - match self { - LeafOp::CustomOp(ext) => ext.signature_desc(), - // TODO: More port descriptions - _ => Default::default(), - } - } - fn other_input(&self) -> Option { Some(EdgeKind::StateOrder) } diff --git a/src/types.rs b/src/types.rs index cabdcf39d..8ea81504e 100644 --- a/src/types.rs +++ b/src/types.rs @@ -10,7 +10,7 @@ pub mod type_row; pub use check::{ConstTypeError, CustomCheckFailure}; pub use custom::CustomType; -pub use signature::{FunctionType, Signature, SignatureDescription}; +pub use signature::{FunctionType, Signature}; pub use type_param::TypeArg; pub use type_row::TypeRow; diff --git a/src/types/signature.rs b/src/types/signature.rs index ce13b5e6e..b2995dec0 100644 --- a/src/types/signature.rs +++ b/src/types/signature.rs @@ -4,13 +4,11 @@ use pyo3::{pyclass, pymethods}; use delegate::delegate; -use smol_str::SmolStr; use std::fmt::{self, Display, Write}; -use std::ops::Index; use crate::extension::ExtensionSet; use crate::types::{Type, TypeRow}; -use crate::{Direction, IncomingPort, OutgoingPort, Port, PortIndex}; +use crate::{Direction, IncomingPort, OutgoingPort, Port}; #[cfg_attr(feature = "pyo3", pyclass)] #[derive(Clone, Debug, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)] @@ -249,97 +247,3 @@ impl Display for Signature { } } } - -/// Descriptive names for the ports in a [`Signature`]. -/// -/// This is a separate type from [`Signature`] as it is not normally used during the compiler operations. -#[cfg_attr(feature = "pyo3", pyclass)] -#[derive(Clone, Default, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)] -pub struct SignatureDescription { - /// Input of the function. - pub input: Vec, - /// Output of the function. - pub output: Vec, -} - -#[cfg_attr(feature = "pyo3", pymethods)] -impl SignatureDescription { - /// The number of wires in the signature. - #[inline(always)] - pub fn is_empty(&self) -> bool { - self.input.is_empty() && self.output.is_empty() - } -} - -impl SignatureDescription { - /// Create a new signature. - pub fn new(input: impl Into>, output: impl Into>) -> Self { - Self { - input: input.into(), - output: output.into(), - } - } - - /// Create a new signature with only linear inputs and outputs. - pub fn new_linear(linear: impl Into>) -> Self { - let linear = linear.into(); - SignatureDescription::new(linear.clone(), linear) - } - - pub(crate) fn row_zip<'a>( - type_row: &'a TypeRow, - name_row: &'a [SmolStr], - ) -> impl Iterator { - name_row - .iter() - .chain(&EmptyStringIterator) - .zip(type_row.iter()) - } - - /// Iterate over the input wires of the signature and their names. - /// - /// Unnamed wires are given an empty string name. - /// - /// TODO: Return Option<&String> instead of &String for the description. - pub fn input_zip<'a>( - &'a self, - signature: &'a Signature, - ) -> impl Iterator { - Self::row_zip(signature.input(), &self.input) - } - - /// Iterate over the output wires of the signature and their names. - /// - /// Unnamed wires are given an empty string name. - pub fn output_zip<'a>( - &'a self, - signature: &'a Signature, - ) -> impl Iterator { - Self::row_zip(signature.output(), &self.output) - } -} - -impl Index for SignatureDescription { - type Output = SmolStr; - - fn index(&self, index: Port) -> &Self::Output { - match index.direction() { - Direction::Incoming => self.input.get(index.index()).unwrap_or(EMPTY_STRING_REF), - Direction::Outgoing => self.output.get(index.index()).unwrap_or(EMPTY_STRING_REF), - } - } -} - -/// An iterator that always returns the an empty string. -pub(crate) struct EmptyStringIterator; - -/// A reference to an empty string. Used by [`EmptyStringIterator`]. -pub(crate) const EMPTY_STRING_REF: &SmolStr = &SmolStr::new_inline(""); - -impl<'a> Iterator for &'a EmptyStringIterator { - type Item = &'a SmolStr; - - fn next(&mut self) -> Option { - Some(EMPTY_STRING_REF) - } -}