MCPcopy Index your code
hub / github.com/egraphs-good/egg

github.com/egraphs-good/egg @v0.11.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release v0.11.0 ↗ · + Follow
523 symbols 1,671 edges 27 files 199 documented · 38%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

egg logo egg: egraphs good

Crates.io Released Docs.rs Main branch docs Zulip

egg is a library for e-graphs and equality saturation. It can be used to build program optimizers, synthesizers, verifiers, and more. For more information, see these resources:

Also check out the egglog system that provides an alternative approach to equality saturation based on Datalog. It features a language-based design, incremental execution, and composable analyses. See also the paper and the egglog web demo.

Are you using egg? Please cite using the BibTeX below and add your project to the "Awesome E-graphs" page!

BibTeX

<code><pre>@article{2021-egg,

author = {Willsey, Max and Nandi, Chandrakana and Wang, Yisu Remy and Flatt, Oliver and Tatlock, Zachary and Panchekha, Pavel}, title = {egg: Fast and Extensible Equality Saturation}, year = {2021}, issue_date = {January 2021}, publisher = {Association for Computing Machinery}, address = {New York, NY, USA}, volume = {5}, number = {POPL}, url = {https://doi.org/10.1145/3434304}, doi = {10.1145/3434304}, abstract = {An e-graph efficiently represents a congruence relation over many expressions. Although they were originally developed in the late 1970s for use in automated theorem provers, a more recent technique known as equality saturation repurposes e-graphs to implement state-of-the-art, rewrite-driven compiler optimizations and program synthesizers. However, e-graphs remain unspecialized for this newer use case. Equality saturation workloads exhibit distinct characteristics and often require ad-hoc e-graph extensions to incorporate transformations beyond purely syntactic rewrites. This work contributes two techniques that make e-graphs fast and extensible, specializing them to equality saturation. A new amortized invariant restoration technique called rebuilding takes advantage of equality saturation's distinct workload, providing asymptotic speedups over current techniques in practice. A general mechanism called e-class analyses integrates domain-specific analyses into the e-graph, reducing the need for ad hoc manipulation. We implemented these techniques in a new open-source library called egg. Our case studies on three previously published applications of equality saturation highlight how egg's performance and flexibility enable state-of-the-art results across diverse domains.}, journal = {Proc. ACM Program. Lang.}, month = jan, articleno = {23}, numpages = {29}, keywords = {equality saturation, e-graphs} }

Using egg

Add egg to your Cargo.toml like this:

[dependencies]
egg = "0.11.0"

Make sure to compile with --release if you are measuring performance!

Developing

It's written in Rust. Typically, you install Rust using rustup.

Run cargo doc --open to build and open the documentation in a browser.

Before committing/pushing, make sure to run make, which runs all the tests and lints that CI will (including those under feature flags). This requires the cbc solver due to the lp feature.

Tests

Running cargo test will run the tests. Some tests may time out; try cargo test --release if that happens.

There are a couple interesting tests in the tests directory:

  • prop.rs implements propositional logic and proves some simple theorems.
  • math.rs implements real arithmetic, with a little bit of symbolic differentiation.
  • lambda.rs implements a small lambda calculus, using egg as a partial evaluator.

Benchmarking

To get a simple csv of the runtime of each test, you set the environment variable EGG_BENCH_CSV to something to append a row per test to a csv.

Example:

EGG_BENCH_CSV=math.csv cargo test --test math --release -- --nocapture --test --test-threads=1

Extension points exported contracts — how you extend this code

Applier (Interface)
The righthand side of a [`Rewrite`]. An [`Applier`] is anything that can do something with a substitution ([`Subst`]). [5 …
src/rewrite.rs
LanguageChildren (Interface)
A marker that defines acceptable children types for [`define_language!`]. See [`define_language!`] for more details. Yo [4 …
src/language.rs
CostFunction (Interface)
A cost function that can be used by an [`Extractor`]. To extract an expression from an [`EGraph`], the [`Extractor`] re [3 …
src/extract.rs
LanguageMapper (Interface)
Translates `EGraph ` into `EGraph `. For common cases, you don't need to implement this manually. See the pr [1 implementers]
src/egraph.rs
IterationData (Interface)
Custom data to inject into the [`Iteration`]s recorded by a [`Runner`] This trait allows you to add custom data to the [1 …
src/run.rs
LpCostFunction (Interface)
(no doc) [1 implementers]
src/lp_extract.rs
DatalogExtTrait (Interface)
(no doc) [1 implementers]
tests/datalog.rs
Searcher (Interface)
The lefthand side of a [`Rewrite`]. A [`Searcher`] is something that can search the egraph and find matching substituti [2 …
src/rewrite.rs

Core symbols most depended-on inside this repo

parse
called by 117
src/rewrite.rs
iter
called by 103
src/eclass.rs
len
called by 71
src/eclass.rs
insert
called by 58
src/util.rs
add_expr
called by 42
src/egraph.rs
get
called by 40
src/subst.rs
add
called by 40
src/egraph.rs
find
called by 39
src/egraph.rs

Shape

Method 371
Function 74
Class 53
Interface 13
Enum 12

Languages

Rust100%

Modules by API surface

src/explain.rs83 symbols
src/egraph.rs77 symbols
src/language.rs73 symbols
src/run.rs46 symbols
src/rewrite.rs31 symbols
src/pattern.rs28 symbols
src/lp_extract.rs19 symbols
tests/math.rs18 symbols
src/machine.rs18 symbols
src/multipattern.rs15 symbols
src/extract.rs15 symbols
tests/lambda.rs14 symbols

For agents

$ claude mcp add egg \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page