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

@@ -159,6 +159,35 @@
(dl-query db (quote (ancestor tom X)))))
(list {:X (quote bob)}))
;; dl-retract! on a relation with BOTH explicit facts AND a rule
;; (a "mixed" relation) used to wipe the EDB portion when the IDB
;; was re-derived, even when the retract didn't match anything.
;; :edb-keys provenance now preserves user-asserted facts.
(dl-api-test-set! "dl-retract! preserves EDB in mixed relation"
(let
((db (dl-program-data
(quote ((p a) (p b) (q c)))
(quote ((p X <- (q X)))))))
(do
(dl-saturate! db)
;; Retract a non-existent tuple — should be a no-op.
(dl-retract! db (quote (p z)))
(dl-query db (quote (p X)))))
(list {:X (quote a)} {:X (quote b)} {:X (quote c)}))
;; And retracting an actual EDB fact in a mixed relation drops
;; only that fact; the derived portion stays.
(dl-api-test-set! "dl-retract! mixed: drop EDB, keep IDB"
(let
((db (dl-program-data
(quote ((p a) (p b) (q c)))
(quote ((p X <- (q X)))))))
(do
(dl-saturate! db)
(dl-retract! db (quote (p a)))
(dl-query db (quote (p X)))))
(list {:X (quote b)} {:X (quote c)}))
;; dl-program-data + dl-query with constants in head.
(dl-api-test-set! "constant-in-head data"
(dl-query