Low Level: egglog.bindings#

This modules contains bindings for the rust egglog library which are meant to be as close to the source as possible. This might result in not the most ergonomic API, if so, we can build higher level abstractions on top of it.

Example:

from egglog.bindings import *

eqsat_basic = """(datatype Math
  (Num i64)
  (Var String)
  (Add Math Math)
  (Mul Math Math))

;; expr1 = 2 * (x + 3)
(let expr1 (Mul (Num 2) (Add (Var "x") (Num 3))))
;; expr2 = 6 + 2 * x
(let expr2 (Add (Num 6) (Mul (Num 2) (Var "x"))))


(rewrite (Add a b)
         (Add b a))
(rewrite (Mul a (Add b c))
         (Add (Mul a b) (Mul a c)))
(rewrite (Add (Num a) (Num b))
         (Num (+ a b)))
(rewrite (Mul (Num a) (Num b))
         (Num (* a b)))

(run 10)
(check (= expr1 expr2))"""

egraph = EGraph()
commands = egraph.parse_program(eqsat_basic)
egraph.run_program(*commands)
[]

The commands are a representation which is close the AST of the egglog text language. We can see this by printing the commands:

for command in commands:
    print(command)
(datatype Math (Num i64) (Var String) (Add Math Math) (Mul Math Math))
(let expr1 (Mul (Num 2) (Add (Var "x") (Num 3))))
(let expr2 (Add (Num 6) (Mul (Num 2) (Var "x"))))
(rewrite (Add a b) (Add b a))
(rewrite (Mul a (Add b c)) (Add (Mul a b) (Mul a c)))
(rewrite (Add (Num a) (Num b)) (Num (+ a b)))
(rewrite (Mul (Num a) (Num b)) (Num (* a b)))
(run-schedule (repeat 10 (run)))
(check (= expr1 expr2))

Bindings for egglog rust library

class egglog.bindings.ActionCommand(action)#
action#
class egglog.bindings.AddRuleset(name)#
name#
class egglog.bindings.Best(termdag, cost, term)#
cost#
term#
termdag#
class egglog.bindings.BiRewriteCommand(name, rewrite)#
name#
rewrite#
class egglog.bindings.Bool(value)#
value#
class egglog.bindings.Call(span, name, args)#
args#
name#
span#
class egglog.bindings.Change(span, change, sym, args)#
args#
change#
span#
sym#
class egglog.bindings.Check(span, facts)#
facts#
span#
class egglog.bindings.Constructor(span, name, schema, cost, unextractable)#
cost#
name#
schema#
span#
unextractable#
class egglog.bindings.Datatype(span, name, variants)#
name#
span#
variants#
class egglog.bindings.Datatypes(span, datatypes)#
datatypes#
span#
class egglog.bindings.Delete#
class egglog.bindings.EGraph#

Create an empty EGraph.

commands()#

Returns the text of the commands that have been run so far, if record was passed.

extract_report()#

Gets the last expressions extracted from the EGraph, if the last command was a Simplify or Extract command.

parse_program(input, /, filename=None)#

Parse a program into a list of commands.

run_program(*commands)#

Run a series of commands on the EGraph. Returns a list of strings representing the output. An EggSmolError is raised if there is problem parsing or executing.

run_report()#

Gets the last run report from the EGraph, if the last command was a run or simplify command.

serialize(root_eclasses, *, max_functions=None, max_calls_per_function=None, include_temporary_functions=False)#

Serialize the EGraph to a SerializedEGraph object.

exception egglog.bindings.EggSmolError(context)#
context#
class egglog.bindings.EgglogSpan(file, i, j)#
file#
i#
j#
class egglog.bindings.Eq(span, left, right)#
left#
right#
span#
class egglog.bindings.Expr_(span, expr)#
expr#
span#
class egglog.bindings.Extract(span, expr, variants)#
expr#
span#
variants#
class egglog.bindings.Fact(expr)#
expr#
class egglog.bindings.Fail(span, command)#
command#
span#
class egglog.bindings.Float(value)#
value#
class egglog.bindings.Function(span, name, schema, merge)#
merge#
name#
schema#
span#
class egglog.bindings.IdentSort(ident, sort)#
ident#
sort#
class egglog.bindings.Include(span, path)#
path#
span#
class egglog.bindings.Input(span, name, file)#
file#
name#
span#
class egglog.bindings.Int(value)#
value#
class egglog.bindings.Let(span, lhs, rhs)#
lhs#
rhs#
span#
class egglog.bindings.Lit(span, value)#
span#
value#
class egglog.bindings.NewSort(name, args)#
args#
name#
class egglog.bindings.Output(span, file, exprs)#
exprs#
file#
span#
class egglog.bindings.Panic(span, msg)#
msg#
span#
class egglog.bindings.PanicSpan#
class egglog.bindings.Pop(span, length)#
length#
span#
class egglog.bindings.PrintFunction(span, name, length)#
length#
name#
span#
class egglog.bindings.PrintOverallStatistics#
class egglog.bindings.PrintSize(span, name)#
name#
span#
class egglog.bindings.Push(length)#
length#
class egglog.bindings.PyObjectSort#
load(expr, /)#
store(obj, /)#

Store a Python object and return an Expr that points to it.

class egglog.bindings.QueryExtract(span, variants, expr)#
expr#
span#
variants#
class egglog.bindings.Relation(span, name, inputs)#
inputs#
name#
span#
class egglog.bindings.Repeat(span, length, schedule)#
length#
schedule#
span#
class egglog.bindings.Rewrite(span, lhs, rhs, conditions=Ellipsis)#
conditions#
lhs#
rhs#
span#
class egglog.bindings.RewriteCommand(name, rewrite, subsume)#
name#
rewrite#
subsume#
class egglog.bindings.Rule(span, head, body)#
body#
head#
span#
class egglog.bindings.RuleCommand(name, ruleset, rule)#
name#
rule#
ruleset#
class egglog.bindings.Run(span, config)#
config#
span#
class egglog.bindings.RunConfig(ruleset, until=Ellipsis)#
ruleset#
until#
class egglog.bindings.RunReport(updated, search_time_per_rule, apply_time_per_rule, search_time_per_ruleset, apply_time_per_ruleset, rebuild_time_per_ruleset, num_matches_per_rule)#
apply_time_per_rule#
apply_time_per_ruleset#
num_matches_per_rule#
rebuild_time_per_ruleset#
search_time_per_rule#
search_time_per_ruleset#
updated#
class egglog.bindings.RunSchedule(schedule)#
schedule#
class egglog.bindings.RustSpan(file, line, column)#
column#
file#
line#
class egglog.bindings.Saturate(span, schedule)#
schedule#
span#
class egglog.bindings.Schema(input, output)#
input#
output#
class egglog.bindings.Sequence(span, schedules)#
schedules#
span#
class egglog.bindings.SerializedEGraph#
inline_leaves()#

Inline all leaf nodes into their parents.

map_ops(map)#

Map each op name to a new op name.

saturate_inline_leaves()#

Inline repeatedly until no more changes are made.

split_classes(egraph, ops)#

Split all primitive nodes, as well as other ops that match, into seperate e-classes

to_dot()#

Serialize the egraph to a dot string.

to_json()#

Serialize the egraph to a json string.

class egglog.bindings.Set(span, lhs, args, rhs)#
args#
lhs#
rhs#
span#
class egglog.bindings.SetOption(name, value)#
name#
value#
class egglog.bindings.Simplify(span, expr, schedule)#
expr#
schedule#
span#
class egglog.bindings.Sort(span, name, presort_and_args)#
name#
presort_and_args#
span#
class egglog.bindings.SrcFile(name, contents)#
contents#
name#
class egglog.bindings.String(value)#
value#
class egglog.bindings.SubVariants(variants)#
variants#
class egglog.bindings.Subsume#
class egglog.bindings.TermApp(name, args)#
args#
name#
class egglog.bindings.TermDag#
app(sym, children)#

Make and return a App with the given head symbol and children, and insert into the DAG if it is not already present.

Panics if any of the children are not already in the DAG.

expr_to_term(expr)#

Recursively converts the given expression to a term.

This involves inserting every subexpression into this DAG. Because TermDags are hashconsed, the resulting term is guaranteed to maximally share subterms.

get(id)#

Convert the given id to the corresponding term.

Panics if the id is not valid.

lit(lit)#

Make and return a [Term::Lit] with the given literal, and insert into the DAG if it is not already present.

lookup(node)#

Convert the given term to its id.

Panics if the term does not already exist in this [TermDag].

size()#

Returns the number of nodes in this DAG.

term_to_expr(term, span)#

Recursively converts the given term to an expression.

Panics if the term contains subterms that are not in the DAG.

to_string(term)#

Converts the given term to a string.

Panics if the term or any of its subterms are not in the DAG.

var(sym)#

Make and return a [Term::Var] with the given symbol, and insert into the DAG if it is not already present.

class egglog.bindings.TermLit(value)#
value#
class egglog.bindings.TermVar(name)#
name#
class egglog.bindings.Union(span, lhs, rhs)#
lhs#
rhs#
span#
class egglog.bindings.Unit#
class egglog.bindings.UnstableCombinedRuleset(name, rulesets)#
name#
rulesets#
class egglog.bindings.Var(span, name)#
name#
span#
class egglog.bindings.Variant(span, name, types, cost=Ellipsis)#
cost#
name#
span#
types#
class egglog.bindings.Variants(termdag, terms)#
termdag#
terms#