Egglog. Program
(EgglogElixir v0.1.0)
Copy Markdown
Loaded egglog program used for query-local runs.
A Program is a plain handle to a loaded native egglog base e-graph
containing static declarations, rulesets, rules, and any persistent theory
facts. Each run/3 call clones that base inside native egglog and executes
the supplied input as query-local work.
Concurrent calls against the same Program are safe and query-local, but
they are serialized by that native resource. This protects the loaded base
and the clone/run operation for one handle without introducing a process-wide
lock or an Elixir worker pool. Load multiple Program handles explicitly
when you need parallel throughput.
Summary
Functions
Runs a native egglog check and returns {:ok, true} or {:ok, false}.
Boolean variant of check/4.
Asserts that a native egglog check fails.
Closes the native loaded-program resource.
Evaluates an egglog expression after applying query-local input.
Bang variant of eval/4.
Extracts the cheapest native egglog term for expr.
Bang variant of extract/4.
Loads a static egglog program into a reusable native handle.
Bang variant of load/2.
Looks up a function row after evaluating the given argument expressions.
Bang variant of lookup/5.
Returns native egglog's tuple count for the loaded base program.
Parses egglog source into a reusable command handle.
Bang variant of parse/1.
Runs query-local egglog code against the loaded program.
Bang variant of run/3.
Builds a query-local e-graph snapshot.
Bang variant of snapshot/3.
Types
@type input() :: %{ optional(:source) => String.t(), optional(:program) => String.t(), optional(:commands) => [String.t()], optional(:facts) => [String.t()], optional(:terms) => %{required(String.t()) => String.t()}, optional(:sets) => [{String.t(), String.t()}], optional(:unions) => [{String.t(), String.t()}], optional(:requests) => [request()], optional(:schedule) => String.t(), optional(:snapshot) => :dot | :json | :none | String.t() | boolean(), optional(:snapshot_max_functions) => non_neg_integer(), optional(:snapshot_max_calls_per_function) => non_neg_integer(), optional(:snapshot_inline_leaves) => non_neg_integer(), optional(:snapshot_split_primitive_outputs) => boolean(), optional(:mode) => atom() | String.t(), optional(:budget) => map() } | %{optional(String.t()) => term()} | Egglog.Commands.t()
Query-local input for run/3, check/4, extract/4, eval/4, and
lookup/5.
Strings are passed as raw egglog source. Lists are joined as egglog commands.
Parsed Egglog.Commands handles can be reused when the same source is run or
inspected repeatedly. Maps let Elixir code build common egglog commands
without string concatenation.
@type request() :: String.t() | %{type: :check, expr: String.t()} | %{ :type => :extract, :expr => String.t(), optional(:variants) => pos_integer(), optional(:limit) => pos_integer() } | %{:type => :print_size, optional(:name) => String.t()} | %{ :type => :print_function, :name => String.t(), optional(:limit) => pos_integer() } | %{optional(String.t()) => term()}
One output-producing egglog request.
A request may be written directly as an egglog command string, or as a small Elixir map that the wrapper renders to native egglog syntax.
@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/4 and lookup/5.
: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 and returns {:ok, true} or {:ok, false}.
Supported options are the same as run/3; they are passed through to the
internal run after appending (check fact) to the input requests. Defaults
are therefore the run/3 defaults.
Only a failed egglog check is converted to false; parse/type/native errors
are returned as {:error, reason}. Snapshot and output options may affect
the internal run, but their result data is not exposed by this function.
Boolean variant of check/4.
Runs a native egglog check and returns {:ok, true} or {:ok, false}.
Supported options are the same as run/3; they are passed through to the
internal run after appending (check fact) to the input requests. Defaults
are therefore the run/3 defaults.
Only a failed egglog check is converted to false; parse/type/native errors
are returned as {:error, reason}. Snapshot and output options may affect
the internal run, but their result data is not exposed by this function.
It raises on non-check errors so syntax/type mistakes are not silently treated as false mathematical assertions.
Asserts that a native egglog check fails.
This maps to egglog's (fail (check ...)) form and is useful for tests and
tutorials that want to show a negative example without surfacing an exception.
Supported options are the same as run/3; they are passed through to the
internal run after appending (fail (check fact)) to the input requests.
Defaults are therefore the run/3 defaults. Only :ok or {:error, reason}
is returned.
Closes the native loaded-program resource.
Evaluates an egglog expression after applying query-local input.
Primitive values are decoded into Elixir values. Non-primitive e-class values
are returned with type: :value.
Supported options are the query-construction subset of run/3 options:
:mode- selected mode when no input:scheduleis supplied. Defaults to input:mode, then:default.:budget- map merged after program default budget and input:budget. Defaults to%{}. Currently:rounds/"rounds"can emit a(run n)command when no selected mode schedule exists.
:output_limit and snapshot options are not used by this function because it
does not return command outputs or snapshots.
String option keys are also accepted, matching the rest of the wrapper.
Bang variant of eval/4.
Evaluates an egglog expression after applying query-local input.
Supported options are the query-construction subset of run/3 options:
:mode- selected mode when no input:scheduleis supplied. Defaults to input:mode, then:default.:budget- map merged after program default budget and input:budget. Defaults to%{}. Currently:rounds/"rounds"can emit a(run n)command when no selected mode schedule exists.
:output_limit and snapshot options are not used by this function because it
does not return command outputs or snapshots.
String option keys are also accepted, matching the rest of the wrapper.
Raises if native egglog rejects the query-local input or expression.
Extracts the cheapest native egglog term for expr.
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 after appending the
extract request to the input. Defaults are the run/3 defaults. Snapshot and
output options may affect the internal run, but extract/4 returns only the
extracted text.
String option keys are also accepted, matching the rest of the wrapper.
Bang variant of extract/4.
Extracts the cheapest native egglog term for expr.
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 after appending the
extract request to the input. Defaults are the run/3 defaults. Snapshot and
output options may affect the internal run, but extract/4 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.
Loads a static egglog program into a reusable native handle.
The theory may be a raw egglog source string or a map containing :source or
:program.
Supported options:
:name- human-readable program 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.:modes- map from mode names to egglog schedule forms or commands. Defaults to%{}. Duringrun/3, the selected:modeis looked up by the mode value and byto_string(mode).:default_budget- map merged into each non-parsedrun/3input budget. Defaults to%{}. Currently:rounds/"rounds"and:output_limit/"output_limit"are read byrun/3.
String option keys are also accepted, matching the rest of the wrapper.
Bang variant of load/2.
Loads a static egglog program into a reusable native handle.
The theory may be a raw egglog source string or a map containing :source or
:program.
Supported options:
:name- human-readable program 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.:modes- map from mode names to egglog schedule forms or commands. Defaults to%{}. Duringrun/3, the selected:modeis looked up by the mode value and byto_string(mode).:default_budget- map merged into each non-parsedrun/3input budget. Defaults to%{}. Currently:rounds/"rounds"and:output_limit/"output_limit"are read byrun/3.
String option keys are also accepted, matching the rest of the wrapper.
Raises if native egglog rejects the loaded theory.
@spec lookup(t(), input(), String.t(), [String.t()], keyword() | map()) :: {:ok, value() | nil} | {:error, term()}
Looks up a function row after evaluating the given argument expressions.
Returns {:ok, nil} when no row exists for the evaluated key.
Supported options are the query-construction subset of run/3 options:
:mode- selected mode when no input:scheduleis supplied. Defaults to input:mode, then:default.:budget- map merged after program default budget and input:budget. Defaults to%{}. Currently:rounds/"rounds"can emit a(run n)command when no selected mode schedule exists.
:output_limit and snapshot options are not used by this function because it
does not return command outputs or snapshots.
String option keys are also accepted, matching the rest of the wrapper.
Bang variant of lookup/5.
Looks up a function row after evaluating the given argument expressions.
Supported options are the query-construction subset of run/3 options:
:mode- selected mode when no input:scheduleis supplied. Defaults to input:mode, then:default.:budget- map merged after program default budget and input:budget. Defaults to%{}. Currently:rounds/"rounds"can emit a(run n)command when no selected mode schedule exists.
:output_limit and snapshot options are not used by this function because it
does not return command outputs or snapshots.
String option keys are also accepted, matching the rest of the wrapper.
Raises if native egglog rejects the query-local input or lookup expression.
@spec num_tuples(t()) :: {:ok, non_neg_integer()} | {:error, term()}
Returns native egglog's tuple count for the loaded base program.
This wraps Rust egglog's EGraph::num_tuples() for the reusable base program.
Query-local facts inserted by run/3 do not mutate this base. For
query-local node/class counts, visualization, or JSON inspection, use
snapshot/3 on the query you want to inspect.
@spec parse(String.t()) :: {:ok, Egglog.Commands.t()} | {:error, term()}
Parses egglog source into a reusable command handle.
Parsed commands can be passed to run/3. This is useful when the same command
sequence is executed repeatedly; native egglog parsing happens once, while
execution still happens against a fresh clone of the loaded base program.
@spec parse!(String.t()) :: Egglog.Commands.t()
Bang variant of parse/1.
Runs query-local egglog code against the loaded program.
The loaded base program is cloned inside native egglog for each run. The clone receives query-local input and the loaded base is not mutated.
input may be:
- a raw egglog source string,
- a list of raw egglog command strings,
- a parsed
%Egglog.Commands{}handle returned byparse/1, or - a map with structured fields rendered to egglog source.
Structured input map fields:
:sourceor:program- raw egglog source emitted first. Defaults to unset.:commands- list of raw egglog commands emitted after source. Defaults to[].:terms- map rendered as(let name expr)commands. Defaults to%{}.:facts- list of raw fact/command strings. Defaults to[].:sets- list of{call, value}tuples rendered as(set call value). Defaults to[].:unions- list of{left, right}tuples rendered as(union left right). Defaults to[].:requests- list of output-producing requests emitted last. Defaults to[]. Supported request maps are:check,:extract,:print_size, and:print_function; raw request strings are also accepted.:schedule- egglog schedule string. Defaults to unset. Blank strings emit no schedule; strings starting with(runare emitted unchanged; other nonblank strings are wrapped as(run-schedule schedule).:mode- mode name used when no:scheduleis supplied. Defaults to:default.:budget- map merged after the program default budget and before theopts[:budget]map. Defaults to%{}.- snapshot fields listed below may also be supplied in the input map; opts take precedence over input map snapshot fields.
Supported options:
:mode- selected mode when no input:scheduleis supplied. Defaults to input:mode, then:default. The selected mode is looked up in the program's:modesmap before:budgetrounds are considered.:budget- map merged afterprogram.default_budgetand input:budget. Defaults to%{}. Currently:rounds/"rounds"emits(run rounds)when positive and no selected mode schedule exists.:output_limit/"output_limit"is used as the default output limit for non-parsed inputs.:output_limit- maximum number of native command outputs to keep in the returned:outputslist. Defaults tobudget[:output_limit]for non-parsed inputs and unset for parsed command handles. 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 input:snapshot, then: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/3when you wantrender: :autoorrender: :svgpost-processing.:snapshot_max_functions- value passed to native egglog'sSerializeConfig.max_functionswhen:snapshotis enabled. Defaults to input:snapshot_max_functions, then0. 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 to input:snapshot_max_calls_per_function, then0. 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 to input:snapshot_inline_leaves, then0. 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 to input:snapshot_split_primitive_outputs, thenfalse.
For parsed %Egglog.Commands{} input, :output_limit and the snapshot
options above are used. :mode is passed through to native result metadata,
but no mode schedule is injected because the command sequence is already
parsed. Structured input fields and budget-derived schedules are not rebuilt.
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 query-local egglog code against the loaded program.
The loaded base program is cloned inside native egglog for each run. The clone receives query-local input and the loaded base is not mutated.
input may be:
- a raw egglog source string,
- a list of raw egglog command strings,
- a parsed
%Egglog.Commands{}handle returned byparse/1, or - a map with structured fields rendered to egglog source.
Structured input map fields:
:sourceor:program- raw egglog source emitted first. Defaults to unset.:commands- list of raw egglog commands emitted after source. Defaults to[].:terms- map rendered as(let name expr)commands. Defaults to%{}.:facts- list of raw fact/command strings. Defaults to[].:sets- list of{call, value}tuples rendered as(set call value). Defaults to[].:unions- list of{left, right}tuples rendered as(union left right). Defaults to[].:requests- list of output-producing requests emitted last. Defaults to[]. Supported request maps are:check,:extract,:print_size, and:print_function; raw request strings are also accepted.:schedule- egglog schedule string. Defaults to unset. Blank strings emit no schedule; strings starting with(runare emitted unchanged; other nonblank strings are wrapped as(run-schedule schedule).:mode- mode name used when no:scheduleis supplied. Defaults to:default.:budget- map merged after the program default budget and before theopts[:budget]map. Defaults to%{}.- snapshot fields listed below may also be supplied in the input map; opts take precedence over input map snapshot fields.
Supported options:
:mode- selected mode when no input:scheduleis supplied. Defaults to input:mode, then:default. The selected mode is looked up in the program's:modesmap before:budgetrounds are considered.:budget- map merged afterprogram.default_budgetand input:budget. Defaults to%{}. Currently:rounds/"rounds"emits(run rounds)when positive and no selected mode schedule exists.:output_limit/"output_limit"is used as the default output limit for non-parsed inputs.:output_limit- maximum number of native command outputs to keep in the returned:outputslist. Defaults tobudget[:output_limit]for non-parsed inputs and unset for parsed command handles. 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 input:snapshot, then: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/3when you wantrender: :autoorrender: :svgpost-processing.:snapshot_max_functions- value passed to native egglog'sSerializeConfig.max_functionswhen:snapshotis enabled. Defaults to input:snapshot_max_functions, then0. 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 to input:snapshot_max_calls_per_function, then0. 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 to input:snapshot_inline_leaves, then0. 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 to input:snapshot_split_primitive_outputs, thenfalse.
For parsed %Egglog.Commands{} input, :output_limit and the snapshot
options above are used. :mode is passed through to native result metadata,
but no mode schedule is injected because the command sequence is already
parsed. Structured input fields and budget-derived schedules are not rebuilt.
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 query-local e-graph snapshot.
This is a convenience wrapper around run/3: it runs the query-local input
against a native clone of the loaded program, requests a native DOT or JSON
snapshot, then optionally renders or writes the 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.
Internally,
:renderdetermines the native:snapshotformat passed torun/3::jsonrequests JSON; every other render value requests DOT.:mode- selected mode when no input:scheduleis supplied. Defaults to input:mode, then:default.:budget- map merged after program default budget and input:budget. Defaults to%{}. Currently:rounds/"rounds"can emit a(run n)command when no selected mode schedule exists.:output_limit- passed to the underlyingrun/3; it can truncatesnapshot.result.outputs. Defaults tobudget[:output_limit]for non-parsed inputs and unset for parsed command handles.:snapshot_max_functions- value passed to native egglog'sSerializeConfig.max_functions. Defaults to input:snapshot_max_functions, then0. Values greater than0becomeSome(value);0and invalid values becomeNone.:snapshot_max_calls_per_function- value passed to native egglog'sSerializeConfig.max_calls_per_function. Defaults to input:snapshot_max_calls_per_function, then0. 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 to input:snapshot_inline_leaves, then0. 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 to input:snapshot_split_primitive_outputs, thenfalse.: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/3.
Builds a query-local e-graph snapshot.
This is a convenience wrapper around run/3: it runs the query-local input
against a native clone of the loaded program, requests a native DOT or JSON
snapshot, then optionally renders or writes the 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.
Internally,
:renderdetermines the native:snapshotformat passed torun/3::jsonrequests JSON; every other render value requests DOT.:mode- selected mode when no input:scheduleis supplied. Defaults to input:mode, then:default.:budget- map merged after program default budget and input:budget. Defaults to%{}. Currently:rounds/"rounds"can emit a(run n)command when no selected mode schedule exists.:output_limit- passed to the underlyingrun/3; it can truncatesnapshot.result.outputs. Defaults tobudget[:output_limit]for non-parsed inputs and unset for parsed command handles.:snapshot_max_functions- value passed to native egglog'sSerializeConfig.max_functions. Defaults to input:snapshot_max_functions, then0. Values greater than0becomeSome(value);0and invalid values becomeNone.:snapshot_max_calls_per_function- value passed to native egglog'sSerializeConfig.max_calls_per_function. Defaults to input:snapshot_max_calls_per_function, then0. 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 to input:snapshot_inline_leaves, then0. 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 to input:snapshot_split_primitive_outputs, thenfalse.: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 native egglog rejects the input, Graphviz rendering fails, or artifact writing fails.