Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Equality check between Sum types #1422

Merged
merged 1 commit into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions hugr-py/src/hugr/tys.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,15 +237,18 @@ def as_tuple(self) -> Tuple:
def __repr__(self) -> str:
return f"Sum({self.variant_rows})"

def __eq__(self, other: object) -> bool:
return isinstance(other, Sum) and self.variant_rows == other.variant_rows

def type_bound(self) -> TypeBound:
return TypeBound.join(*(t.type_bound() for r in self.variant_rows for t in r))


@dataclass()
@dataclass(eq=False)
class UnitSum(Sum):
"""Simple :class:`Sum` type with `size` variants of empty rows."""

size: int
size: int = field(compare=False)

def __init__(self, size: int):
self.size = size
Expand All @@ -262,7 +265,7 @@ def __repr__(self) -> str:
return f"UnitSum({self.size})"


@dataclass()
@dataclass(eq=False)
class Tuple(Sum):
"""Product type with `tys` elements. Instances of this type correspond to
:class:`Sum` with a single variant.
Expand Down
14 changes: 14 additions & 0 deletions hugr-py/tests/test_tys.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from __future__ import annotations

from hugr.tys import Bool, Qubit, Sum, Tuple, UnitSum


def test_sums():
assert Sum([[Bool, Qubit]]) == Tuple(Bool, Qubit)
assert Tuple(Bool, Qubit) == Sum([[Bool, Qubit]])
assert Sum([[Bool, Qubit]]).as_tuple() == Sum([[Bool, Qubit]])

assert Tuple() == Sum([[]])
assert UnitSum(0) == Sum([])
assert UnitSum(1) == Tuple()
assert UnitSum(4) == Sum([[], [], [], []])
Loading