Skip to content

Commit bf39e4f

Browse files
Add basic example
1 parent 895c2a8 commit bf39e4f

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
"""
2+
Basic equality saturation example.
3+
==================================
4+
"""
5+
from __future__ import annotations
6+
7+
from egg_smol import *
8+
9+
egraph = EGraph()
10+
11+
12+
@egraph.class_
13+
class Num(BaseExpr):
14+
def __init__(self, value: i64Like) -> None:
15+
...
16+
17+
@classmethod
18+
def var(cls, name: StringLike) -> Num: # type: ignore[empty-body]
19+
...
20+
21+
def __add__(self, other: Num) -> Num: # type: ignore[empty-body]
22+
...
23+
24+
def __mul__(self, other: Num) -> Num: # type: ignore[empty-body]
25+
...
26+
27+
28+
# expr1 = 2 * (x + 3)
29+
expr1 = egraph.define("expr1", Num(2) * (Num.var("x") + Num(3)))
30+
# expr2 = 6 + 2 * x
31+
expr2 = egraph.define("expr2", Num(6) + Num(2) * Num.var("x"))
32+
33+
a, b, c = vars_("a b c", Num)
34+
i, j = vars_("i j", i64)
35+
egraph.register(
36+
rewrite(a + b).to(b + a),
37+
rewrite(a * (b + c)).to((a * b) + (a * c)),
38+
rewrite(Num(i) + Num(j)).to(Num(i + j)),
39+
rewrite(Num(i) * Num(j)).to(Num(i * j)),
40+
)
41+
egraph.run(10)
42+
egraph.check(eq(expr1).to(expr2))

0 commit comments

Comments
 (0)