Replace egg-style deferred rebuilding with traditional immediate E-Graph repair - #154
Conversation
Co-authored-by: hzhangxyz <11623447+hzhangxyz@users.noreply.github.com>
Co-authored-by: hzhangxyz <11623447+hzhangxyz@users.noreply.github.com>
Done. Removed the |
There was a problem hiding this comment.
Pull request overview
This PR replaces the egg-style deferred rebuilding mechanism with a traditional E-Graph algorithm that maintains congruence invariants immediately during merge operations, eliminating the need for explicit rebuild() calls.
Key Changes:
- Removed the worklist-based deferred scheduling mechanism from
EGraph - Modified
merge()to call_repair()immediately after merging e-classes - Redesigned
_repair()with an iterative convergence loop to handle cascading upward merges until full congruence is restored
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| egg/apyds_egg/init.py | Removed worklist attribute, eliminated rebuild() method, modified merge() to call _repair() immediately, and updated _repair() to use iterative convergence with a while changed loop for handling cascading merges |
| egg/tests/test_egraph.py | Removed all rebuild() calls after merge operations, renamed test_egraph_rebuild_empty_worklist to test_egraph_immediate_congruence, and renamed test_egraph_are_equal_after_rebuild to test_egraph_are_equal_after_merge to reflect immediate congruence maintenance |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
|
||
|
|
||
| def test_egraph_rebuild_empty_worklist(): | ||
| def test_egraph_immediate_congruence(): |
There was a problem hiding this comment.
The new test name test_egraph_immediate_congruence doesn't accurately describe what this test does. The test simply adds a term and verifies that find returns the same ID - it doesn't test congruence at all (congruence refers to structurally identical terms being merged automatically).
Consider renaming this test to something more descriptive like test_egraph_find_returns_self or test_egraph_single_term to better reflect its purpose.
| def test_egraph_immediate_congruence(): | |
| def test_egraph_find_returns_self(): |
The current implementation uses egg-style deferred rebuilding with a worklist, allowing temporary invariant violations. This PR replaces it with the traditional E-Graph algorithm that maintains congruence immediately.
Changes
worklistattribute fromEGraph.__init__()merge()to repair immediately: Now calls_repair(r)directly after merging instead of adding to worklist_repair()with iterative convergence: Addedchangedflag and while loop to handle cascading upward merges until congruence is fully restoredrebuild()method entirely: Method is no longer needed since congruence is maintained immediately duringmerge()rebuild()calls from tests and renamed tests that referenced rebuildingBehavior difference
Invariants are never violated. All tests pass with the
rebuild()method completely removed.Original prompt
This section details on the original issue you should resolve
<issue_title>使用原始的egraph实现</issue_title>
<issue_description>目前的egg实现参考的是:
这个egraph实现使用了一些技巧,即 worklist / rebuild / deferred scheduling,
我现在希望使用原始的egraph:
我拿gpt写了一个demo: