datalog: dl-retract! preserves EDB in mixed relations
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 27s

A "mixed" relation has both user-asserted facts AND rules with the
same head. Previously dl-retract! wiped every rule-head relation
wholesale before re-saturating — the saturator only re-derives the
IDB portion, so explicit EDB facts vanished even for a no-op retract
of a non-existent tuple. Repro:

  (let ((db (dl-program "p(a). p(b). p(X) :- q(X). q(c).")))
    (dl-retract! db (quote (p z)))
    (dl-query db (quote (p X))))

went from {a, b, c} to just {c}.

Fix: track :edb-keys provenance in the db.

  - dl-make-db now allocates an :edb-keys dict.
  - dl-add-fact! (public) marks (rel-key, tuple-key) in :edb-keys.
  - New internal dl-add-derived! does the append without marking.
  - Saturator (semi-naive + naive driver) now calls dl-add-derived!.
  - dl-retract! strips only the IDB-derived portion of rule-head
    relations (anything not in :edb-keys) and preserves the EDB
    portion through the re-saturate pass.

2 new regression tests; conformance 262/262.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-11 07:51:08 +00:00
parent 285cd530eb
commit c6f646607e
7 changed files with 184 additions and 40 deletions

View File

@@ -15,7 +15,7 @@ for rose-ash data (e.g. federation graph, content relationships).
## Status (rolling)
`bash lib/datalog/conformance.sh`**260/260 across 11 suites**
`bash lib/datalog/conformance.sh`**262/262 across 11 suites**
(tokenize, parse, unify, eval, builtins, semi_naive, negation, aggregates,
api, magic, demo). Source is ~3100 LOC, tests ~2900 LOC, public API
documented in `lib/datalog/datalog.sx`.
@@ -320,6 +320,22 @@ large graphs.
_Newest first._
- 2026-05-11 — `dl-retract!` was silently destroying EDB facts in
"mixed" relations (those with BOTH user-asserted facts AND a rule
defining the same head). The retract pass wiped every rule-head
relation wholesale and then re-saturated — but the saturator only
re-derives the IDB portion, so explicit EDB facts vanished even
for a no-op retract of a non-existent tuple. Probe:
`(let ((db (dl-program "p(a). p(b). p(X) :- q(X). q(c).")))
(dl-retract! db (quote (p z))) (dl-query db (quote (p X))))`
went from `{a,b,c}` to just `{c}`.
Fix: tracked `:edb-keys` provenance in the db. `dl-add-fact!` (public
API) marks the tuple as EDB; saturator calls new internal
`dl-add-derived!` which doesn't mark it. `dl-retract!` now strips
only the IDB-derived portion of rule-head relations and preserves
EDB-marked tuples through the re-saturate pass. 2 new regression
tests; 262/262.
- 2026-05-11 — Eval-semantics bug-hunt: nested `not(not(P))` was
silently misinterpreted. Outer-level `not(...)` is parsed as
negation, but the inner `not(banned(X))` was parsed as a regular