Rules are the core mechanism for equality saturation in egglog. They match facts in the database and perform actions.
Rule Syntax
A rule matches a list of facts (the body) and executes a list of actions (the head). Matches are performed modulo equality.
(rule ((body_fact1) (body_fact2)) ((action1) (action2)))
Rewrite Syntax
rewrite is syntactic sugar for a rule that unions the left-hand side (LHS) with the right-hand side (RHS).
(rewrite <lhs> <rhs>)
Rewrite Options
:when (<conditions>): The rewrite only applies if the specified conditions are met.:subsume: Causes the LHS to be subsumed after matching, meaning it can no longer be matched in a rule but can still be checked against.bi-rewrite: Generates two rules, one for each direction (LHS $\to$ RHS and RHS $\to$ LHS).
Examples
// Standard rewrite
(rewrite (Add a b) (Add b a))
// Rewrite with condition
(rewrite (Add a b) (Add b a) :when ((= a (Num 0))))
// Rewrite with subsumption
(rewrite (Mul a 2) (bitshift-left a 1) :subsume)
// Bidirectional rewrite
(bi-rewrite (Mul (Var x) (Num 0)) (Var x))
(rule ((edge x y)) ((path x y)))
(rewrite (Add a b) (Add b a))
(bi-rewrite (Mul (Var x) (Num 0)) (Var x))