E-graphs and equality saturation in MoonBit, ported from egg
///|
test "simplify with equality saturation" {
let graph = @egg.EGraph()
let root = graph.add_expr(@egg.Expr::parse("(+ 0 (* 1 x))"))
let rules = [
@egg.Rewrite::parse("commute", "(+ ?a ?b)", "(+ ?b ?a)"),
@egg.Rewrite::parse("add-zero", "(+ ?a 0)", "?a"),
@egg.Rewrite::parse("multiply-one", "(* 1 ?a)", "?a"),
]
let report = graph.run(rules)
assert_eq(report.reason, Saturated)
let best = graph.extract(root).unwrap()
assert_eq(best.expression.to_string(), "x")
assert_eq(best.cost, 1L)
}moon add bobzhang/egg@0.1.0moon check --target all --warn-list +unnecessary_annotation --deny-warn
moon test --target all
moon run cmd/demo
moon fmt --check
moon infopython3 validation/compare.pypub struct EGraph[Op] {
// private fields
}pub struct Expr[Op] {
// private fields
}pub struct Rewrite[Op] {
// private fields
}Install
Download zipE-graphs and equality saturation in MoonBit, ported from egg