Skip to content

Commit 4339919

Browse files
Merge pull request #27 from metadsl/modules
Add proper module system
2 parents 6c2e36f + 7537084 commit 4339919

File tree

10 files changed

+1158
-910
lines changed

10 files changed

+1158
-910
lines changed

docs/changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ _This project uses semantic versioning. Before 1.0.0, this means that every brea
1212
- Added `Vec` sort
1313
- Added support for variable args for builtin functions, to use in creation of `Vec` and `Set` sorts.
1414
- Switch generated egg names to use `.` as seperate (i.e. `Math.__add__`) instead of `_` (i.e. `Math___add__`)
15+
- Adds support for modules to define functions/sorts/rules without executing them, for reuse in other modules
16+
- Moved simplifying and running rulesets to the `run` and `simplify` methods on the `EGraph` from those methods on the `Ruleset` since we can now create `Rulset`s for modules which don't have an EGraph attached and can't be run
1517

1618
## 0.4.0 (2023-05-03)
1719

docs/reference/egglog-translation.md

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ path_ruleset = egraph.ruleset("path")
311311
# (rule ((edge x y))
312312
# ((path x y)) :ruleset path)
313313
x, y = vars_("x y", i64)
314-
path_ruleset.register(rule(edge(x, y)).then(path(x, y)))
314+
egraph.register(rule(edge(x, y), ruleset=path_ruleset).then(path(x, y)))
315315
```
316316

317317
### Rewrites
@@ -348,8 +348,7 @@ Rulsets can be run as well, by calling the `run` method on them:
348348

349349
```{code-cell} python
350350
# egg: (run 10 :ruleset path)
351-
run_report = path_ruleset.run(10)
352-
run_report
351+
egraph.run(10, ruleset=path_ruleset)
353352
```
354353

355354
After a run, you get a run report, with some timing information as well as whether things were updated.
@@ -406,19 +405,18 @@ step_egraph.register(left(i64(0)), right(i64(0)))
406405
x, y = vars_("x y", i64)
407406
408407
step_left = step_egraph.ruleset("step-left")
409-
step_left.register(
408+
step_right = step_egraph.ruleset("step-right")
409+
step_egraph.register(
410410
rule(
411411
left(x),
412412
right(x),
413-
).then(left(x + 1))
414-
)
415-
416-
step_right = step_egraph.ruleset("step-right")
417-
step_right.register(
413+
ruleset=step_left
414+
).then(left(x + 1)),
418415
rule(
419416
left(x),
420417
right(y),
421418
eq(x).to(y + 1),
419+
ruleset=step_right
422420
).then(right(x))
423421
)
424422
@@ -501,3 +499,48 @@ with egraph:
501499
egraph.check(eq(Math(0)).to(Math(1)))
502500
egraph.check_fail(eq(Math(0)).to(Math(1)))
503501
```
502+
503+
## Include
504+
505+
The `(include <path>)` command is used to add modularity, by allowing you to pull in the source from another egglog file into the current file.
506+
507+
In Python, we support the same use case with the ability to define a `Module` which is then depended on in `EGraph`. All commands registered on a `Module` won't be run immediately on an `EGraph`, but instead stored so that when they are included, they will be run:
508+
509+
```{code-cell} python
510+
# egg file: path.egg
511+
# (relation path (i64 i64))
512+
# (relation edge (i64 i64))
513+
#
514+
# (rule ((edge x y))
515+
# ((path x y)))
516+
#
517+
# (rule ((path x y) (edge y z))
518+
# ((path x z)))
519+
#
520+
# egg:
521+
# (include "path.egg")
522+
# (edge 1 2)
523+
# (edge 2 3)
524+
# (edge 3 4)
525+
# (run 3)
526+
# (check (path 1 3))
527+
528+
path_mod = Module()
529+
path = path_mod.relation("path", i64, i64)
530+
edge = path_mod.relation("edge", i64, i64)
531+
532+
x, y, z = vars_("x y z", i64)
533+
path_mod.register(
534+
rule(edge(x, y)).then(path(x, y)),
535+
rule(path(x, y), edge(y, z)).then(path(x, z)),
536+
)
537+
538+
egraph = EGraph([path_mod])
539+
egraph.register(
540+
edge(1, 2),
541+
edge(2, 3),
542+
edge(3, 4)
543+
)
544+
egraph.run(3)
545+
egraph.check(path(1, 3))
546+
```

python/egglog/bindings.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ class ExtractReport:
214214
##
215215
# Schedules
216216
##
217+
217218
@final
218219
class Saturate:
219220
schedule: _Schedule

0 commit comments

Comments
 (0)