Skip to content

Commit 7176108

Browse files
re-enable rational tests
1 parent 34b5235 commit 7176108

File tree

2 files changed

+66
-67
lines changed

2 files changed

+66
-67
lines changed

python/egglog/builtins.py

Lines changed: 62 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -527,92 +527,91 @@ def __add__(self, other: MultiSet[T]) -> MultiSet[T]: ...
527527
def map(self, f: Callable[[T], T]) -> MultiSet[T]: ...
528528

529529

530-
# Removed until egglog experimental supports new backend
531-
# class Rational(BuiltinExpr):
532-
# @method(preserve=True)
533-
# def eval(self) -> Fraction:
534-
# call = _extract_call(self)
535-
# if call.callable != InitRef("Rational"):
536-
# msg = "Rational can only be initialized with the Rational constructor."
537-
# raise BuiltinEvalError(msg)
530+
class Rational(BuiltinExpr):
531+
@method(preserve=True)
532+
def eval(self) -> Fraction:
533+
call = _extract_call(self)
534+
if call.callable != InitRef("Rational"):
535+
msg = "Rational can only be initialized with the Rational constructor."
536+
raise BuiltinEvalError(msg)
538537

539-
# def _to_int(e: TypedExprDecl) -> int:
540-
# expr = e.expr
541-
# if not isinstance(expr, LitDecl):
542-
# msg = "Rational can only be initialized with literals"
543-
# raise BuiltinEvalError(msg)
544-
# assert isinstance(expr.value, int)
545-
# return expr.value
538+
def _to_int(e: TypedExprDecl) -> int:
539+
expr = e.expr
540+
if not isinstance(expr, LitDecl):
541+
msg = "Rational can only be initialized with literals"
542+
raise BuiltinEvalError(msg)
543+
assert isinstance(expr.value, int)
544+
return expr.value
546545

547-
# num, den = call.args
548-
# return Fraction(_to_int(num), _to_int(den))
546+
num, den = call.args
547+
return Fraction(_to_int(num), _to_int(den))
549548

550-
# @method(preserve=True)
551-
# def __float__(self) -> float:
552-
# return float(self.eval())
549+
@method(preserve=True)
550+
def __float__(self) -> float:
551+
return float(self.eval())
553552

554-
# @method(preserve=True)
555-
# def __int__(self) -> int:
556-
# return int(self.eval())
553+
@method(preserve=True)
554+
def __int__(self) -> int:
555+
return int(self.eval())
557556

558-
# @method(egg_fn="rational")
559-
# def __init__(self, num: i64Like, den: i64Like) -> None: ...
557+
@method(egg_fn="rational")
558+
def __init__(self, num: i64Like, den: i64Like) -> None: ...
560559

561-
# @method(egg_fn="to-f64")
562-
# def to_f64(self) -> f64: ...
560+
@method(egg_fn="to-f64")
561+
def to_f64(self) -> f64: ...
563562

564-
# @method(egg_fn="+")
565-
# def __add__(self, other: Rational) -> Rational: ...
563+
@method(egg_fn="+")
564+
def __add__(self, other: Rational) -> Rational: ...
566565

567-
# @method(egg_fn="-")
568-
# def __sub__(self, other: Rational) -> Rational: ...
566+
@method(egg_fn="-")
567+
def __sub__(self, other: Rational) -> Rational: ...
569568

570-
# @method(egg_fn="*")
571-
# def __mul__(self, other: Rational) -> Rational: ...
569+
@method(egg_fn="*")
570+
def __mul__(self, other: Rational) -> Rational: ...
572571

573-
# @method(egg_fn="/")
574-
# def __truediv__(self, other: Rational) -> Rational: ...
572+
@method(egg_fn="/")
573+
def __truediv__(self, other: Rational) -> Rational: ...
575574

576-
# @method(egg_fn="min")
577-
# def min(self, other: Rational) -> Rational: ...
575+
@method(egg_fn="min")
576+
def min(self, other: Rational) -> Rational: ...
578577

579-
# @method(egg_fn="max")
580-
# def max(self, other: Rational) -> Rational: ...
578+
@method(egg_fn="max")
579+
def max(self, other: Rational) -> Rational: ...
581580

582-
# @method(egg_fn="neg")
583-
# def __neg__(self) -> Rational: ...
581+
@method(egg_fn="neg")
582+
def __neg__(self) -> Rational: ...
584583

585-
# @method(egg_fn="abs")
586-
# def __abs__(self) -> Rational: ...
584+
@method(egg_fn="abs")
585+
def __abs__(self) -> Rational: ...
587586

588-
# @method(egg_fn="floor")
589-
# def floor(self) -> Rational: ...
587+
@method(egg_fn="floor")
588+
def floor(self) -> Rational: ...
590589

591-
# @method(egg_fn="ceil")
592-
# def ceil(self) -> Rational: ...
590+
@method(egg_fn="ceil")
591+
def ceil(self) -> Rational: ...
593592

594-
# @method(egg_fn="round")
595-
# def round(self) -> Rational: ...
593+
@method(egg_fn="round")
594+
def round(self) -> Rational: ...
596595

597-
# @method(egg_fn="pow")
598-
# def __pow__(self, other: Rational) -> Rational: ...
596+
@method(egg_fn="pow")
597+
def __pow__(self, other: Rational) -> Rational: ...
599598

600-
# @method(egg_fn="log")
601-
# def log(self) -> Rational: ...
599+
@method(egg_fn="log")
600+
def log(self) -> Rational: ...
602601

603-
# @method(egg_fn="sqrt")
604-
# def sqrt(self) -> Rational: ...
602+
@method(egg_fn="sqrt")
603+
def sqrt(self) -> Rational: ...
605604

606-
# @method(egg_fn="cbrt")
607-
# def cbrt(self) -> Rational: ...
605+
@method(egg_fn="cbrt")
606+
def cbrt(self) -> Rational: ...
608607

609-
# @method(egg_fn="numer") # type: ignore[misc]
610-
# @property
611-
# def numer(self) -> i64: ...
608+
@method(egg_fn="numer") # type: ignore[misc]
609+
@property
610+
def numer(self) -> i64: ...
612611

613-
# @method(egg_fn="denom") # type: ignore[misc]
614-
# @property
615-
# def denom(self) -> i64: ...
612+
@method(egg_fn="denom") # type: ignore[misc]
613+
@property
614+
def denom(self) -> i64: ...
616615

617616

618617
class BigInt(BuiltinExpr):

python/tests/test_high_level.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -512,10 +512,10 @@ def test_set(self):
512512
assert i64(1) in s
513513
assert i64(3) not in s
514514

515-
# def test_rational(self):
516-
# assert Rational(1, 2).eval() == Fraction(1, 2)
517-
# assert float(Rational(1, 2)) == 0.5
518-
# assert int(Rational(1, 1)) == 1
515+
def test_rational(self):
516+
assert Rational(1, 2).eval() == Fraction(1, 2)
517+
assert float(Rational(1, 2)) == 0.5
518+
assert int(Rational(1, 1)) == 1
519519

520520
def test_vec(self):
521521
assert Vec[i64].empty().eval() == ()

0 commit comments

Comments
 (0)