Skip to content

Commit

Permalink
Move crate::values to crate::ops::constant
Browse files Browse the repository at this point in the history
  • Loading branch information
aborgna-q committed Mar 14, 2024
1 parent e30fb09 commit 7de0332
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 73 deletions.
6 changes: 3 additions & 3 deletions quantinuum-hugr/src/extension/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
1 change: 0 additions & 1 deletion quantinuum-hugr/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
11 changes: 0 additions & 11 deletions quantinuum-hugr/src/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<FunctionType> {
None
}
}

#[enum_dispatch]
/// Methods for Ops to validate themselves and children
pub trait ValidateOp {
Expand Down
39 changes: 34 additions & 5 deletions quantinuum-hugr/src/ops/constant.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -272,21 +275,47 @@ 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};
use serde_yaml::Value as YamlValue;

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()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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
Expand All @@ -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
Expand All @@ -51,11 +53,11 @@ pub trait CustomConst:

/// Const equality for types that have PartialEq
pub fn downcast_equal_consts<T: CustomConst + PartialEq>(
value: &T,
constant: &T,
other: &dyn CustomConst,
) -> bool {
if let Some(other) = other.as_any().downcast_ref::<T>() {
value == other
constant == other
} else {
false
}
Expand All @@ -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,
Expand Down Expand Up @@ -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()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down
6 changes: 3 additions & 3 deletions quantinuum-hugr/src/std_extensions/arithmetic/float_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ use smol_str::SmolStr;

use crate::{
extension::{ExtensionId, ExtensionSet},
ops::constant::CustomConst,
types::{CustomType, Type, TypeBound},
values::CustomConst,
Extension,
};
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])
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions quantinuum-hugr/src/std_extensions/arithmetic/int_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions quantinuum-hugr/src/std_extensions/collections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 7de0332

Please sign in to comment.