datalog: magic-sets rewriter (Phase 6, 202/202)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 27s

dl-magic-rewrite rules query-rel adn args returns:
  {:rules <rewritten-rules> :seed <magic-seed-fact>}

Worklist over (rel, adn) pairs starts from the query and stops
when no new pairs appear. For each rule with head matching a
worklist pair:
  - Adorned rule: head :- magic_<rel>^<adn>(bound), body...
  - Propagation rules: for each positive non-builtin body lit
    at position i:
      magic_<lit-rel>^<lit-adn>(bound-of-lit) :-
        magic_<rel>^<adn>(bound-of-head),
        body[0..i-1]
  - Add (lit-rel, lit-adn) to the worklist.

Built-ins, negation, and aggregates pass through without
generating propagation rules. EDB facts are unchanged.

3 new tests cover seed structure, equivalence on chain-3 (full
closure, 6 ancestor tuples — magic helps only when the EDB has
nodes outside the seed's transitive cone), and same-query-answers
under the rewritten program. Total 202/202.

Wiring up a `dl-saturate-magic!` driver and large-graph perf
benchmarks is left for a future iteration.
This commit is contained in:
2026-05-08 09:58:36 +00:00
parent 2a01d8ac91
commit a080ce656c
5 changed files with 245 additions and 14 deletions

View File

@@ -154,10 +154,13 @@ large graphs.
- [x] Adornments: `dl-adorn-goal goal` and `dl-adorn-lit lit bound` in
`lib/datalog/magic.sx`. Per-arg `b`/`f` based on whether the arg
is a constant or a variable already in the bound set.
- [ ] Magic transformation: for each adorned predicate, generate a
`magic_<pred>` relation and rewrite rule bodies to filter through it.
*Building blocks present in `magic.sx`: `dl-magic-rel-name`,
`dl-magic-lit`, `dl-bound-args`. Full rewriter still TODO.*
- [x] Magic transformation: `dl-magic-rewrite rules query-rel adn args`
generates `{:rules <rewritten-rules> :seed <magic-seed>}`. Each
original rule is gated with a `magic_<rel>^<adn>(bound)` filter,
and propagation rules are emitted for each positive non-builtin
body literal. Worklist over `(rel, adn)` pairs starts from the
query and stops when no new pairs appear. EDB facts pass through
unchanged.
- [x] Sideways information passing strategy (SIPS): left-to-right
`dl-rule-sips rule head-adornment` walks body literals tracking
the bound set, returning `({:lit :adornment} ...)`. Recognises
@@ -168,9 +171,14 @@ large graphs.
`:semi-naive`. `:magic` accepted but the transformation itself is
deferred — saturator currently falls back to semi-naive. Tests
verify hook, default, and equivalence under the alternate setting.
- [ ] Tests: equivalence vs naive on small inputs; perf win on a 10k-node
reachability query from a single root. *Pending real magic-set
transformation.*
- [x] Equivalence test: rewritten ancestor program over the same EDB
derives the same number of `ancestor` tuples and returns the
same query answers as the unrewritten program (chain-3 case).
- [ ] Perf test: 10k-node reachability with magic vs semi-naive.
Pending — would need a `dl-saturate-magic!` driver that builds
a temporary db from rewrite output. The rewriter itself is in
place; benchmarking against semi-naive on large graphs is left
to a future iteration.
### Phase 7 — stratified negation
- [x] Dependency graph: `dl-build-dep-graph db` returns `{head -> ({:rel
@@ -291,6 +299,17 @@ large graphs.
_Newest first._
- 2026-05-08 — Phase 6 magic-sets rewriter. `dl-magic-rewrite rules
query-rel adn args` returns `{:rules <rewritten> :seed <seed-fact>}`.
Worklist over `(rel, adn)` pairs starts from the query, gates each
original rule with a `magic_<rel>^<adn>(bound)` filter, and emits
propagation rules for each positive non-builtin body literal so
that magic spreads to body relations. EDB facts pass through.
3 new tests cover seed structure, equivalence on chain-3 by
ancestor-relation tuple count, and same-query-answers under
the rewritten program. The plumbing for a `dl-saturate-magic!`
driver and large-graph perf benchmarks is still future work.
- 2026-05-08 — Phase 6 building blocks for the magic-sets
transformation: `dl-magic-rel-name`, `dl-magic-lit`,
`dl-bound-args`. The rewriter that generates magic seed and