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.
This commit is contained in:
2026-05-09 13:11:47 +00:00
parent a4ef271459
commit d437727f1d
3 changed files with 34 additions and 6 deletions

View File

@@ -242,6 +242,34 @@
(= (len semi) (len magic))))
true)
;; 3-stratum program under magic — distinct rule heads at
;; strata 0/1/2 must all rewrite via the worklist.
(dl-mt-test! "magic 3-stratum program"
(let
((db (dl-program
"a(1). a(2). a(3). b(2).
c(X) :- a(X), not(b(X)).
d(X) :- c(X), not(banned(X)).
banned(3).")))
(let
((semi (dl-query db (list (quote d) (quote X))))
(magic (dl-magic-query db (list (quote d) (quote X)))))
(= (len semi) (len magic))))
true)
;; Aggregate -> derived -> threshold chain via magic.
(dl-mt-test! "magic aggregate-derived chain"
(let
((db (dl-program
"src(1). src(2). src(3).
cnt(N) :- count(N, X, src(X)).
active(N) :- cnt(N), >=(N, 2).")))
(let
((semi (dl-query db (list (quote active) (quote N))))
(magic (dl-magic-query db (list (quote active) (quote N)))))
(= (len semi) (len magic))))
true)
;; Multi-relation rewrite chain: query r4 → propagate to r3,
;; r2, r1, a. The worklist must process all of them; an
;; earlier bug stopped after only the head pair.