Translation to/from egglog#
The high level bindings available at the top module (egglog) expose most of the functionality of the egglog text format. This guide explains how to translate between the two.
Any EGraph can also be converted to egglog with the egraph.as_egglog_string property, as long as it was created with EGraph(save_egglog_string=True).
Call egraph.close() to remove that saved transcript when it is no longer
needed. A recorded e-graph does not accept further commands after it is closed.
If a failure cannot be represented by Egglog’s (fail ...) command—for
example, a Python exception or a parse, expansion, or typechecking error—the
transcript can no longer be guaranteed to replay the live state. Later
commands and transcript reads then raise RuntimeError.
Because the transcript is Egglog source, explicit callable, sort, variable,
ruleset, and let names must also be unambiguous Egglog symbols. Names that are
valid only through the direct AST API are rejected before executing the saved
command; direct e-graphs continue to accept them.
Builtin Types#
Builtin sorts including Unit, String, i64, f64, BigInt, BigRat,
Map, Set, and Vec are exposed as Python classes.
These can be imported from egglog can be instantiated using the class constructor from the equivalent Python type. Many of the functions on them are mapped to Python operators. For example, the >> operator is mapped to __rshift__ so it can be used as a >> b in Python.
from __future__ import annotations
from egglog import *
# egg: (+ 10 2)
i64(10) + i64(2)
i64(10) + 2
# egg: (+ (bigrat (bigint 1) (bigint 2)) (big-rat (bigint 2) (bigint 1)))
BigRat(1, 2) / BigRat(2, 1)
BigRat(BigInt(1), BigInt(2)) / BigRat(BigInt(2), BigInt(1))
These types are also all checked statically with MyPy, so for example, if you try to add a String and a i64, you will get a type error.
Type Promotion#
Since it is cumbersome to have to wrap every Python literal in the corresponding egglog type, we also support converting automatically from the Python primitives to these types when they are passed as arguments. The above could be written as:
i64(10) + 2
i64(10) + 2
BigRat(1, 2) / BigRat(2, 1)
BigRat(BigInt(1), BigInt(2)) / BigRat(BigInt(2), BigInt(1))
The floating-point sort also exposes the backend’s exp(), log(), sqrt(),
and is_finite() primitives. BigRat.to_i64() is partial: it is defined only
when the rational value is an integer that fits in i64. As with other partial
primitives, undefined use in a rule fact skips that match, while undefined use
in an action is an error.
The experimental Rational sort accepts fractions.Fraction and integer
inputs through the RationalLike type. Those values work in arithmetic,
powers, min/max, and comparisons, including reflected operations such as
1 - Rational(1, 2). Comparisons return a Unit fact rather than a Python
bool.
!= Operator#
The != function in egglog works on any two types with the same sort. In Python, this is mapped to the ne function:
# egg: (!= 10 2)
ne(i64(10)).to(i64(2))
ne(i64(10)).to(i64(2))
This is a two part function so that we can statically check both sides are the same type.
Declaring Sorts#
Users can declare their own sorts in Python by subclassing the Expr class:
# egg: (datatype Math)
class Math(Expr):
pass
By default, the Egglog sort name is generated from the module-qualified Python
class name and made safe for Egglog source. You can override it with the
egg_sort keyword argument:
class Math(Expr, egg_sort="Math2"):
pass
Parameterized sorts#
In egglog, the builtin Map sort can be parameterized with the key and value sorts. In Python, we can use the generic typing syntax to do the same:
# egg: (sort MyMap (Map i64 String))
MyMap = Map[i64, String]
# egg: (map-insert (map-empty) 1 "one")
MyMap.empty().insert(i64(1), String("one"))
Map[i64, String].empty().insert(i64(1), String("one"))
Since the generic types in the Map sort as specified with the Generic class, all of the methods will be statically checked, to make sure the right key/value types are used.
This doesn’t require any custom type analysis on our part, only using Python’s built in annotations with generic types.
Generic container operations#
Pair[L, R] and Maybe[T] expose Egglog’s generic product and optional-value
patterns in Python. catch(lambda: expression) converts an undefined partial
primitive call, such as a missing map lookup, into Maybe.none() instead of
failing the surrounding expression.
pair = Pair(i64(1), String("one"))
pair.left, pair.right
present = Maybe[i64].some(1)
missing = catch(lambda: Map[i64, String].empty()[1])
present, missing
(Maybe[i64].some(i64(1)), catch(lambda: Map[i64, String].empty()[i64(1)]))
Maps have a general map_fold_kv primitive. Derived operations can be written
as ordinary expressions at their use sites. Give the fold an explicitly typed
initial value when its result is another container:
numbers = Map[i64, i64].empty().insert(1, 10).insert(2, 20)
map_fold_kv(
lambda result, key, value: result.insert(key, value + 1),
Map[i64, i64].empty(),
numbers,
)
map_fold_kv(lambda result, key, value: result.insert(key, value + 1), Map[i64, i64].empty(), Map[i64, i64].empty().insert(i64(1), i64(10)).insert(i64(2), i64(20)))
Map folding uses opaque, e-graph-local Value order, not a semantic ordering
promised for arbitrary e-class keys. Prefer order-independent callbacks. An
undefined callback makes the fold undefined; use catch in the callback when
undefined values should instead select a fallback expression.
Declaring Functions#
In egglog, the most general way to declare a function is with the (function ...) command. In Python, we can use the @function decorator on a function with no body. The arg and return types are inferred from the function signature:
# egg: (function fib (i64) i64)
@function
def fib(n: i64Like) -> i64:
pass
Note that instead of using i64 as the argument type, we used i64Like which is i64 | int. This allows us statically to declare that this function can take integers as well which will be upcasted to i64 automatically.
The function decorator also accepts keyword arguments that map to backend features. Which ones are valid depends on how
the callable lowers, as described in Functions vs Constructors:
egg_fn: The name of the function in Egglog. By default, this is generated from the module-qualified Python function name and made safe for Egglog source.merge: A function to merge the results of function-style declarations. This must take the old and new return values and return a single value of the same type.cost: The extraction cost for constructor-style declarations.
# egg: (function foo () i64 :merge (max old new))
@function(egg_fn="foo", merge=lambda old, new: old.max(new))
def my_foo() -> i64:
pass
The static types on the decorator preserve the type of the underlying function, so that they can all be checked statically.
Functions vs Constructors#
The Python bindings follow the backend split in egglog:
non-
Unitprimitive-returning callables use function-style loweringbodyless
Unit-returning callables use relation-style loweringeqsort-returning callables use constructor-style lowering
That is not a Python-only policy choice. It comes from which backend features exist on each command:
function-style declarations support
mergeconstructor-style declarations support
costandunextractablesubsumeonly applies to rewrite-backed bodies, so it only makes sense with an explicitruleset
Python automatically infers which backend lowering to use from the callable shape. In practice, Python declarations can
lower to a function, a constructor, or an eager primitive depending on the return kind and whether a body/default
is present.
For bodies and defaults, the canonical lowering mapping is:
Python shape |
Lowering |
|---|---|
non- |
lower to |
|
lower to |
primitive return, body |
lower to eager |
eqsort return, no body, no |
lower to |
eqsort return, no body, with |
lower to |
eqsort return, body, no |
lower to eager |
eqsort return, body, explicit |
lower to |
Constants and class-variable defaults are just zero-arg bodies/defaults, so they follow the same split based on their declared return type:
no-default constants lower like zero-arg declarations, so primitive-returning constants lower as functions, while eqsort-returning constants lower as constructors unless
mergeforces function-style loweringeqsort-returning defaults lower eagerly without a
ruleset, and lower to rewrite-backed defaults with an explicitrulesetprimitive-returning defaults lower eagerly, and cannot use an explicit
ruleset
Options follow that same backend split:
no-body function-style declarations may use
mergebuiltin declarations are primitive/function-style only
constructor-style declarations may use
costandunextractablesubsumeis only valid when an eqsort-returning body is lowered through an explicitrulesetegg_fnand mutating arguments are supported in every casedirect top-level
@function(ruleset=...)declarations require a bodyconstant(..., ruleset=...)declarations require an eqsort-returning defaultconstant(..., merge=...)declarations must not provide a defaultclass-level
ruleset=is still valid shorthand for attaching rewrite-backed eqsort method and class-variable defaults
For the Python ergonomics of attaching rewrite-backed bodies/defaults to an explicit ruleset, see
Python Integration.
Datatype functions#
In egglog, the (datatype ...) command can also be used to declare functions. All of the functions declared in this block return the type of the declared datatype. Similarly, in Python, any methods of an Expr will be registered automatically. These
can be either instance methods (including any supported __ method), class methods, or the __init__ method. The return type of these functions is inferred from the return type of the function. Additionally, any supported keyword argument for the @function decorator can be used here as well, by using the @method decorator to add values.
By default, a method’s Egglog name is generated from its module-qualified Python class name and method name, then made safe for Egglog source. This lets classes define methods with the same Python name and different signatures without mapping them to the same Egglog function.
# egg:
# (datatype Math
# (Num i64)
# (Var String)
# (Add Math Math)
# (Mul Math Math)
# (Neg Math))
class Math(Expr):
@method(egg_fn="Num")
def __init__(self, v: i64Like):
...
@method(egg_fn="Var")
@classmethod
def var(cls, v: StringLike) -> Math:
...
@method(egg_fn="Add")
def __add__(self, other: Math) -> Math:
...
@method(egg_fn="Mul")
def __mul__(self, other: Math) -> Math:
...
@method(egg_fn="Neg")
@property
def neg(self) -> Math:
...
# egg: (Neg (Mul (Num 2) (Add (Var "x") (Num 3)))))
(Math(2) * (Math.var("x") + Math(3))).neg
(Math(2) * (Math.var("x") + Math(3))).neg
As shown above, we can also use the @classmethod and @property decorators to define class methods and properties.
For more information on how to define methods, see the Python Integration guide.
Declarations#
In egglog, the (declare ...) command is syntactic sugar for a nullary function. In Python, these can be declare either as class variables or with the toplevel constant function:
# egg:
# (datatype Boolean)
# (function or (Boolean Boolean) Boolean)
# (declare True Boolean)
# (declare False Boolean)
# (or True False)
#
from typing import ClassVar
class Boolean(Expr):
TRUE: ClassVar[Boolean]
def __or__(self, other: Boolean) -> Boolean:
...
FALSE = constant("False", Boolean)
Boolean.TRUE | FALSE
Boolean.TRUE | False
Relations#
The (relation ...) command is syntactic sugar for a function that returns the Unit type. This can be declared in Python with the relation function:
# egg: (relation path (i64 i64))
# (path 1 2)
path = relation("path", i64, i64)
path(i64(1), i64(2))
path(1, 2)
The correct function type (in this case it would be Callable[[i64, i64], Unit]) is inferred from the arguments to the relation function, so that it can be checked statically.
Running Actions#
To run actions in Python, they are passed as arguments to the egraph.register function. We have constructors to create each kind of action. They are created and registered in this way, so that we can use the same syntax for executing them on the top level egraph as we do for defining them as results for rules.
You can also pass initial actions directly to the high-level constructor:
egraph = EGraph(
let("x", i64(1)),
set_(fib(0)).to(i64(0)),
)
Here are examples of all the actions:
Let#
egraph = EGraph()
# egg: (let x 1)
egraph.register(let("x", i64(1)))
Set#
# egg: (set (fib 0) 0)
egraph.register(set_(fib(0)).to(i64(0)))
# egg: (set (fib 1) 1)
egraph.register(set_(fib(1)).to(i64(1)))
For set_, we use a fluent API of set(...).to(...), so that we can type check that the two values match statically.
Delete#
# egg: (delete (fib 0))
egraph.register(delete(fib(0)))
Union#
# egg: (union (or True False) True)
egraph.register(union(Boolean.TRUE | FALSE).with_(Boolean.TRUE))
Similar to the set function, this uses a fluent API, so that we can verify the types statically.
Expr as an action#
# re-set after deletion:
egraph.register(set_(fib(0)).to(i64(1)))
# egg: (fib 0)
egraph.register(fib(0))
Panic#
# egg: (panic "This is an error")
try:
EGraph().register(panic("This is an error"))
except BaseException as e:
print(e)
Panic: This is an error
Set Cost#
You can also set the cost of individual values, like the egglog experimental feature, to override the default cost from constructing a function:
# egg: (set-cost (fib 0) 1)
egraph.register(set_cost(fib(0), 1))
This will be taken into account when extracting. Any value that can be
converted to an i64 is supported, so dynamic costs can be created in rules;
the resulting cost must be nonnegative.
Note: Unlike in Egglog source, Python does not require a separate declaration
that a callable supports custom costs; calling set_cost enables them
automatically.
You can also get the cost of a function with get_cost, which will return an i64 if one has already been set.
Defining Rules#
To define rules in Python, we create a rule with the rule(*facts).then(*actions) (rule ...) command in egglog.
# egg:
# (rule ((= f0 (fib x))
# (= f1 (fib (+ x 1))))
# ((set (fib (+ x 2)) (+ f0 f1))))
f0, f1, x = vars_("f0 f1 x", i64)
egraph.register(
rule(
eq(f0).to(fib(x)),
eq(f1).to(fib(x + 1)),
).then(set_(fib(x + 2)).to(f0 + f1))
)
Rules use semi-naive evaluation by default. A rule whose higher-order callback
must read tables populated during the same run can opt into naive evaluation
with rule(..., eval_mode="naive"). The third mode,
eval_mode="unsafe-seminaive", skips semi-naive validation and should only be
used when the rule is known to be valid under that evaluation strategy.
Egglog normally decomposes rules before execution. EGraph defaults to
no_decomp=False; pass no_decomp=True to disable decomposition for
subsequently registered rules, and use no_decomp() or
set_no_decomp(...) to inspect or change that setting. For a single rule,
pass no_decomp=True to rule(...) instead. This is an advanced execution
control.
Variables#
Unlike in egglog, variables must be declared before being use and must be given a type. They need a type both so that they can be checked statically and also so that we know what types are used to understand what how the names of the egg functions correspond to the method names.
Facts#
Facts can either be created with the eq function or with Unit expressions.
The eq function is also fluent, similar to set and union, so that we can verify that the types match statically.
Rulesets#
Rulesets can be generated in Python with the ruleset([*rules], [name]) function and used by registering rules with them:
# egg: (relation edge (i64 i64))
edge = relation("edge", i64, i64)
# egg: (ruleset path)
# (rule ((edge x y))
# ((path x y)) :ruleset path)
x, y = vars_("x y", i64)
path_ruleset = ruleset(rule(edge(x, y)).then(path(x, y)), name="path_ruleset")
Rewrites#
Rewrites in egglog are syntactic sugar for rules. In Python, we can use the rewrite(expr).to(expr, *when) function to create a rule that rewrites the first expression to the second expression when the when expressions are true, like the (rewrite ...) command in egglog.
# egg: (rewrite (Add a b) (Add b a))
a, b = vars_("a b", Math)
egraph.register(rewrite(a + b).to(b + a))
Since it uses a fluent API, static type checkers can verify that the type of the first expression matches the type of the second expression.
The (birewrite ...) command in egglog is syntactic sugar for creating two rewrites, one in each direction. In Python, we can use the birewrite(expr).to(expr, *when) function to create two rules that rewrite in each direction.
Using functions to define vars#
Instead of defining variables with vars_, we can also use functions to define variables. This can be more succinct
and also will make sure the variables won’t be used outside of the scope of the function.
# egg: (rewrite (Mul a b) (Mul b a))
# egg: (rewrite (Add a b) (Add b a))
@EGraph().register
def _math(a: Math, b: Math):
yield rewrite(a * b).to(b * a)
yield rewrite(a + b).to(b + a)
Running#
To run the egraph, we can use the egraph.run() function. This will run all the default rules until a fixed point is reached, or until a timeout is reached.
# egg: (run 5)
egraph.run(5)
RunReport(iterations=[IterationReport(rule_set_report=RuleSetReport(changed=True, rule_reports={'rule(eq(f0).to(fib(x)), eq(f1).to(fib(x + 1))).then(set_(fib(x + 2)).to(f0 + f1))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=10), num_matches=1)]}, search_and_apply_time=0:00:00.000020, merge_time=0:00:00.000002), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=True, rule_reports={'rule(eq(f0).to(fib(x)), eq(f1).to(fib(x + 1))).then(set_(fib(x + 2)).to(f0 + f1))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=5), num_matches=0), RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=3), num_matches=1)]}, search_and_apply_time=0:00:00.000013, merge_time=0:00:00.000001), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=True, rule_reports={'rule(eq(f0).to(fib(x)), eq(f1).to(fib(x + 1))).then(set_(fib(x + 2)).to(f0 + f1))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=2), num_matches=0), RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=2), num_matches=1)]}, search_and_apply_time=0:00:00.000008, merge_time=0:00:00.000001), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=True, rule_reports={'rule(eq(f0).to(fib(x)), eq(f1).to(fib(x + 1))).then(set_(fib(x + 2)).to(f0 + f1))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=2), num_matches=0), RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=1), num_matches=1)]}, search_and_apply_time=0:00:00.000007, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=True, rule_reports={'rule(eq(f0).to(fib(x)), eq(f1).to(fib(x + 1))).then(set_(fib(x + 2)).to(f0 + f1))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=2), num_matches=0), RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=1), num_matches=1)]}, search_and_apply_time=0:00:00.000008, merge_time=0:00:00), rebuild_time=datetime.timedelta(0))], updated=True, can_stop=False, search_and_apply_time_per_rule={'rule(eq(f0).to(fib(x)), eq(f1).to(fib(x + 1))).then(set_(fib(x + 2)).to(f0 + f1))': datetime.timedelta(microseconds=32)}, num_matches_per_rule={'rule(eq(f0).to(fib(x)), eq(f1).to(fib(x + 1))).then(set_(fib(x + 2)).to(f0 + f1))': 5}, search_and_apply_time_per_ruleset={'': datetime.timedelta(microseconds=58)}, merge_time_per_ruleset={'': datetime.timedelta(microseconds=6)}, rebuild_time_per_ruleset={'': datetime.timedelta(0)})
Facts can be passed after the timeout to only run until those facts are reached:
# egg: (run 10000 :until (fib 7))
egraph.run(10000, fib(7))
RunReport(iterations=[IterationReport(rule_set_report=RuleSetReport(changed=True, rule_reports={'rule(eq(f0).to(fib(x)), eq(f1).to(fib(x + 1))).then(set_(fib(x + 2)).to(f0 + f1))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=5), num_matches=0), RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=2), num_matches=1)]}, search_and_apply_time=0:00:00.000015, merge_time=0:00:00.000002), rebuild_time=datetime.timedelta(0))], updated=True, can_stop=False, search_and_apply_time_per_rule={'rule(eq(f0).to(fib(x)), eq(f1).to(fib(x + 1))).then(set_(fib(x + 2)).to(f0 + f1))': datetime.timedelta(microseconds=7)}, num_matches_per_rule={'rule(eq(f0).to(fib(x)), eq(f1).to(fib(x + 1))).then(set_(fib(x + 2)).to(f0 + f1))': 1}, search_and_apply_time_per_ruleset={'': datetime.timedelta(microseconds=15)}, merge_time_per_ruleset={'': datetime.timedelta(microseconds=2)}, rebuild_time_per_ruleset={'': datetime.timedelta(0)})
Rulesets can be run as well, by calling the run method on them:
# egg: (run 10 :ruleset path)
egraph.run(10, ruleset=path_ruleset)
RunReport(iterations=[IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={}, search_and_apply_time=0:00:00, merge_time=0:00:00), rebuild_time=datetime.timedelta(0))], updated=False, can_stop=True, search_and_apply_time_per_rule={}, num_matches_per_rule={}, search_and_apply_time_per_ruleset={'path_ruleset': datetime.timedelta(0)}, merge_time_per_ruleset={'path_ruleset': datetime.timedelta(0)}, rebuild_time_per_ruleset={'path_ruleset': datetime.timedelta(0)})
After a run, you get a run report, with some timing information as well as whether things were updated.
Schedules#
EGraph.run can run either a bounded number of iterations or a full schedule.
The bounded form:
egraph.run(5)
is shorthand for running the default ruleset five times. You can also pass a ruleset and optional stop facts:
egraph.run(10, ruleset=path_ruleset)
egraph.run(10000, fib(7))
For more control, pass a Schedule object. Schedules correspond to egglog’s
(run-schedule ...) command and are composed from these Python forms:
Python |
egglog |
Meaning |
|---|---|---|
|
|
Run the default ruleset once. |
|
|
Run one named ruleset once. |
|
|
Run until the fact is reached. |
|
|
Repeat until the schedule stops changing the e-graph. |
|
|
Repeat a schedule exactly |
|
|
Run two schedules in order. |
|
|
Run any number of schedules in order. |
Rulesets are schedules, so egraph.run(path_ruleset) runs path_ruleset once.
For readability, prefer run(path_ruleset) when you are composing a larger
schedule and egraph.run(10, ruleset=path_ruleset) when all you need is a
bounded run.
We can show an example of this by translating the schedule-demo.egg to Python:
; Step with alternating feet, left before right
(relation left (i64))
(relation right (i64))
(left 0)
(right 0)
(ruleset step-left)
(rule ((left x) (right x))
((left (+ x 1)))
:ruleset step-left)
(ruleset step-right)
(rule ((left x) (right y) (= x (+ y 1)))
((right x))
:ruleset step-right)
(run-schedule
(repeat 10
(saturate step-right)
(saturate step-left)))
; We took 10 steps with the left, but the right couldn't go the first round,
; so we took only 9 steps with the right.
(check (left 10))
(check (right 9))
(fail (check (left 11)))
(fail (check (right 10)))
left = relation("left", i64)
right = relation("right", i64)
x, y = vars_("x y", i64)
step_left = ruleset(
rule(
left(x),
right(x),
).then(left(x + 1))
)
step_right = ruleset(
rule(
left(x),
right(y),
eq(x).to(y + 1),
).then(right(x))
)
step_egraph = EGraph()
step_egraph.register(left(i64(0)), right(i64(0)))
step_egraph.run(
seq(
step_right.saturate(),
step_left.saturate(),
) * 10
)
RunReport(iterations=[IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={'rule(left(x), right(y), eq(x).to(y + 1)).then(right(x))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=5), num_matches=0)]}, search_and_apply_time=0:00:00.000008, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=True, rule_reports={'rule(left(x), right(x)).then(left(x + 1))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=5), num_matches=1)]}, search_and_apply_time=0:00:00.000010, merge_time=0:00:00.000002), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={'rule(left(x), right(x)).then(left(x + 1))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=1), num_matches=0)]}, search_and_apply_time=0:00:00.000001, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=True, rule_reports={'rule(left(x), right(y), eq(x).to(y + 1)).then(right(x))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=2), num_matches=1)]}, search_and_apply_time=0:00:00.000005, merge_time=0:00:00.000001), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={'rule(left(x), right(y), eq(x).to(y + 1)).then(right(x))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=8), num_matches=0)]}, search_and_apply_time=0:00:00.000009, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=True, rule_reports={'rule(left(x), right(x)).then(left(x + 1))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=2), num_matches=1)]}, search_and_apply_time=0:00:00.000005, merge_time=0:00:00.000001), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={'rule(left(x), right(x)).then(left(x + 1))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=1), num_matches=0)]}, search_and_apply_time=0:00:00.000001, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=True, rule_reports={'rule(left(x), right(y), eq(x).to(y + 1)).then(right(x))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=2), num_matches=1)]}, search_and_apply_time=0:00:00.000005, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={'rule(left(x), right(y), eq(x).to(y + 1)).then(right(x))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=1), num_matches=0)]}, search_and_apply_time=0:00:00.000003, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=True, rule_reports={'rule(left(x), right(x)).then(left(x + 1))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=2), num_matches=1)]}, search_and_apply_time=0:00:00.000005, merge_time=0:00:00.000001), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={'rule(left(x), right(x)).then(left(x + 1))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=1), num_matches=0)]}, search_and_apply_time=0:00:00.000001, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=True, rule_reports={'rule(left(x), right(y), eq(x).to(y + 1)).then(right(x))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=2), num_matches=1)]}, search_and_apply_time=0:00:00.000011, merge_time=0:00:00.000001), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={'rule(left(x), right(y), eq(x).to(y + 1)).then(right(x))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=2), num_matches=0)]}, search_and_apply_time=0:00:00.000004, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=True, rule_reports={'rule(left(x), right(x)).then(left(x + 1))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=2), num_matches=1)]}, search_and_apply_time=0:00:00.000005, merge_time=0:00:00.000001), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={'rule(left(x), right(x)).then(left(x + 1))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=1), num_matches=0)]}, search_and_apply_time=0:00:00.000001, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=True, rule_reports={'rule(left(x), right(y), eq(x).to(y + 1)).then(right(x))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=2), num_matches=1)]}, search_and_apply_time=0:00:00.000005, merge_time=0:00:00.000001), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={'rule(left(x), right(y), eq(x).to(y + 1)).then(right(x))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=1), num_matches=0)]}, search_and_apply_time=0:00:00.000008, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=True, rule_reports={'rule(left(x), right(x)).then(left(x + 1))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=2), num_matches=1)]}, search_and_apply_time=0:00:00.000005, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={'rule(left(x), right(x)).then(left(x + 1))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=1), num_matches=0)]}, search_and_apply_time=0:00:00.000001, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=True, rule_reports={'rule(left(x), right(y), eq(x).to(y + 1)).then(right(x))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=2), num_matches=1)]}, search_and_apply_time=0:00:00.000005, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={'rule(left(x), right(y), eq(x).to(y + 1)).then(right(x))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=1), num_matches=0)]}, search_and_apply_time=0:00:00.000003, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=True, rule_reports={'rule(left(x), right(x)).then(left(x + 1))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=1), num_matches=1)]}, search_and_apply_time=0:00:00.000016, merge_time=0:00:00.000001), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={'rule(left(x), right(x)).then(left(x + 1))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=1), num_matches=0)]}, search_and_apply_time=0:00:00.000002, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=True, rule_reports={'rule(left(x), right(y), eq(x).to(y + 1)).then(right(x))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=2), num_matches=1)]}, search_and_apply_time=0:00:00.000005, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={'rule(left(x), right(y), eq(x).to(y + 1)).then(right(x))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=1), num_matches=0)]}, search_and_apply_time=0:00:00.000003, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=True, rule_reports={'rule(left(x), right(x)).then(left(x + 1))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=1), num_matches=1)]}, search_and_apply_time=0:00:00.000004, merge_time=0:00:00.000001), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={'rule(left(x), right(x)).then(left(x + 1))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=1), num_matches=0)]}, search_and_apply_time=0:00:00.000001, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=True, rule_reports={'rule(left(x), right(y), eq(x).to(y + 1)).then(right(x))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=1), num_matches=1)]}, search_and_apply_time=0:00:00.000004, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={'rule(left(x), right(y), eq(x).to(y + 1)).then(right(x))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=1), num_matches=0)]}, search_and_apply_time=0:00:00.000003, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=True, rule_reports={'rule(left(x), right(x)).then(left(x + 1))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=1), num_matches=1)]}, search_and_apply_time=0:00:00.000010, merge_time=0:00:00.000001), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={'rule(left(x), right(x)).then(left(x + 1))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=1), num_matches=0)]}, search_and_apply_time=0:00:00.000002, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=True, rule_reports={'rule(left(x), right(y), eq(x).to(y + 1)).then(right(x))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=2), num_matches=1)]}, search_and_apply_time=0:00:00.000005, merge_time=0:00:00.000001), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={'rule(left(x), right(y), eq(x).to(y + 1)).then(right(x))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=2), num_matches=0)]}, search_and_apply_time=0:00:00.000004, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=True, rule_reports={'rule(left(x), right(x)).then(left(x + 1))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=9), num_matches=1)]}, search_and_apply_time=0:00:00.000018, merge_time=0:00:00.000001), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={'rule(left(x), right(x)).then(left(x + 1))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=6), num_matches=0)]}, search_and_apply_time=0:00:00.000006, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=True, rule_reports={'rule(left(x), right(y), eq(x).to(y + 1)).then(right(x))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=2), num_matches=1)]}, search_and_apply_time=0:00:00.000006, merge_time=0:00:00.000001), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={'rule(left(x), right(y), eq(x).to(y + 1)).then(right(x))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=2), num_matches=0)]}, search_and_apply_time=0:00:00.000008, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=True, rule_reports={'rule(left(x), right(x)).then(left(x + 1))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=2), num_matches=1)]}, search_and_apply_time=0:00:00.000005, merge_time=0:00:00.000001), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={'rule(left(x), right(x)).then(left(x + 1))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=1), num_matches=0)]}, search_and_apply_time=0:00:00.000001, merge_time=0:00:00), rebuild_time=datetime.timedelta(0))], updated=True, can_stop=False, search_and_apply_time_per_rule={'rule(left(x), right(x)).then(left(x + 1))': datetime.timedelta(microseconds=48), 'rule(left(x), right(y), eq(x).to(y + 1)).then(right(x))': datetime.timedelta(microseconds=49)}, num_matches_per_rule={'rule(left(x), right(x)).then(left(x + 1))': 10, 'rule(left(x), right(y), eq(x).to(y + 1)).then(right(x))': 9}, search_and_apply_time_per_ruleset={'ruleset_132977564197072': datetime.timedelta(microseconds=110), 'ruleset_132977562911760': datetime.timedelta(microseconds=112)}, merge_time_per_ruleset={'ruleset_132977562911760': datetime.timedelta(microseconds=11), 'ruleset_132977564197072': datetime.timedelta(microseconds=14)}, rebuild_time_per_ruleset={'ruleset_132977564197072': datetime.timedelta(0), 'ruleset_132977562911760': datetime.timedelta(0)})
step_egraph.check(left(i64(10)), right(i64(9)))
step_egraph.check_fail(left(i64(11)), right(i64(10)))
Backoff Scheduler#
The custom backoff scheduler can delay rules that produce too many matches in a
single scheduler iteration. Create one with
bo = back_off(match_limit=None, ban_length=None), then pass it to
run(ruleset, *facts, scheduler=bo).
match_limit: per-rule threshold of matches allowed in a single scheduler iteration. If a rule produces more matches than the threshold, that rule is temporarily banned.ban_length: initial ban duration (in scheduler iterations). While banned, that rule is skipped.Exponential backoff: each time a rule is banned, both the threshold and ban length double for that rule. After
times_bannedbans, the effective threshold ismatch_limit << times_bannedand the ban duration isban_length << times_banned.Fast-forwarding: when any rule is banned, the scheduler fast-forwards by the minimum remaining ban to unban at least one rule before checking for termination again.
Defaults: match_limit defaults to 1000; ban_length defaults to 5.
:untilsupport: custom scheduler runs can use at most one non-equality fact as the stop condition. Equality stop facts and multiple stop facts raiseValueError.
For example, this egglog code:
(run-schedule
(let-scheduler bo (back-off :match-limit 10))
(repeat 10 (run-with bo step_right)))
Is translated as:
step_egraph.run(
run(step_right, scheduler=back_off(match_limit=10)) * 10
)
RunReport(iterations=[IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={'rule(left(x), right(y), eq(x).to(y + 1)).then(right(x))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=5), num_matches=10)]}, search_and_apply_time=0:00:00.000011, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=True, rule_reports={'rule(left(x), right(y), eq(x).to(y + 1)).then(right(x))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=1), num_matches=10)]}, search_and_apply_time=0:00:00.000008, merge_time=0:00:00.000001), rebuild_time=datetime.timedelta(microseconds=4)), IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={'rule(left(x), right(y), eq(x).to(y + 1)).then(right(x))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=1), num_matches=0)]}, search_and_apply_time=0:00:00.000003, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={}, search_and_apply_time=0:00:00, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={}, search_and_apply_time=0:00:00, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={}, search_and_apply_time=0:00:00, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={}, search_and_apply_time=0:00:00, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={}, search_and_apply_time=0:00:00, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={}, search_and_apply_time=0:00:00, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={}, search_and_apply_time=0:00:00, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={}, search_and_apply_time=0:00:00, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={}, search_and_apply_time=0:00:00, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={}, search_and_apply_time=0:00:00, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={}, search_and_apply_time=0:00:00, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={}, search_and_apply_time=0:00:00, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={}, search_and_apply_time=0:00:00, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={}, search_and_apply_time=0:00:00, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={}, search_and_apply_time=0:00:00, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={}, search_and_apply_time=0:00:00, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={}, search_and_apply_time=0:00:00, merge_time=0:00:00), rebuild_time=datetime.timedelta(0))], updated=True, can_stop=False, search_and_apply_time_per_rule={'rule(left(x), right(y), eq(x).to(y + 1)).then(right(x))': datetime.timedelta(microseconds=9)}, num_matches_per_rule={'rule(left(x), right(y), eq(x).to(y + 1)).then(right(x))': 10}, search_and_apply_time_per_ruleset={'ruleset_132977562911760': datetime.timedelta(microseconds=24)}, merge_time_per_ruleset={'ruleset_132977562911760': datetime.timedelta(microseconds=2)}, rebuild_time_per_ruleset={'ruleset_132977562911760': datetime.timedelta(microseconds=4)})
By default the scheduler will be created before any other schedules are run.
To control where it is instantiated explicitly, use bo.scope(<schedule>), where it will be created before everything in <schedule>.
So the previous is equivalent to:
bo = back_off(match_limit=10)
step_egraph.run(
bo.scope(run(step_right, scheduler=bo) * 10)
)
RunReport(iterations=[IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={'rule(left(x), right(y), eq(x).to(y + 1)).then(right(x))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=5), num_matches=10)]}, search_and_apply_time=0:00:00.000010, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={'rule(left(x), right(y), eq(x).to(y + 1)).then(right(x))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=1), num_matches=10)]}, search_and_apply_time=0:00:00.000004, merge_time=0:00:00), rebuild_time=datetime.timedelta(microseconds=3)), IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={}, search_and_apply_time=0:00:00, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={}, search_and_apply_time=0:00:00, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={}, search_and_apply_time=0:00:00, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={}, search_and_apply_time=0:00:00, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={}, search_and_apply_time=0:00:00, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={}, search_and_apply_time=0:00:00, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={}, search_and_apply_time=0:00:00, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={}, search_and_apply_time=0:00:00, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={}, search_and_apply_time=0:00:00, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={}, search_and_apply_time=0:00:00, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={}, search_and_apply_time=0:00:00, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={}, search_and_apply_time=0:00:00, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={}, search_and_apply_time=0:00:00, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={}, search_and_apply_time=0:00:00, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={}, search_and_apply_time=0:00:00, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={}, search_and_apply_time=0:00:00, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={}, search_and_apply_time=0:00:00, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={}, search_and_apply_time=0:00:00, merge_time=0:00:00), rebuild_time=datetime.timedelta(0))], updated=False, can_stop=True, search_and_apply_time_per_rule={'rule(left(x), right(y), eq(x).to(y + 1)).then(right(x))': datetime.timedelta(microseconds=7)}, num_matches_per_rule={'rule(left(x), right(y), eq(x).to(y + 1)).then(right(x))': 10}, search_and_apply_time_per_ruleset={'ruleset_132977562911760': datetime.timedelta(microseconds=15)}, merge_time_per_ruleset={'ruleset_132977562911760': datetime.timedelta(microseconds=1)}, rebuild_time_per_ruleset={'ruleset_132977562911760': datetime.timedelta(microseconds=3)})
If you wanted to create the scheduler inside the repeated schedule, you can do:
bo = back_off(match_limit=10)
step_egraph.run(
bo.scope(run(step_right, scheduler=bo)) * 10
)
RunReport(iterations=[IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={'rule(left(x), right(y), eq(x).to(y + 1)).then(right(x))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=7), num_matches=10)]}, search_and_apply_time=0:00:00.000015, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={'rule(left(x), right(y), eq(x).to(y + 1)).then(right(x))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=2), num_matches=10)]}, search_and_apply_time=0:00:00.000007, merge_time=0:00:00.000001), rebuild_time=datetime.timedelta(microseconds=3)), IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={'rule(left(x), right(y), eq(x).to(y + 1)).then(right(x))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=6), num_matches=10)]}, search_and_apply_time=0:00:00.000011, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={'rule(left(x), right(y), eq(x).to(y + 1)).then(right(x))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=2), num_matches=10)]}, search_and_apply_time=0:00:00.000007, merge_time=0:00:00.000001), rebuild_time=datetime.timedelta(microseconds=2)), IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={'rule(left(x), right(y), eq(x).to(y + 1)).then(right(x))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=6), num_matches=10)]}, search_and_apply_time=0:00:00.000012, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={'rule(left(x), right(y), eq(x).to(y + 1)).then(right(x))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=2), num_matches=10)]}, search_and_apply_time=0:00:00.000012, merge_time=0:00:00.000001), rebuild_time=datetime.timedelta(microseconds=2)), IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={'rule(left(x), right(y), eq(x).to(y + 1)).then(right(x))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=6), num_matches=10)]}, search_and_apply_time=0:00:00.000011, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={'rule(left(x), right(y), eq(x).to(y + 1)).then(right(x))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=2), num_matches=10)]}, search_and_apply_time=0:00:00.000011, merge_time=0:00:00.000001), rebuild_time=datetime.timedelta(microseconds=2)), IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={'rule(left(x), right(y), eq(x).to(y + 1)).then(right(x))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=6), num_matches=10)]}, search_and_apply_time=0:00:00.000012, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={'rule(left(x), right(y), eq(x).to(y + 1)).then(right(x))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=2), num_matches=10)]}, search_and_apply_time=0:00:00.000011, merge_time=0:00:00.000023), rebuild_time=datetime.timedelta(microseconds=3)), IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={'rule(left(x), right(y), eq(x).to(y + 1)).then(right(x))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=6), num_matches=10)]}, search_and_apply_time=0:00:00.000016, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={'rule(left(x), right(y), eq(x).to(y + 1)).then(right(x))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=2), num_matches=10)]}, search_and_apply_time=0:00:00.000007, merge_time=0:00:00.000001), rebuild_time=datetime.timedelta(microseconds=3)), IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={'rule(left(x), right(y), eq(x).to(y + 1)).then(right(x))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=4), num_matches=10)]}, search_and_apply_time=0:00:00.000007, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={'rule(left(x), right(y), eq(x).to(y + 1)).then(right(x))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=1), num_matches=10)]}, search_and_apply_time=0:00:00.000005, merge_time=0:00:00), rebuild_time=datetime.timedelta(microseconds=2)), IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={'rule(left(x), right(y), eq(x).to(y + 1)).then(right(x))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=3), num_matches=10)]}, search_and_apply_time=0:00:00.000006, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={'rule(left(x), right(y), eq(x).to(y + 1)).then(right(x))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=1), num_matches=10)]}, search_and_apply_time=0:00:00.000003, merge_time=0:00:00), rebuild_time=datetime.timedelta(microseconds=2)), IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={'rule(left(x), right(y), eq(x).to(y + 1)).then(right(x))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=5), num_matches=10)]}, search_and_apply_time=0:00:00.000008, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={'rule(left(x), right(y), eq(x).to(y + 1)).then(right(x))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(0), num_matches=10)]}, search_and_apply_time=0:00:00.000003, merge_time=0:00:00), rebuild_time=datetime.timedelta(microseconds=2)), IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={'rule(left(x), right(y), eq(x).to(y + 1)).then(right(x))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=2), num_matches=10)]}, search_and_apply_time=0:00:00.000005, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={'rule(left(x), right(y), eq(x).to(y + 1)).then(right(x))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(0), num_matches=10)]}, search_and_apply_time=0:00:00.000008, merge_time=0:00:00), rebuild_time=datetime.timedelta(microseconds=2))], updated=False, can_stop=True, search_and_apply_time_per_rule={'rule(left(x), right(y), eq(x).to(y + 1)).then(right(x))': datetime.timedelta(microseconds=74)}, num_matches_per_rule={'rule(left(x), right(y), eq(x).to(y + 1)).then(right(x))': 100}, search_and_apply_time_per_ruleset={'ruleset_132977562911760': datetime.timedelta(microseconds=187)}, merge_time_per_ruleset={'ruleset_132977562911760': datetime.timedelta(microseconds=37)}, rebuild_time_per_ruleset={'ruleset_132977562911760': datetime.timedelta(microseconds=26)})
This would be equivalent to this egglog:
(run-schedule
(repeat 10
(let-scheduler bo (back-off :match-limit 10))
(run-with bo step_right)))
That distinction matters because a scheduler carries state. Hoisting one
scheduler outside * 10 lets its times_banned counters accumulate across all
ten runs. Placing bo.scope(...) inside * 10 creates a fresh scheduler each
time, so every iteration starts with the initial match_limit and ban_length.
The scheduler bindings above are local to one call to EGraph.run. To carry a
scheduler’s ban state across separate calls on the same e-graph, mark it as
persistent:
bo = back_off(match_limit=10).persistent()
step_egraph.run(run(step_right, scheduler=bo))
step_egraph.run(run(step_right, scheduler=bo))
RunReport(iterations=[IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={}, search_and_apply_time=0:00:00, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={}, search_and_apply_time=0:00:00, merge_time=0:00:00), rebuild_time=datetime.timedelta(0))], updated=False, can_stop=True, search_and_apply_time_per_rule={}, num_matches_per_rule={}, search_and_apply_time_per_ruleset={'ruleset_132977562911760': datetime.timedelta(0)}, merge_time_per_ruleset={'ruleset_132977562911760': datetime.timedelta(0)}, rebuild_time_per_ruleset={'ruleset_132977562911760': datetime.timedelta(0)})
The scheduler is registered once on that e-graph and reused by both calls. A
persistent scheduler has its own identity, so deriving it from another
scheduler configuration does not alias that configuration’s local state.
High-level EGraph.saturate() also waits for RunReport.can_stop, so a
no-change round does not discard work deferred by a persistent scheduler.
Check#
The (check ...) command to verify that some facts are true, can be translated to Python with the egraph.check function:
# egg: (check (= (fib 7) 13))
egraph.check(eq(fib(1)).to(i64(1)))
Low-level proof-mode commands such as Prove, ProveExists, and
ProveExistsOutput are exposed through the bindings layer, but the high-level
Python API does not yet support a complete proof workflow.
Extract#
The (extract ...) command in egglog translates to the egraph.extract method, returning the lowest cost expression:
# egg: (extract (fib 1))
egraph.extract(fib(1))
i64(1)
If you want to see the cost as well, pass in the include_cost flag:
egraph.extract(fib(1), include_cost=True)
(i64(1), 1)
Multiple items can also be extracted, returning a list of the lowest cost expressions, with egraph.extract_multiple:
a, b, c = vars_("a b c", Math)
i, j = vars_("i j", i64)
egraph.register(
rewrite(a * (b * c)).to((a * b) * c),
rewrite(a * (b + c)).to((a * b) + (a * c)),
rewrite(Math(i) + Math(j)).to(Math(i + j)),
rewrite(Math(i) * Math(j)).to(Math(i * j)),
)
# egg:
# (define y (Add (Num 6) (Mul (Num 2) (Var "x")))
# (run 10)
# (extract y :variants 2)
y = egraph.let("y", Math(6) + Math(2) * Math.var("x"))
egraph.run(10)
egraph.extract_multiple(y, 2)
[Math(6) + Math(2) * Math.var("x"), Math(2) * Math.var("x") + Math(6)]
Push/Pop#
The (push) and (pop) commands in egglog can be translated to the context manager on the egraph object:
# egg:
# (push)
# (union (Num 0) (Num 1))
# (check (= (Num 0) (Num 1)))
# (pop)
# (fail (check (= (Num 0) (Num 1))))
with egraph:
egraph.register(union(Math(0)).with_(Math(1)))
egraph.check(eq(Math(0)).to(Math(1)))
egraph.check_fail(eq(Math(0)).to(Math(1)))
Function Sizes#
The (print-size <function name>?) command is translated into either egraph.function_size(fn) to get the number of
rows in one table-backed callable or egraph.all_function_sizes() to list the sizes of all registered function tables.
Relations, constructors, bodyless functions, and bodyless constants have tables; eager and builtin primitives do not:
# (function-size Math)
egraph.function_size(Math)
2
# (function-size)
egraph.all_function_sizes()
[(False, 1),
(· + ·, 2),
(Boolean.TRUE, 1),
(· | ·, 1),
(· * ·, 1),
(Math, 2),
(Math.var, 1),
(fib, 18),
(edge, 0),
(path, 0)]
Overall Statistics#
The (print-stats) command is translated into egraph.stats() to get overall statistics about the EGraph.
# (print-stats)
egraph.stats()
RunReport(iterations=[IterationReport(rule_set_report=RuleSetReport(changed=True, rule_reports={'rule(eq(f0).to(fib(x)), eq(f1).to(fib(x + 1))).then(set_(fib(x + 2)).to(f0 + f1))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=10), num_matches=1)]}, search_and_apply_time=0:00:00.000020, merge_time=0:00:00.000002), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=True, rule_reports={'rule(eq(f0).to(fib(x)), eq(f1).to(fib(x + 1))).then(set_(fib(x + 2)).to(f0 + f1))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=5), num_matches=0), RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=3), num_matches=1)]}, search_and_apply_time=0:00:00.000013, merge_time=0:00:00.000001), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=True, rule_reports={'rule(eq(f0).to(fib(x)), eq(f1).to(fib(x + 1))).then(set_(fib(x + 2)).to(f0 + f1))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=2), num_matches=0), RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=2), num_matches=1)]}, search_and_apply_time=0:00:00.000008, merge_time=0:00:00.000001), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=True, rule_reports={'rule(eq(f0).to(fib(x)), eq(f1).to(fib(x + 1))).then(set_(fib(x + 2)).to(f0 + f1))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=2), num_matches=0), RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=1), num_matches=1)]}, search_and_apply_time=0:00:00.000007, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=True, rule_reports={'rule(eq(f0).to(fib(x)), eq(f1).to(fib(x + 1))).then(set_(fib(x + 2)).to(f0 + f1))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=2), num_matches=0), RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=1), num_matches=1)]}, search_and_apply_time=0:00:00.000008, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=True, rule_reports={'rule(eq(f0).to(fib(x)), eq(f1).to(fib(x + 1))).then(set_(fib(x + 2)).to(f0 + f1))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=5), num_matches=0), RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=2), num_matches=1)]}, search_and_apply_time=0:00:00.000015, merge_time=0:00:00.000002), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=False, rule_reports={}, search_and_apply_time=0:00:00, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=True, rule_reports={'rewrite(a + b).to(b + a)': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=1), num_matches=1)], 'rewrite(Math(i) + Math(j)).to(Math(i + j))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=2), num_matches=0)], 'rule(eq(f0).to(fib(x)), eq(f1).to(fib(x + 1))).then(set_(fib(x + 2)).to(f0 + f1))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=6), num_matches=0), RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=1), num_matches=1)], 'rewrite(a * (b * c)).to(a * b * c)': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=3), num_matches=0)], 'rewrite(Math(i) * Math(j)).to(Math(i * j))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=1), num_matches=0)], 'rewrite(a * (b + c)).to(a * b + a * c)': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(0), num_matches=0)]}, search_and_apply_time=0:00:00.000032, merge_time=0:00:00.000003), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=True, rule_reports={'rewrite(a + b).to(b + a)': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(0), num_matches=1)], 'rewrite(a * (b + c)).to(a * b + a * c)': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(0), num_matches=0)], 'rewrite(Math(i) + Math(j)).to(Math(i + j))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=1), num_matches=0)], 'rule(eq(f0).to(fib(x)), eq(f1).to(fib(x + 1))).then(set_(fib(x + 2)).to(f0 + f1))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=2), num_matches=0), RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=1), num_matches=1)]}, search_and_apply_time=0:00:00.000010, merge_time=0:00:00.000001), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=True, rule_reports={'rule(eq(f0).to(fib(x)), eq(f1).to(fib(x + 1))).then(set_(fib(x + 2)).to(f0 + f1))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=1), num_matches=0), RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=1), num_matches=1)]}, search_and_apply_time=0:00:00.000004, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=True, rule_reports={'rule(eq(f0).to(fib(x)), eq(f1).to(fib(x + 1))).then(set_(fib(x + 2)).to(f0 + f1))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=1), num_matches=0), RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=12), num_matches=1)]}, search_and_apply_time=0:00:00.000016, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=True, rule_reports={'rule(eq(f0).to(fib(x)), eq(f1).to(fib(x + 1))).then(set_(fib(x + 2)).to(f0 + f1))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=2), num_matches=0), RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=1), num_matches=1)]}, search_and_apply_time=0:00:00.000005, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=True, rule_reports={'rule(eq(f0).to(fib(x)), eq(f1).to(fib(x + 1))).then(set_(fib(x + 2)).to(f0 + f1))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=1), num_matches=0), RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=1), num_matches=1)]}, search_and_apply_time=0:00:00.000004, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=True, rule_reports={'rule(eq(f0).to(fib(x)), eq(f1).to(fib(x + 1))).then(set_(fib(x + 2)).to(f0 + f1))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=2), num_matches=0), RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=1), num_matches=1)]}, search_and_apply_time=0:00:00.000005, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=True, rule_reports={'rule(eq(f0).to(fib(x)), eq(f1).to(fib(x + 1))).then(set_(fib(x + 2)).to(f0 + f1))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=1), num_matches=0), RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=1), num_matches=1)]}, search_and_apply_time=0:00:00.000004, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=True, rule_reports={'rule(eq(f0).to(fib(x)), eq(f1).to(fib(x + 1))).then(set_(fib(x + 2)).to(f0 + f1))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=1), num_matches=0), RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=1), num_matches=1)]}, search_and_apply_time=0:00:00.000004, merge_time=0:00:00), rebuild_time=datetime.timedelta(0)), IterationReport(rule_set_report=RuleSetReport(changed=True, rule_reports={'rule(eq(f0).to(fib(x)), eq(f1).to(fib(x + 1))).then(set_(fib(x + 2)).to(f0 + f1))': [RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=1), num_matches=0), RuleReport(plan=None, search_and_apply_time=datetime.timedelta(microseconds=1), num_matches=1)]}, search_and_apply_time=0:00:00.000004, merge_time=0:00:00), rebuild_time=datetime.timedelta(0))], updated=True, can_stop=False, search_and_apply_time_per_rule={'rewrite(Math(i) * Math(j)).to(Math(i * j))': datetime.timedelta(microseconds=1), 'rule(eq(f0).to(fib(x)), eq(f1).to(fib(x + 1))).then(set_(fib(x + 2)).to(f0 + f1))': datetime.timedelta(microseconds=86), 'rewrite(a + b).to(b + a)': datetime.timedelta(microseconds=2), 'rewrite(a * (b + c)).to(a * b + a * c)': datetime.timedelta(microseconds=1), 'rewrite(Math(i) + Math(j)).to(Math(i + j))': datetime.timedelta(microseconds=4), 'rewrite(a * (b * c)).to(a * b * c)': datetime.timedelta(microseconds=3)}, num_matches_per_rule={'rewrite(Math(i) + Math(j)).to(Math(i + j))': 0, 'rewrite(a * (b + c)).to(a * b + a * c)': 0, 'rewrite(Math(i) * Math(j)).to(Math(i * j))': 0, 'rewrite(a + b).to(b + a)': 2, 'rewrite(a * (b * c)).to(a * b * c)': 0, 'rule(eq(f0).to(fib(x)), eq(f1).to(fib(x + 1))).then(set_(fib(x + 2)).to(f0 + f1))': 16}, search_and_apply_time_per_ruleset={'path_ruleset': datetime.timedelta(0), '': datetime.timedelta(microseconds=168)}, merge_time_per_ruleset={'path_ruleset': datetime.timedelta(0), '': datetime.timedelta(microseconds=17)}, rebuild_time_per_ruleset={'': datetime.timedelta(0), 'path_ruleset': datetime.timedelta(0)})
Function Values#
The print-function command is translated into egraph.function_values(fn, [length]?) to get the rows of a
table-backed callable. As with function_size, eager and builtin primitives cannot be inspected this way.
# (print-function fib 3)
egraph.function_values(fib, length=3)
{fib(1): i64(1), fib(0): i64(1), fib(2): i64(2)}
Include#
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.
In Python, we can instead just import the desired types, functions, and rulesets and use them in our EGraph.