Egglog. EGraph
(EgglogElixir v0.1.0)
Copy Markdown
Process-owned mutable native egglog e-graph session.
Egglog.EGraph is the interactive counterpart to Egglog.Program.
Use it when you want egglog state to persist across calls, as in a REPL,
Livebook, or exploratory workflow:
{:ok, eg} = Egglog.EGraph.new()
{:ok, _} = Egglog.EGraph.run(eg, "(datatype T (A) (B))")
{:ok, _} = Egglog.EGraph.run(eg, "(let x (A))")Each successful run/3 mutates the native e-graph owned by this session's
Elixir process. Concurrent callers are serialized through that owner process.
This is intentionally different from Egglog.Program.run/3, which runs each
query against a native clone of a loaded base program.
push/2 and pop/2 delegate to native egglog's (push) and (pop)
commands. They are useful for temporary experiments:
:ok = Egglog.EGraph.push(eg)
{:ok, _} = Egglog.EGraph.run(eg, "(let tmp (A))")
:ok = Egglog.EGraph.pop(eg)The result shape of run/3 matches Egglog.Program.run/3: outputs are a
list of %{type: atom(), text: String.t()} maps and stats are a compact map.
Summary
Functions
Runs a native egglog check against the current mutable session.
Boolean variant of check/3.
Asserts that a native egglog check fails.
Closes the native mutable e-graph and stops the session process.
Evaluates an egglog expression in the current mutable session.
Bang variant of eval/2.
Extracts the cheapest native egglog term for expr in the current session.
Bang variant of extract/3.
Looks up a function row after evaluating the given argument expressions.
Bang variant of lookup/3.
Creates a process-owned mutable native e-graph session.
Returns native egglog's tuple count for the current mutable session.
Parses egglog source into a reusable command handle.
Bang variant of parse/1.
Pops native egglog rollback points.
Pushes a native egglog rollback point.
Runs egglog code against the mutable session.
Bang variant of run/3.
Builds a snapshot of the current mutable e-graph.
Bang variant of snapshot/2.
Types
@type input() :: String.t() | [String.t()] | Egglog.Commands.t()
Raw or parsed egglog commands accepted by run/3.
@type run_result() :: %{ :status => atom(), :outputs => [%{type: atom(), text: String.t()}], :stats => map(), :report => map(), optional(:snapshot) => map() }
Result returned by run/3.
@opaque t()
Decoded value returned by eval/2 and lookup/3.
:value contains a native egglog e-class value rendered as text. Primitive
egglog values are decoded to ordinary Elixir values when possible:
integers, floats, booleans, strings, and nil for unit.
Functions
Runs a native egglog check against the current mutable session.
Supported options are the same as run/3; they are passed through to the
internal (check ...) run. Defaults are therefore the run/3 defaults.
Only the check result is returned. Snapshot and output options may affect the internal run, but their result data is not exposed by this function.
Boolean variant of check/3.
Runs a native egglog check against the current mutable session.
Supported options are the same as run/3; they are passed through to the
internal (check ...) run. Defaults are therefore the run/3 defaults.
Only the check result is returned. Snapshot and output options may affect the internal run, but their result data is not exposed by this function.
Raises on non-check errors so syntax/type/native errors are not silently treated as false mathematical assertions.
Asserts that a native egglog check fails.
Supported options are the same as run/3; they are passed through to the
internal (fail (check ...)) run. Defaults are therefore the run/3
defaults. Only :ok or {:error, reason} is returned.
Closes the native mutable e-graph and stops the session process.
Evaluates an egglog expression in the current mutable session.
Primitive values are decoded into Elixir values. Non-primitive e-class values
are returned with type: :value.
Bang variant of eval/2.
Extracts the cheapest native egglog term for expr in the current session.
Supported options:
:variants- optional value inserted as the second argument to native egglog's(extract expr variants)command. Defaults to unset, which uses(extract expr).
All run/3 options are also accepted and passed through to the internal
extract run. Defaults are the run/3 defaults. Snapshot and output options
may affect the internal run, but extract/3 returns only the extracted text.
String option keys are also accepted, matching the rest of the wrapper.
Bang variant of extract/3.
Extracts the cheapest native egglog term for expr in the current session.
Supported options:
:variants- optional value inserted as the second argument to native egglog's(extract expr variants)command. Defaults to unset, which uses(extract expr).
All run/3 options are also accepted and passed through to the internal
extract run. Defaults are the run/3 defaults. Snapshot and output options
may affect the internal run, but extract/3 returns only the extracted text.
String option keys are also accepted, matching the rest of the wrapper.
Raises if native egglog rejects the input or extraction output is not available.
Looks up a function row after evaluating the given argument expressions.
Returns {:ok, nil} when no row exists for the evaluated key.
Bang variant of lookup/3.
Creates a process-owned mutable native e-graph session.
source is optional initial egglog code. Unlike Egglog.Program, this state
is not a read-only base: later run/3 calls mutate it directly through the
session owner process.
Options:
:name- human-readable session name stored in the returned struct. Defaults tonil; it is not passed to native egglog.:proofs- create the native e-graph with proof support enabled. Defaults tofalse. Truthy values aretrue,"true","1", and1; all other values are treated as false.
String option keys are also accepted, matching the rest of the wrapper.
@spec num_tuples(t()) :: {:ok, non_neg_integer()} | {:error, term()}
Returns native egglog's tuple count for the current mutable session.
This wraps Rust egglog's EGraph::num_tuples(). It is a coarse
database-size counter, not a graph summary. For node/class counts,
visualization, or JSON inspection, use snapshot/2.
@spec parse(String.t()) :: {:ok, Egglog.Commands.t()} | {:error, term()}
Parses egglog source into a reusable command handle.
@spec parse!(String.t()) :: Egglog.Commands.t()
Bang variant of parse/1.
@spec pop(t(), pos_integer()) :: :ok | {:error, term()}
Pops native egglog rollback points.
count defaults to 1. Popping too much returns an error from native egglog.
@spec push(t(), pos_integer()) :: :ok | {:error, term()}
Pushes a native egglog rollback point.
count defaults to 1. This corresponds to (push) for 1, or
(push count) for larger counts.
Runs egglog code against the mutable session.
On success, native egglog state is retained for future calls. On error,
egglog may have applied commands that appeared before the failing command in
the same program; use push/2 and pop/2 around speculative work when you
need an explicit rollback point.
input may be:
- a raw egglog source string,
- a list of raw egglog command strings, or
- a parsed
%Egglog.Commands{}handle returned byparse/1.
Supported options:
:output_limit- maximum number of native command outputs to keep in the returned:outputslist. Defaults to unset, meaning all outputs are kept. Values must be positive integers; invalid values are ignored. When outputs are truncated, the returned result hasstatus: :limit.:snapshot- request a native snapshot after the input has run. Defaults to:none.Accepted values:
:none,"none",false, ornil- do not include a snapshot.:dot,"dot", ortrue- include native Graphviz DOT text.:jsonor"json"- include native JSON snapshot text.
run/3does not render DOT to SVG. Usesnapshot/2when you wantrender: :autoorrender: :svgpost-processing.:snapshot_max_functions- value passed to native egglog'sSerializeConfig.max_functionswhen:snapshotis enabled. Defaults to0. Values greater than0becomeSome(value);0and invalid values becomeNone.:snapshot_max_calls_per_function- value passed to native egglog'sSerializeConfig.max_calls_per_functionwhen:snapshotis enabled. Defaults to0. Values greater than0becomeSome(value);0and invalid values becomeNone.:snapshot_inline_leaves- number of native leaf-inlining passes to use on the serialized snapshot before producing DOT or JSON text. Defaults to0. Negative or invalid values behave like0.:snapshot_split_primitive_outputs- whether the native serialized snapshot should split primitive outputs into separate classes before producing DOT or JSON text. Defaults tofalse.
The returned result map contains:
:status- native run status, or:limitwhen:output_limittruncated outputs.:outputs- list of%{type: atom(), text: String.t()}command outputs.:stats- compact numeric/text stats from native egglog.:report- per-rule/per-ruleset reporting data from native egglog.:snapshot- present only when:snapshotis:dotor:json; contains%{format: :dot | :json, text: binary(), omitted: term()}.
String option keys are also accepted, matching the rest of the wrapper.
@spec run!(t(), input(), keyword() | map()) :: run_result()
Bang variant of run/3.
Runs egglog code against the mutable session.
On success, native egglog state is retained for future calls. On error,
egglog may have applied commands that appeared before the failing command in
the same program; use push/2 and pop/2 around speculative work when you
need an explicit rollback point.
input may be:
- a raw egglog source string,
- a list of raw egglog command strings, or
- a parsed
%Egglog.Commands{}handle returned byparse/1.
Supported options:
:output_limit- maximum number of native command outputs to keep in the returned:outputslist. Defaults to unset, meaning all outputs are kept. Values must be positive integers; invalid values are ignored. When outputs are truncated, the returned result hasstatus: :limit.:snapshot- request a native snapshot after the input has run. Defaults to:none.Accepted values:
:none,"none",false, ornil- do not include a snapshot.:dot,"dot", ortrue- include native Graphviz DOT text.:jsonor"json"- include native JSON snapshot text.
run/3does not render DOT to SVG. Usesnapshot/2when you wantrender: :autoorrender: :svgpost-processing.:snapshot_max_functions- value passed to native egglog'sSerializeConfig.max_functionswhen:snapshotis enabled. Defaults to0. Values greater than0becomeSome(value);0and invalid values becomeNone.:snapshot_max_calls_per_function- value passed to native egglog'sSerializeConfig.max_calls_per_functionwhen:snapshotis enabled. Defaults to0. Values greater than0becomeSome(value);0and invalid values becomeNone.:snapshot_inline_leaves- number of native leaf-inlining passes to use on the serialized snapshot before producing DOT or JSON text. Defaults to0. Negative or invalid values behave like0.:snapshot_split_primitive_outputs- whether the native serialized snapshot should split primitive outputs into separate classes before producing DOT or JSON text. Defaults tofalse.
The returned result map contains:
:status- native run status, or:limitwhen:output_limittruncated outputs.:outputs- list of%{type: atom(), text: String.t()}command outputs.:stats- compact numeric/text stats from native egglog.:report- per-rule/per-ruleset reporting data from native egglog.:snapshot- present only when:snapshotis:dotor:json; contains%{format: :dot | :json, text: binary(), omitted: term()}.
String option keys are also accepted, matching the rest of the wrapper.
Raises if native egglog rejects the input or any option validation raises.
Builds a snapshot of the current mutable e-graph.
This runs no new egglog commands; it asks native egglog to serialize the session's current state, then optionally renders or writes that serialized graph.
Supported options:
:render- output format to return. Defaults to:auto.Accepted values:
:auto,"auto", ortrue- render SVG with Graphviz when thedotexecutable is available onPATH; otherwise return DOT text.:svgor"svg"- require Graphviz SVG rendering. Returns{:error, {:graphviz_missing, message}}ifdotis unavailable.:dot,"dot",false, ornil- return Graphviz DOT text.:jsonor"json"- return egglog's native JSON snapshot.
:snapshot_max_functions- value passed to native egglog'sSerializeConfig.max_functions. Defaults to0. Values greater than0becomeSome(value);0and invalid values becomeNone.:snapshot_max_calls_per_function- value passed to native egglog'sSerializeConfig.max_calls_per_function. Defaults to0. Values greater than0becomeSome(value);0and invalid values becomeNone.:snapshot_inline_leaves- number of native leaf-inlining passes to use on the serialized snapshot before producing DOT or JSON text. Defaults to0. Negative or invalid values behave like0.:snapshot_split_primitive_outputs- whether the native serialized snapshot should split primitive outputs into separate classes before producing DOT or JSON text. Defaults tofalse.:path- optional file path where the returned:textpayload is written. Defaults to unset.:dot_path- optional file path where DOT is written when DOT text is available. Defaults to unset. Forrender: :json, this key is recorded asnilif provided because no DOT text is produced.:svg_path- optional file path where SVG is written when SVG rendering succeeds. Defaults to unset. For non-SVG renders, this key is recorded asnilif provided.:json_path- optional file path where JSON is written whenrender: :jsonis used. Defaults to unset. For non-JSON renders, this key is recorded asnilif provided.
The returned snapshot map contains:
:format- one of:dot,:svg, or:json.:text- the primary payload for the selected render.:dot,:svg, or:json- the format-specific payload when present.:omitted- native omission metadata for capped snapshots.:stats- snapshot-specific native stats, currently:snapshot_nodesand:snapshot_classeswhen available.:result- the underlyingrun/3result used to obtain the native serialization.- any provided artifact path keys after successful writes.
String option keys are also accepted, matching the rest of the wrapper.
Bang variant of snapshot/2.
Builds a snapshot of the current mutable e-graph.
This runs no new egglog commands; it asks native egglog to serialize the session's current state, then optionally renders or writes that serialized graph.
Supported options:
:render- output format to return. Defaults to:auto.Accepted values:
:auto,"auto", ortrue- render SVG with Graphviz when thedotexecutable is available onPATH; otherwise return DOT text.:svgor"svg"- require Graphviz SVG rendering. Returns{:error, {:graphviz_missing, message}}ifdotis unavailable.:dot,"dot",false, ornil- return Graphviz DOT text.:jsonor"json"- return egglog's native JSON snapshot.
:snapshot_max_functions- value passed to native egglog'sSerializeConfig.max_functions. Defaults to0. Values greater than0becomeSome(value);0and invalid values becomeNone.:snapshot_max_calls_per_function- value passed to native egglog'sSerializeConfig.max_calls_per_function. Defaults to0. Values greater than0becomeSome(value);0and invalid values becomeNone.:snapshot_inline_leaves- number of native leaf-inlining passes to use on the serialized snapshot before producing DOT or JSON text. Defaults to0. Negative or invalid values behave like0.:snapshot_split_primitive_outputs- whether the native serialized snapshot should split primitive outputs into separate classes before producing DOT or JSON text. Defaults tofalse.:path- optional file path where the returned:textpayload is written. Defaults to unset.:dot_path- optional file path where DOT is written when DOT text is available. Defaults to unset. Forrender: :json, this key is recorded asnilif provided because no DOT text is produced.:svg_path- optional file path where SVG is written when SVG rendering succeeds. Defaults to unset. For non-SVG renders, this key is recorded asnilif provided.:json_path- optional file path where JSON is written whenrender: :jsonis used. Defaults to unset. For non-JSON renders, this key is recorded asnilif provided.
The returned snapshot map contains:
:format- one of:dot,:svg, or:json.:text- the primary payload for the selected render.:dot,:svg, or:json- the format-specific payload when present.:omitted- native omission metadata for capped snapshots.:stats- snapshot-specific native stats, currently:snapshot_nodesand:snapshot_classeswhen available.:result- the underlyingrun/3result used to obtain the native serialization.- any provided artifact path keys after successful writes.
String option keys are also accepted, matching the rest of the wrapper.
Raises if snapshot serialization, Graphviz rendering, or artifact writing fails.