Skip to content

Commit

Permalink
Derive PartialEq for ConstValue by impl'ing PartialEq for Box<dyn Cus…
Browse files Browse the repository at this point in the history
…tomConst>
  • Loading branch information
acl-cqc committed Jul 27, 2023
1 parent 33bd218 commit d8f933c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 34 deletions.
2 changes: 1 addition & 1 deletion src/hugr/typecheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ pub fn typecheck_const(typ: &ClassicType, val: &ConstValue) -> Result<(), ConstT
(Container::Sum(_), _) => {
Err(ConstTypeError::TypeMismatch(ty.clone(), tm.const_type()))
}
(Container::Opaque(ty), ConstValue::Opaque(ty_act, _val)) => {
(Container::Opaque(ty), ConstValue::Opaque((ty_act, _val))) => {
if ty_act != ty {
return Err(ConstTypeError::TypeMismatch(
ty.clone().into(),
Expand Down
45 changes: 12 additions & 33 deletions src/ops/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub(crate) const HUGR_MAX_INT_WIDTH: HugrIntWidthStore =
///
/// TODO: Add more constants
/// TODO: bigger/smaller integers.
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
#[non_exhaustive]
#[allow(missing_docs)]
pub enum ConstValue {
Expand All @@ -69,38 +69,17 @@ pub enum ConstValue {
},
/// A tuple of constant values.
Tuple(Vec<ConstValue>),
/// An opaque constant value, with cached type.
Opaque(CustomType, Box<dyn CustomConst>),
/// An opaque constant value, with cached type
Opaque((CustomType, Box<dyn CustomConst>)),
}

impl PartialEq for ConstValue {
/// An opaque custom constant, with cached type
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct OpaqueValue(CustomType, Box<dyn CustomConst>);

impl PartialEq for Box<dyn CustomConst> {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(
Self::Int {
value: l0,
width: l_width,
},
Self::Int {
value: r0,
width: r_width,
},
) => l0 == r0 && l_width == r_width,
(Self::Opaque(l0, l1), Self::Opaque(r0, r1)) => l0 == r0 && l1.eq(&**r1),
(
Self::Sum { tag, variants, val },
Self::Sum {
tag: t1,
variants: type1,
val: v1,
},
) => tag == t1 && variants == type1 && val == v1,

(Self::Tuple(v1), Self::Tuple(v2)) => v1.eq(v2),
(Self::F64(f1), Self::F64(f2)) => f1 == f2,

_ => false,
}
(**self).eq(&**other)
}
}

Expand All @@ -118,7 +97,7 @@ impl ConstValue {
pub fn const_type(&self) -> ClassicType {
match self {
Self::Int { value: _, width } => HashableType::Int(*width).into(),
Self::Opaque(_, b) => Container::Opaque((*b).custom_type()).into(),
Self::Opaque((_, b)) => Container::Opaque((*b).custom_type()).into(),
Self::Sum { variants, .. } => ClassicType::new_sum(variants.clone()),
Self::Tuple(vals) => {
let row: Vec<_> = vals.iter().map(|val| val.const_type()).collect();
Expand All @@ -132,7 +111,7 @@ impl ConstValue {
match self {
Self::Int { value, width } => format!("const:int<{width}>:{value}"),
Self::F64(f) => format!("const:float:{f}"),
Self::Opaque(_, v) => format!("const:{}", v.name()),
Self::Opaque((_, v)) => format!("const:{}", v.name()),
Self::Sum { tag, val, .. } => {
format!("const:sum:{{tag:{tag}, val:{}}}", val.name())
}
Expand Down Expand Up @@ -203,7 +182,7 @@ impl ConstValue {

impl<T: CustomConst> From<T> for ConstValue {
fn from(v: T) -> Self {
Self::Opaque(v.custom_type(), Box::new(v))
Self::Opaque((v.custom_type(), Box::new(v)))
}
}

Expand Down

0 comments on commit d8f933c

Please sign in to comment.