diff --git a/quantinuum-hugr/src/extension/prelude.rs b/quantinuum-hugr/src/extension/prelude.rs index 7965323b9f..cdccfdecd5 100644 --- a/quantinuum-hugr/src/extension/prelude.rs +++ b/quantinuum-hugr/src/extension/prelude.rs @@ -6,13 +6,13 @@ use smol_str::SmolStr; use crate::types::SumType; use crate::{ extension::{ExtensionId, TypeDefBound}, + ops::constant::CustomConst, ops::LeafOp, type_row, types::{ type_param::{TypeArg, TypeParam}, CustomType, FunctionType, PolyFuncType, Type, TypeBound, }, - values::CustomConst, Extension, }; @@ -185,7 +185,7 @@ impl CustomConst for ConstUsize { } fn equal_consts(&self, other: &dyn CustomConst) -> bool { - crate::values::downcast_equal_consts(self, other) + crate::ops::constant::downcast_equal_consts(self, other) } fn extension_reqs(&self) -> ExtensionSet { @@ -223,7 +223,7 @@ impl CustomConst for ConstError { } fn equal_consts(&self, other: &dyn CustomConst) -> bool { - crate::values::downcast_equal_consts(self, other) + crate::ops::constant::downcast_equal_consts(self, other) } fn extension_reqs(&self) -> ExtensionSet { diff --git a/quantinuum-hugr/src/lib.rs b/quantinuum-hugr/src/lib.rs index 85b7502a76..e83ea9fbd5 100644 --- a/quantinuum-hugr/src/lib.rs +++ b/quantinuum-hugr/src/lib.rs @@ -149,7 +149,6 @@ pub mod ops; pub mod std_extensions; pub mod types; mod utils; -pub mod values; pub use crate::core::{ CircuitUnit, Direction, IncomingPort, Node, NodeIndex, OutgoingPort, Port, PortIndex, Wire, diff --git a/quantinuum-hugr/src/ops.rs b/quantinuum-hugr/src/ops.rs index 19892545e8..7145b35e10 100644 --- a/quantinuum-hugr/src/ops.rs +++ b/quantinuum-hugr/src/ops.rs @@ -409,17 +409,6 @@ impl OpParent for Conditional {} impl OpParent for FuncDecl {} impl OpParent for ExitBlock {} -/// Properties of operations that represent a function, either as a declaration -/// or by defining a dataflow graph.kkk -pub trait FunctionOp { - /// The type of . - /// - /// Non-container ops like `FuncDecl` return `None` even though they represent a function. - fn inner_function_type(&self) -> Option { - None - } -} - #[enum_dispatch] /// Methods for Ops to validate themselves and children pub trait ValidateOp { diff --git a/quantinuum-hugr/src/ops/constant.rs b/quantinuum-hugr/src/ops/constant.rs index c2be89ee69..14b5ef2221 100644 --- a/quantinuum-hugr/src/ops/constant.rs +++ b/quantinuum-hugr/src/ops/constant.rs @@ -1,16 +1,19 @@ //! Constant value definitions. +mod custom; + use super::{OpName, OpTrait, StaticTag}; use super::{OpTag, OpType}; use crate::extension::ExtensionSet; use crate::types::{CustomType, EdgeKind, SumType, SumTypeError, Type}; -use crate::values::CustomConst; use crate::{Hugr, HugrView}; use itertools::Itertools; use smol_str::SmolStr; use thiserror::Error; +pub use custom::{downcast_equal_consts, CustomConst, CustomSerialized}; + /// An operation returning a constant value. /// /// Represents core types and extension types. @@ -272,14 +275,11 @@ mod test { prelude::{ConstUsize, USIZE_CUSTOM_T, USIZE_T}, ExtensionId, ExtensionRegistry, PRELUDE, }, + ops::constant::CustomSerialized, std_extensions::arithmetic::float_types::{self, ConstF64, FLOAT64_TYPE}, type_row, types::type_param::TypeArg, types::{CustomType, FunctionType, Type, TypeBound, TypeRow}, - values::{ - test::{serialized_float, CustomTestValue}, - CustomSerialized, - }, }; use cool_asserts::assert_matches; use rstest::{fixture, rstest}; @@ -287,6 +287,35 @@ mod test { use super::*; + #[derive(Debug, Clone, serde::Serialize, serde::Deserialize)] + /// A custom constant value used in testing + pub(crate) struct CustomTestValue(pub CustomType); + + #[typetag::serde] + impl CustomConst for CustomTestValue { + fn name(&self) -> SmolStr { + format!("CustomTestValue({:?})", self.0).into() + } + + fn extension_reqs(&self) -> ExtensionSet { + ExtensionSet::singleton(self.0.extension()) + } + + fn get_type(&self) -> Type { + self.0.clone().into() + } + } + + /// A [`CustomSerialized`] encoding a [`FLOAT64_TYPE`] float constant used in testing. + pub(crate) fn serialized_float(f: f64) -> Const { + CustomSerialized::new( + FLOAT64_TYPE, + serde_yaml::Value::Number(f.into()), + float_types::EXTENSION_ID, + ) + .into() + } + fn test_registry() -> ExtensionRegistry { ExtensionRegistry::try_new([PRELUDE.to_owned(), float_types::EXTENSION.to_owned()]).unwrap() } diff --git a/quantinuum-hugr/src/values.rs b/quantinuum-hugr/src/ops/constant/custom.rs similarity index 66% rename from quantinuum-hugr/src/values.rs rename to quantinuum-hugr/src/ops/constant/custom.rs index b45e19d5aa..9d9b6fe684 100644 --- a/quantinuum-hugr/src/values.rs +++ b/quantinuum-hugr/src/ops/constant/custom.rs @@ -1,7 +1,8 @@ -//! Representation of values (shared between [Const] and in future [TypeArg]) +//! Representation of custom constant values. //! -//! [Const]: crate::ops::Const -//! [TypeArg]: crate::types::type_param::TypeArg +//! These can be used as [`Const`] operations in HUGRs. +//! +//! [`Const`]: crate::ops::Const use std::any::Any; @@ -26,7 +27,7 @@ pub trait CustomConst: /// An identifier for the constant. fn name(&self) -> SmolStr; - /// The extension(s) defining the custom value + /// The extension(s) defining the custom constant /// (a set to allow, say, a [List] of [USize]) /// /// [List]: crate::std_extensions::collections::LIST_TYPENAME @@ -39,7 +40,8 @@ pub trait CustomConst: } /// Compare two constants for equality, using downcasting and comparing the definitions. - // Can't derive PartialEq for trait objects + /// + /// If the type implements `PartialEq`, use [`downcast_equal_consts`] to compare the values. fn equal_consts(&self, _other: &dyn CustomConst) -> bool { // false unless overloaded false @@ -51,11 +53,11 @@ pub trait CustomConst: /// Const equality for types that have PartialEq pub fn downcast_equal_consts( - value: &T, + constant: &T, other: &dyn CustomConst, ) -> bool { if let Some(other) = other.as_any().downcast_ref::() { - value == other + constant == other } else { false } @@ -65,7 +67,7 @@ impl_downcast!(CustomConst); impl_box_clone!(CustomConst, CustomConstBoxClone); #[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)] -/// A value stored as a serialized blob that can report its own type. +/// A constant value stored as a serialized blob that can report its own type. pub struct CustomSerialized { typ: Type, value: serde_yaml::Value, @@ -115,39 +117,3 @@ impl PartialEq for dyn CustomConst { (*self).equal_consts(other) } } - -#[cfg(test)] -pub(crate) mod test { - use super::*; - use crate::ops::Const; - use crate::std_extensions::arithmetic::float_types::{self, FLOAT64_TYPE}; - use crate::types::CustomType; - - #[derive(Debug, Clone, serde::Serialize, serde::Deserialize)] - - /// A custom constant value used in testing - pub(crate) struct CustomTestValue(pub CustomType); - #[typetag::serde] - impl CustomConst for CustomTestValue { - fn name(&self) -> SmolStr { - format!("CustomTestValue({:?})", self.0).into() - } - - fn extension_reqs(&self) -> ExtensionSet { - ExtensionSet::singleton(self.0.extension()) - } - - fn get_type(&self) -> Type { - self.0.clone().into() - } - } - - pub(crate) fn serialized_float(f: f64) -> Const { - CustomSerialized { - typ: FLOAT64_TYPE, - value: serde_yaml::Value::Number(f.into()), - extensions: float_types::EXTENSION_ID.into(), - } - .into() - } -} diff --git a/quantinuum-hugr/src/std_extensions/arithmetic/conversions/const_fold.rs b/quantinuum-hugr/src/std_extensions/arithmetic/conversions/const_fold.rs index 030034135d..a66c6f2cfb 100644 --- a/quantinuum-hugr/src/std_extensions/arithmetic/conversions/const_fold.rs +++ b/quantinuum-hugr/src/std_extensions/arithmetic/conversions/const_fold.rs @@ -5,12 +5,12 @@ use crate::{ ConstFold, ConstFoldResult, OpDef, }, ops, + ops::constant::CustomConst, std_extensions::arithmetic::{ float_types::ConstF64, int_types::{get_log_width, ConstIntS, ConstIntU, INT_TYPES}, }, types::ConstTypeError, - values::CustomConst, IncomingPort, }; diff --git a/quantinuum-hugr/src/std_extensions/arithmetic/float_types.rs b/quantinuum-hugr/src/std_extensions/arithmetic/float_types.rs index 3bc15dde9e..6527a21458 100644 --- a/quantinuum-hugr/src/std_extensions/arithmetic/float_types.rs +++ b/quantinuum-hugr/src/std_extensions/arithmetic/float_types.rs @@ -4,8 +4,8 @@ use smol_str::SmolStr; use crate::{ extension::{ExtensionId, ExtensionSet}, + ops::constant::CustomConst, types::{CustomType, Type, TypeBound}, - values::CustomConst, Extension, }; use lazy_static::lazy_static; @@ -13,7 +13,7 @@ use lazy_static::lazy_static; /// The extension identifier. pub const EXTENSION_ID: ExtensionId = ExtensionId::new_unchecked("arithmetic.float.types"); -/// Identfier for the 64-bit IEEE 754-2019 floating-point type. +/// Identifier for the 64-bit IEEE 754-2019 floating-point type. const FLOAT_TYPE_ID: SmolStr = SmolStr::new_inline("float64"); /// 64-bit IEEE 754-2019 floating-point type (as [CustomType]) @@ -61,7 +61,7 @@ impl CustomConst for ConstF64 { } fn equal_consts(&self, other: &dyn CustomConst) -> bool { - crate::values::downcast_equal_consts(self, other) + crate::ops::constant::downcast_equal_consts(self, other) } fn extension_reqs(&self) -> ExtensionSet { diff --git a/quantinuum-hugr/src/std_extensions/arithmetic/int_types.rs b/quantinuum-hugr/src/std_extensions/arithmetic/int_types.rs index d7501f5a39..7c7724cc89 100644 --- a/quantinuum-hugr/src/std_extensions/arithmetic/int_types.rs +++ b/quantinuum-hugr/src/std_extensions/arithmetic/int_types.rs @@ -6,11 +6,11 @@ use smol_str::SmolStr; use crate::{ extension::{ExtensionId, ExtensionSet}, + ops::constant::CustomConst, types::{ type_param::{TypeArg, TypeArgError, TypeParam}, ConstTypeError, CustomType, Type, TypeBound, }, - values::CustomConst, Extension, }; use lazy_static::lazy_static; @@ -150,7 +150,7 @@ impl CustomConst for ConstIntU { format!("u{}({})", self.log_width, self.value).into() } fn equal_consts(&self, other: &dyn CustomConst) -> bool { - crate::values::downcast_equal_consts(self, other) + crate::ops::constant::downcast_equal_consts(self, other) } fn extension_reqs(&self) -> ExtensionSet { @@ -168,7 +168,7 @@ impl CustomConst for ConstIntS { format!("i{}({})", self.log_width, self.value).into() } fn equal_consts(&self, other: &dyn CustomConst) -> bool { - crate::values::downcast_equal_consts(self, other) + crate::ops::constant::downcast_equal_consts(self, other) } fn extension_reqs(&self) -> ExtensionSet { diff --git a/quantinuum-hugr/src/std_extensions/collections.rs b/quantinuum-hugr/src/std_extensions/collections.rs index 25e81c432e..6c829cbfb7 100644 --- a/quantinuum-hugr/src/std_extensions/collections.rs +++ b/quantinuum-hugr/src/std_extensions/collections.rs @@ -13,12 +13,12 @@ use crate::{ ConstFold, ExtensionId, ExtensionRegistry, ExtensionSet, SignatureError, TypeDef, TypeDefBound, }, + ops::constant::CustomConst, ops::{self, custom::ExtensionOp, OpName}, types::{ type_param::{TypeArg, TypeParam}, CustomCheckFailure, CustomType, FunctionType, PolyFuncType, Type, TypeBound, }, - values::CustomConst, Extension, }; @@ -92,7 +92,7 @@ impl CustomConst for ListValue { } fn equal_consts(&self, other: &dyn CustomConst) -> bool { - crate::values::downcast_equal_consts(self, other) + crate::ops::constant::downcast_equal_consts(self, other) } fn extension_reqs(&self) -> ExtensionSet {