Commit Graph

18 Commits

Author SHA1 Message Date
82dfa20e82 datalog: dl-magic-query pre-saturates for aggregate correctness
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 13s
dl-magic-query could silently diverge from dl-query when an
aggregate's inner-goal relation was IDB. The rewriter passes
aggregate body lits through unchanged (no magic propagation
generated for them), so the inner relation was empty in the magic
db and the aggregate returned 0. Repro:

  (dl-eval-magic
    "u(a). u(b). u(c). u(d). banned(b). banned(d).
     active(X) :- u(X), not(banned(X)).
     n(N) :- count(N, X, active(X))."
    "?- n(N).")
  => ({:N 0})   ; should be ({:N 2})

dl-magic-query now pre-saturates the source db before copying facts
into the magic db. This guarantees equivalence with dl-query for
every stratified program; the magic benefit still comes from
goal-directed re-derivation of the query relation under the seed
(which matters for large recursive joins). The existing test cases
happened to dodge this because their aggregate inner-goals were all
EDB.

1 new regression test; conformance 274/274.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-11 08:59:28 +00:00
69a53ece43 datalog: dl-magic-query shape validator (255/255)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 44s
Bug: dl-magic-query crashed with cryptic "rest: 1 list arg" when
the goal argument was a string, number, or arbitrary dict. The
first thing the function does is dl-rel-name + dl-adorn-goal,
both of which assume a positive-literal list shape.

Fix: explicit shape check up front. A goal must be a non-empty
list whose first element is a symbol. Otherwise raise with a
clear diagnostic. Built-in / aggregate / negation dispatch (the
fall-back to dl-query) is unchanged.

2 new magic tests cover string and bare-dict goal rejection.
2026-05-10 21:13:30 +00:00
d437727f1d datalog: magic regression tests from bug-hunt round (242/242)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 48s
Bug-hunt round probed magic-sets against many edge cases. No new
bugs surfaced. Added regression tests for two patterns that
exercise the worklist post-fix:

- 3-stratum program (a → c via not-b → d via not-banned).
  Distinct rule heads at three strata; magic must rewrite each.
- Aggregate-derived chain (count(src) → cnt → active threshold).
  Magic correctly handles multi-step aggregate dependencies.

Magic-sets is robust against: 3-stratum negation, aggregate
chains, mutual recursion, all-bound goals, multi-arity rules,
diagonal queries, EDB-only goals, and rules whose body has
identical positive lits.
2026-05-09 13:11:47 +00:00
a4ef271459 datalog: cousin (multi-adornment same-relation) magic test (240/240)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 54s
2026-05-08 23:00:22 +00:00
17d6f58cc5 datalog: dl-magic-rewrite worklist now drains across rule chains (239/239)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Has been cancelled
Real bug: the worklist used (set! queue (rest queue)) to pop the
head, which left queue bound to a fresh empty list as soon as the
last item was popped. Subsequent (append! queue ...) was a no-op
on the empty list — so when the head's rewrite generated new
(rel, adn) pairs to enqueue, they vanished. Multi-relation
programs (e.g. shortest -> path -> edge, or chained derived
relations) only had their head's rules rewritten; downstream
rules silently dropped.

Fix: use an index-based loop (idx 0 → len queue), with append!
adding to the same list. Items added after the current pointer
are picked up in subsequent iterations.

2 new regression tests:
- 4-level chain (a → r1 → r2 → r3 → r4) under magic returns 2
- shortest-path demo via magic equals dl-query (1 result)
2026-05-08 14:41:05 +00:00
e981368dcf datalog: magic ≡ semi on federation foaf demo (237/237)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 54s
2026-05-08 14:29:18 +00:00
8e508bc90f datalog: magic existence check (bb-adornment) regression test (228/228)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 54s
2026-05-08 12:35:33 +00:00
5f4defe99e datalog: magic over rule with negation regression test (227/227)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 42s
2026-05-08 12:31:50 +00:00
d20df7aa8c datalog: magic over rule with aggregate body literal (226/226)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 53s
2026-05-08 12:28:52 +00:00
96f66d3596 datalog: dl-magic-query handles mixed EDB+IDB relations (225/225)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 51s
Bug: dl-magic-query was skipping EDB facts for relations that had
rules ("rule-headed"). When a single relation has both EDB facts
and rules deriving more (mixed EDB+IDB), the rewritten run would
miss the EDB portion entirely, producing too few or zero results.

Fix: copy ALL existing facts to the internal mdb regardless of
whether the relation has rules. EDB-only relations bring their
tuples; mixed relations bring both EDB and any pre-saturated IDB
(which the rewritten rules would re-derive anyway).

1 new test: link relation seeded with 3 EDB tuples plus a
recursive rule via via/2. dl-magic-query rooted at `a` returns
2 results (a→b direct, a→c via via(a,e), link(e,c)).
2026-05-08 10:41:36 +00:00
a648247ae4 datalog: dl-magic-query falls back on built-in/agg/neg goals (222/222)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 25s
Bug: dl-magic-query was always trying to seed a magic_<rel>^<adn>
fact for the query goal. For aggregate goals like (count N X (p X))
this produced a non-ground "fact" (magic_count^... N X (p X)) and
dl-add-fact! correctly rejected it, surfacing as an error.

Fix: dl-magic-query now detects built-in / aggregate / negation
goals up front and dispatches to plain dl-query for those cases —
magic-sets only applies to positive non-builtin literals against
rule-defined relations. Other shapes don't benefit from the
rewrite anyway.

1 new test confirms (count N X (p X)) returns the expected
{:N 3} via dl-magic-query.
2026-05-08 10:32:01 +00:00
5a3db1a458 datalog: magic preserves arithmetic test (221/221)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 26s
2026-05-08 10:29:14 +00:00
43fa31375d datalog: magic-vs-semi work-shape test on chain-12 (209/209)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 24s
Demonstrates the practical effect of goal-directed evaluation:
chain of 12 nodes, semi-naive derives the full ancestor closure
(78 = 12·13/2 tuples), while a magic-rooted query at node 0
returns only its 12 descendants. Concrete check that magic
limits derivation to the query's transitive cone.
2026-05-08 10:13:13 +00:00
57a1dbb232 datalog: magic-sets benefit test on disjoint-cluster graph (205/205)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 24s
Two disjoint chains, query rooted in cluster 1. Semi-naive
derives the full closure over both clusters (6 ancestor tuples).
Magic-sets only seeds magic_ancestor^bf for cluster 1, so only
2 query-relevant tuples are returned (a→b, a→c). The test
asserts both numbers, demonstrating the actual perf-shape
benefit of goal-directed evaluation.
2026-05-08 10:03:04 +00:00
a53e47b415 datalog: dl-magic-query driver (204/204)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 22s
End-to-end magic-sets entry point. Given (db, query-goal):
  - copies the caller's EDB facts (relations not headed by any
    rule) into a fresh internal db
  - adds the magic seed fact
  - adds the rewritten rules
  - saturates and runs the query
  - returns the substitution list

Caller's db is untouched. Equivalent to dl-query for any
fully-stratifiable program; intended as a perf alternative on
goal-shaped queries against large recursive relations.

2 new tests: equivalence to dl-query on chain-3 ancestor, and
non-mutation of the caller's db (rules count unchanged).
2026-05-08 10:00:44 +00:00
a080ce656c 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.
2026-05-08 09:58:36 +00:00
2a01d8ac91 datalog: magic-sets building blocks (199/199)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 22s
Adds the primitives a future magic-sets rewriter will compose:

  dl-magic-rel-name rel adornment    → "magic_<rel>^<adornment>"
  dl-magic-lit rel adn bound-args    → magic literal as SX list
  dl-bound-args lit adornment        → bound-position arg values

Rewriter algorithm (worklist over (rel, adornment) pairs,
generating seed, propagation, and adorned-rule outputs) is still
TODO — these helpers are inspection-only for now.

4 new magic tests cover naming, lit construction, and bound-args
extraction (mixed/free).
2026-05-08 09:53:38 +00:00
71b73bd87e datalog: Phase 6 adornments + SIPS analysis (194/194)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 27s
New lib/datalog/magic.sx — first piece of magic-sets:

  dl-adorn-arg arg bound          → "b" or "f"
  dl-adorn-args args bound        → adornment string
  dl-adorn-goal goal              → adornment under empty bound set
  dl-adorn-lit lit bound          → adornment of any literal
  dl-vars-bound-by-lit lit bound  → free vars this lit will bind
  dl-init-head-bound head adn     → bound set seeded from head adornment
  dl-rule-sips rule head-adn      → ({:lit :adornment} ...) per body lit

SIPS walks left-to-right tracking the bound set; recognises `is` and
aggregate result-vars as new binders, lets comparisons and negation
pass through with computed adornments.

Inspection-only — saturator doesn't yet consume these. Lays
groundwork for a future magic-sets transformation.

10 new tests cover pure adornment, SIPS over a chain rule,
head-fully-bound rules, comparisons, and `is`. Total 194/194.
2026-05-08 09:51:05 +00:00