File tree Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Original file line number Diff line number Diff line change
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 ))
You can’t perform that action at this time.
0 commit comments