.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/eqsat_basic.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_eqsat_basic.py: Basic equality saturation example. ================================== .. GENERATED FROM PYTHON SOURCE LINES 6-46 .. code-block:: Python from __future__ import annotations from egglog import * egraph = EGraph() class Num(Expr): def __init__(self, value: i64Like) -> None: ... @classmethod def var(cls, name: StringLike) -> Num: ... def __add__(self, other: Num) -> Num: ... def __mul__(self, other: Num) -> Num: ... expr1 = Num(2) * (Num.var("x") + Num(3)) expr2 = Num(6) + Num(2) * Num.var("x") a, b, c = vars_("a b c", Num) i, j = vars_("i j", i64) check( # Check that these expressions are equal eq(expr1).to(expr2), # After running these rules, up to ten times ruleset( rewrite(a + b).to(b + a), rewrite(a * (b + c)).to((a * b) + (a * c)), rewrite(Num(i) + Num(j)).to(Num(i + j)), rewrite(Num(i) * Num(j)).to(Num(i * j)), ) * 10, # On these two initial expressions expr1, expr2, ) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.011 seconds) .. _sphx_glr_download_auto_examples_eqsat_basic.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: eqsat_basic.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: eqsat_basic.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_