.. 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 5-44 .. raw:: html
%3 outer_cluster_1 cluster_1 outer_cluster_6 cluster_6 outer_cluster_0 cluster_0 outer_cluster_3 cluster_3 outer_cluster_2 cluster_2 outer_cluster_8 cluster_8 outer_cluster_4 cluster_4 outer_cluster_String-1145338382888088809 cluster_String-1145338382888088809 outer_cluster_i64-17615343019692007359 cluster_i64-17615343019692007359 outer_cluster_i64-11743562013128004906 cluster_i64-11743562013128004906 outer_cluster_i64-16783941965674463102 cluster_i64-16783941965674463102 Num_var-1145338382888088809:s->String-1145338382888088809 Num___mul__-5871781006564002453:s->Num_var-1145338382888088809 Num___mul__-5871781006564002453:s->Num___init__-11743562013128004906 Num___init__-11743562013128004906:s->i64-11743562013128004906 Num___add__-7659469028595837896:s->Num_var-1145338382888088809 Num___add__-7659469028595837896:s->Num___init__-17615343019692007359 Num___init__-17615343019692007359:s->i64-17615343019692007359 Num___add__-7784354942592584825:s->Num_var-1145338382888088809 Num___add__-7784354942592584825:s->Num___init__-17615343019692007359 Num___init__-16783941965674463102:s->i64-16783941965674463102 Num___mul__-11743562013128004906:s->Num___init__-11743562013128004906 Num___mul__-11743562013128004906:s->Num___init__-17615343019692007359 Num___mul__-17615343019692007359:s->Num___init__-11743562013128004906 Num___mul__-17615343019692007359:s->Num___add__-7784354942592584825 Num___add__-16545935510313822457:s->Num___mul__-5871781006564002453 Num___add__-16545935510313822457:s->Num___mul__-11743562013128004906 Num___add__-5000171696738118755:s->Num___mul__-5871781006564002453 Num___add__-5000171696738118755:s->Num___init__-16783941965674463102 Num_var-1145338382888088809 Num.var String-1145338382888088809 "x" Num___mul__-5871781006564002453 (· * ·) Num___init__-11743562013128004906 Num i64-11743562013128004906 2 Num___add__-7659469028595837896 (· + ·) Num___init__-17615343019692007359 Num Num___add__-7784354942592584825 (· + ·) i64-17615343019692007359 3 Num___init__-16783941965674463102 Num i64-16783941965674463102 6 Num___mul__-11743562013128004906 (· * ·) Num___mul__-17615343019692007359 (· * ·) Num___add__-16545935510313822457 (· + ·) Num___add__-5000171696738118755 (· + ·)


.. code-block:: Python from __future__ import annotations from egglog import * egraph = EGraph() @egraph.class_ class Num(Expr): def __init__(self, value: i64Like) -> None: ... @classmethod def var(cls, name: StringLike) -> Num: # type: ignore[empty-body] ... def __add__(self, other: Num) -> Num: # type: ignore[empty-body] ... def __mul__(self, other: Num) -> Num: # type: ignore[empty-body] ... # expr1 = 2 * (x + 3) expr1 = egraph.let("expr1", Num(2) * (Num.var("x") + Num(3))) # expr2 = 6 + 2 * x expr2 = egraph.let("expr2", Num(6) + Num(2) * Num.var("x")) a, b, c = vars_("a b c", Num) i, j = vars_("i j", i64) egraph.register( 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)), ) egraph.run(10) egraph.check(eq(expr1).to(expr2)) egraph .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.047 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 `_