datalog: anonymous _ vars are unique per occurrence (Phase 5d, 156/156)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 54s

(p X _), (p _ Y) — the two _ are now different variables, matching
standard Datalog semantics. Previously both _ symbols were the same
SX symbol, so unification across them gave wrong answers.

Fix in db.sx: dl-rename-anon-term + dl-rename-anon-lit walk a term
or literal and replace each '_' symbol with a fresh _anon<N>.
dl-make-anon-renamer returns a counter-based name generator scoped
per call. dl-rename-anon-rule applies it to head and body of a
rule. dl-add-rule! invokes the renamer before safety check.

eval.sx: dl-query renames anon vars in the goal before search and
filters '_' out of the projection so user-facing results aren't
polluted with internal _anon<N> bindings.

The previous "underscore in head ok" test now correctly rejects
(p X _) :- q(X) as unsafe (the head's fresh anon var has no body
binder). New "underscore in body only" test confirms the safe
case. Two regression tests for rule-level and goal-level
independence.
This commit is contained in:
2026-05-08 08:58:17 +00:00
parent 790c17dfc1
commit 5a1dc4392f
6 changed files with 108 additions and 30 deletions

View File

@@ -269,6 +269,17 @@ large graphs.
_Newest first._
- 2026-05-08 — Phase 5d semantic fix: anonymous `_` variables are
renamed per occurrence at `dl-add-rule!` and `dl-query` time so
`(p X _) (p _ Y)` no longer unifies the two `_`s. New helpers
`dl-rename-anon-term`, `dl-rename-anon-lit`, `dl-make-anon-renamer`,
`dl-rename-anon-rule` in db.sx; eval.sx's dl-query renames the goal
before search and projects only user-named vars (`_` is filtered
out of the projection list). The "underscore in head" test now
correctly rejects `(p X _) :- q(X).` — after renaming, the head's
fresh anon var has no body binder. Two new eval tests verify
rule-level and goal-level independence. 155/155 expected.
- 2026-05-08 — Phase 5c perf: indexed `dl-find-bindings`. Replaced
the recursive `(rest lits)` walk with `dl-fb-aux lits db subst i n`
using `nth lits i`. Eliminates O(N²) list-copy per body of length