Magic-sets program transformation for demand-driven (goal-directed) evaluation.
Magic sets rewrites a program so that bottom-up semi-naive evaluation computes
only the facts relevant to a query goal, instead of the full least fixpoint.
It is a program transformation: the engine is unchanged. Given a goal
{relation, pattern}, the transformation:
- computes the goal's adornment (which argument positions are bound),
- generates
magic_<relation>_<adornment>predicates capturing demand, - rewrites recursive rules to consume the magic predicates,
- seeds the magic predicate with the bound goal constants.
The transformed IR is then evaluated by the existing semi-naive engine.
Scope (v0.5.0, experimental)
- Positive recursive programs only.
- A single goal.
- Ground (constant) bound positions.
Programs outside this scope fall back to full semi-naive evaluation
({:fallback, reason}), never producing incorrect results.
Summary
Functions
Transforms an IR program for goal-directed evaluation.
Functions
@spec transform( ExDatalog.IR.t(), {String.t(), [term()]} ) :: {:ok, ExDatalog.IR.t()} | {:fallback, term()}
Transforms an IR program for goal-directed evaluation.
Returns {:ok, transformed_ir} when the magic-sets transformation applies,
or {:fallback, reason} when the program is outside the supported scope (the
caller should evaluate the original IR with semi-naive instead).