Parsing and running program strings#
You can provide your program in a special DSL language. Parse it with
egglog.bindings.EGraph.parse_program() and run the resulting commands with
egglog.bindings.EGraph.run_program():
from egglog.bindings import EGraph
egraph = EGraph()
commands = egraph.parse_program("(check (= (+ 1 2) 3))")
commands
[Check(EgglogSpan(SrcFile(None, '(check (= (+ 1 2) 3))'), 0, 21), [Eq(EgglogSpan(SrcFile(None, '(check (= (+ 1 2) 3))'), 7, 20), Call(EgglogSpan(SrcFile(None, '(check (= (+ 1 2) 3))'), 10, 17), '+', [Lit(EgglogSpan(SrcFile(None, '(check (= (+ 1 2) 3))'), 13, 14), Int(1)), Lit(EgglogSpan(SrcFile(None, '(check (= (+ 1 2) 3))'), 15, 16), Int(2))]), Lit(EgglogSpan(SrcFile(None, '(check (= (+ 1 2) 3))'), 18, 19), Int(3)))])]
egraph.run_program(*commands)
[]
When the program is already a string, use
egglog.bindings.EGraph.parse_and_run_program() to parse and execute it in
one call. Supplying a filename adds that source name to parse and runtime error
locations:
egraph.parse_and_run_program(
"(function double (i64) i64 :no-merge)\n(set (double 2) 4)",
filename="generated.egg",
)
[]
The low-level binding can also record successfully executed command batches. Recording is opt-in and preserves program order:
recording_egraph = EGraph(record=True)
recording_egraph.parse_and_run_program("(let $answer 42)\n(check (= $answer 42))")
print(recording_egraph.commands())
(let $answer 42)
(check (= $answer 42))
If a batch fails after executing an earlier command, Egglog keeps the mutation
made by that prefix, but the failed batch is not appended to commands(). Code
that needs transactional behavior should use a separate e-graph or explicit
push/pop boundaries.